CTkinter Entry组件快捷键调用返回历史旧值问题求助
问题原因分析
- 重复绑定快捷键:每次点击Save按钮调用
shut_number时,都会执行kb.add_hotkey("Shift+1", vstavit),给同一个快捷键新增一个回调函数。多次点击后,Shift+1会绑定多个vstavit实例,按下快捷键时所有绑定的函数都会依次执行,因此会输出所有历史值。 - 闭包捕获旧值:每个
vstavit函数是在点击Save时创建的,它捕获了当时date2、time2、shut的旧值,所以旧的回调函数只会输出创建时的Entry内容。
解决方法
方案1:先移除旧绑定再添加新绑定
每次点击Save时,先检查并移除之前的Shift+1快捷键绑定,再添加新的回调。这样快捷键始终只绑定最新的回调函数,适合需要固定保存点击Save时内容的场景。
修改后的代码:
import customtkinter import keyboard as kb import time # 保存快捷键的回调引用,方便后续移除 hotkey_callback = None def shut_number(): global hotkey_callback # 先移除之前的快捷键绑定 if hotkey_callback is not None: kb.remove_hotkey(hotkey_callback) date2 = date1.get() time2 = time1.get() shut = shutnumber.get() print ("New Value") print (date2,time2,shut) def vstavit(): print("Hotkey print") print(date2,time2,shut) time.sleep(.5) kb.send("Tab") kb.write(date2) time.sleep(.5) kb.send("Tab") kb.write(time2) time.sleep(.5) kb.send("Tab") kb.send("Tab") kb.write(shut) # 保存新的回调引用 hotkey_callback = kb.add_hotkey("Shift+1", vstavit) app = customtkinter.CTk() app.title("my app") app.geometry("800x150") date1 = customtkinter.CTkEntry(app, placeholder_text="Date") date1.grid(row=0, column=1, padx=20, pady=20) time1 = customtkinter.CTkEntry(app, placeholder_text="Time") time1.grid(row=0, column=2, padx=20, pady=20) shutnumber = customtkinter.CTkEntry(app, placeholder_text="Shut") shutnumber.grid(row=0, column=3, padx=20, pady=20) button = customtkinter.CTkButton(app, text="Save", command=shut_number) button.grid(row=1, column=0, padx=20, pady=20, sticky="ew", columnspan=4) app.grid_columnconfigure(0, weight=1) app.mainloop()
方案2:让回调实时获取Entry当前值
让vstavit在执行时直接调用date1.get()等方法获取最新值,同时配合移除旧绑定,确保快捷键始终输出Entry当前最新内容。
修改后的代码:
import customtkinter import keyboard as kb import time hotkey_callback = None def shut_number(): global hotkey_callback if hotkey_callback is not None: kb.remove_hotkey(hotkey_callback) print ("New Value") print (date1.get(), time1.get(), shutnumber.get()) def vstavit(): # 实时获取Entry当前值 current_date = date1.get() current_time = time1.get() current_shut = shutnumber.get() print("Hotkey print") print(current_date, current_time, current_shut) time.sleep(.5) kb.send("Tab") kb.write(current_date) time.sleep(.5) kb.send("Tab") kb.write(current_time) time.sleep(.5) kb.send("Tab") kb.send("Tab") kb.write(current_shut) hotkey_callback = kb.add_hotkey("Shift+1", vstavit) app = customtkinter.CTk() app.title("my app") app.geometry("800x150") date1 = customtkinter.CTkEntry(app, placeholder_text="Date") date1.grid(row=0, column=1, padx=20, pady=20) time1 = customtkinter.CTkEntry(app, placeholder_text="Time") time1.grid(row=0, column=2, padx=20, pady=20) shutnumber = customtkinter.CTkEntry(app, placeholder_text="Shut") shutnumber.grid(row=0, column=3, padx=20, pady=20) button = customtkinter.CTkButton(app, text="Save", command=shut_number) button.grid(row=1, column=0, padx=20, pady=20, sticky="ew", columnspan=4) app.grid_columnconfigure(0, weight=1) app.mainloop()
内容的提问来源于stack exchange,提问作者Сергей Разумчиков
相关产品推荐
相关产品推荐

