帮我把这个窗口变得更加好看绚丽: root = tk.Tk() root.title("MOM扫码过站") root.geometry("1000x500")
root.configure(bg="#1a1a2e
") # 背景为深色调,适合科技感
font = ("Helvetica", 12)
frame = tk.Frame(root, bg="#1a1a2e
")
frame.pack(expand=True, fill=tk.BOTH, pady=10)
frame.grid_rowconfigure(0, weight=0) frame.grid_rowconfigure(1, weight=0) frame.grid_rowconfigure(2, weight=0) frame.grid_rowconfigure(3, weight=0) frame.grid_rowconfigure(4, weight=1)
frame.grid_columnconfigure(0, weight=1) frame.grid_columnconfigure(1, weight=0)
station_label = tk.Label(frame, text=f"采集点: {config['station']}", font=font, fg="white", bg="#1a1a2e
")
station_label.grid(row=0, column=0, columnspan=2, sticky='ew', pady=5)
operater_label = tk.Label(frame, text=f"操作人: {config['operater']}", font=font, fg="white", bg="#1a1a2e
")
operater_label.grid(row=1, column=0, columnspan=2, sticky='ew', pady=5)
order_frame = tk.Frame(frame, bg="#1a1a2e
")
order_frame.grid(row=2, column=0, columnspan=3, pady=5)
work_order_label = tk.Label(order_frame, text="工单编号:", font=font, fg="white", bg="#1a1a2e
")
work_order_label.pack(side=tk.LEFT, padx=(0, 10)) # 设置右侧间距
def refresh_work_order_codes(): # 重新调用接口获取新的工单编号列表 new_work_order_codes = load_work_order_codes()
if new_work_order_codes:
# 在工单编号列表中添加一个空值选项
if "" not in new_work_order_codes: # 避免重复添加空值
new_work_order_codes.insert(0, "")
work_order_combobox['values'] = new_work_order_codes # 更新下拉框的值
work_order_combobox.set("") # 设置默认值为空
else:
logging.error("未能获取到工单编号")
work_order_codes = load_work_order_codes()
if work_order_codes and "" not in work_order_codes: work_order_codes.insert(0, "") # 在列表的第一个位置插入空值
work_order_combobox = ttk.Combobox(order_frame, values=work_order_codes, width=25, state='readonly', font=font) work_order_combobox.pack(side=tk.LEFT) # 使下拉框与标签在同一行
work_order_combobox.set("") # 默认显示为空
refresh_button = tk.Button(order_frame, text="刷新", command=refresh_work_order_codes, font=font, bg='#3498db
', fg='white', relief="flat", width=8)
refresh_button.pack(side=tk.LEFT, padx=(10, 0)) # 设置左侧间距
def button_style(button):
button.config(font=font, relief="flat", bg='#2ecc71
', fg='white', activebackground='#27ae60
', activeforeground='white')
def close_warning_lights(): send_serial_command([0xA0, 0x07, 0x00, 0xA7]) # 关闭红色警告灯 send_serial_command([0xA0, 0x02, 0x01, 0xA3]) # 恢复绿灯
def close_all_lights(): send_serial_command([0xA0, 0x00, 0x00, 0xA0]) clear_log()
def clear_log(): log_text.config(state=tk.NORMAL) log_text.delete('1.0', tk.END) log_text.config(state=tk.DISABLED)
close_button1 = tk.Button(frame, text="恢复绿灯", command=close_warning_lights, bg='lightgreen') close_button2 = tk.Button(frame, text="关闭所有灯光", command=close_all_lights, bg='white')
button_style(close_button1) button_style(close_button2)
close_button1.grid(row=3, column=0, padx=20, pady=5, sticky='ew') close_button2.grid(row=3, column=1, padx=20, pady=5, sticky='ew')
frame.grid_columnconfigure(0, weight=1) frame.grid_columnconfigure(1, weight=1)
log_text = scrolledtext.ScrolledText(frame, width=50, height=10, font=font, bg='#2c3e50
', fg='white')
log_text.grid(row=4, column=0, columnspan=2, padx=10, pady=10, sticky='nsew')
log_text.config(state=tk.DISABLED)
log_text.tag_configure('success', foreground='black') log_text.tag_configure('failure', foreground='red')
def startup_green_light(): send_serial_command([0xA0, 0x02, 0x01, 0xA3]) # 绿灯指令
def start_listening(): startup_green_light() listener = keyboard.Listener(on_press=on_key_press) listener.start()
date_check_thread = threading.Thread(target=check_date_periodically, daemon=True) date_check_thread.start()
start_listening()
root.mainloop()
让我们来改进这个窗口的设计,使其更加现代化和吸引人。我们将使用自定义样式、渐变背景和一些动画效果来增强用户界面的视觉吸引力。以下是改进后的代码:
这个新版本的界面有以下改进:
customtkinter
库来创建更现代化的 UI 组件。要运行这个新版本,你需要安装 customtkinter
库:
pip install customtkinter
这个新设计不仅看起来更加现代和专业,而且还保留了原有的所有功能。它提供了更好的用户体验,同时也使代码更加结构化和易于维护。