我以这样一种方式总结了脚本,它只显示了显示我的问题所必需的内容。简而言之,我想将globally
回调函数绑定到appbutton
中的所有类型小部件。
class Frame_1(Frame):
def__init__(self,master, **kwargs):
super().__init__(master, **kwargs)
self.btn_0 = Button(self, text='Press 10')
self.btn_0 .pack() #...
# links that I would like to disappear
# and replace with a global link that only reaches button type widgets
self.btn_0 .bind('<ButtonPress>', self.master.press)
self.btn_0 .bind('<ButtonRelease>', self.master.release)
class Frame_2(Frame):
def__init__(self,master, **kwargs):
super().__init__(master, **kwargs)
self.btn_0 = Button(self, text='Press 10')
self.btn_0 .pack() #....
self.btn_0 .bind('<ButtonPress>', self.master.press)
self.btn_0 .bind('<ButtonRelease>', self.master.release)
class Controller(Frame):
def__init__(self,master, **kwargs):
super().__init__(master, **kwargs)
self.frm1 = Frame_1(self, bg='blue')
self.frm1 .pack()
self.frm2 = Frame_2(self, bg='green')
self.frm2 .pack()
def press(self, e):
print('press')
def release(self, e):
print('release')
root=Tk()
app=Controller(root)
app.pack()
root.mainloop()
Tkinter 提供了一种bind_class
方法,允许您绑定到小部件类而不是单个小部件。在您的情况下,课程将是Button
.
您的代码可能如下所示:
root=Tk()
app=Controller(root)
root.bind_class("Button", "<ButtonPress-1>", app.press)
root.bind_class("Button", "<ButtonRelease-1>", app.release)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句