我在当前的应用程序中通过过度模块化运行了一个问题。我试图绑定程序的快捷方式,但对于我的生活我无法弄清楚绑定它的位置。我希望它可以在应用程序的所有“屏幕”上工作,我将在以后安全地处理保存和切换。任何帮助,将不胜感激。我还有一个全局文件,其中包含所有文件的所有全局变量。所以,简而言之,我将把绑定放在哪里。 (很抱歉,不包括一个小的可运行样本,但如果需要,我可以制作一个。)
class Application(tk.Frame):
def __init__(self, w, h, master=None):
tk.Frame.__init__(self, master)
tk_screens = scr.GUIScreens(self)
self.grid()
# master.bind("Left",print("Something"))
self.window_width = w
self.window_height = h
globals.window_width = w
globals.window_height = h
# print(window_width,window_height)
# Application.bind('<<Return>>',Application.test(self,"enter"))
print(Application)
tk_screens.home_screen(True)
tk_screens.query_screen(False)
tk_screens.res_screen(False)
tk_screens.settings_screen(False)
tk_screens.customer_screen(False)
# print(master.winfo_width())
# self.update()
def init_widget_state(self, dict, state):
if not state:
for i in dict:
dict[i].grid_remove()
else:
for i in dict:
dict[i].grid()
def swap(self, old, new):
for i in old:
old[i].grid_remove()
# self.query_screen()
for i2 in new:
new[i2].grid()
def test(self,event):
print("Clicked a button:"+event)
if __name__ == "__main__":
main = tk.Tk()
main.wm_title("Title")
main.geometry("1800x900")
# main.state("zoomed")
main.bind('<<Return>>',Application.test(self=main,event="enter"))
main.update()
globals.init()
app = Application(master=main, w=main.winfo_width(), h=main.winfo_height())
app.mainloop()
这是包含所有屏幕的单独文件。
class GUIScreens():
def __init__(self, frame):
self.frame = frame
self.frame.bind('<Return>',lambda :main.Application.test(self,"enter"))
def home_screen(self, state):
self.frame.bind('<<Return>>',main.Application.test(self,"enter"))
"""This where I declare all the widgets for this "screen""""
self.frame.bind('<F1>',print("Test"))
main.Application.init_widget_state(self, globals.home_widgets, state)
PS。操作系统是Ubuntu
答案 0 :(得分:1)
在bind_all
实例上使用Tk
将每个小部件绑定到返回键:
main.bind_all("<Return>", event)
此外,您应该检查您如何使用函数作为参数。有时,您正确使用lambda
,但有时候,您只是使用函数的 return 值,默认为None
。