I have summarized the script in such a way that it only shows what is necessary to show my problem. In short, I would like to globally
bind the callback functions to all button
type widgets in the app.
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()
CodePudding user response:
try it this way: you tried to call "self". At this point you need an object reference. But you didn't have that. Public objects can still be integrated into a class.
from tkinter import *
def press( event):
print('press')
def release( event):
print('release')
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>', press)
self.btn_0.bind('<ButtonRelease>', 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>', press)
self.btn_0.bind('<ButtonRelease>', release)
class Controller(Frame):
def __init__(self, master, **kwargs):
super().__init__(master, **kwargs)
global press, release
self.frm1 = Frame_1(self, bg='blue')
self.frm1.pack()
self.frm2 = Frame_2(self, bg='green')
self.frm2.pack()
root = Tk()
app = Controller(root)
app.pack()
root.mainloop()
CodePudding user response:
Tkinter provides a bind_class
method that allows you to bind to a widget class rather than an individual widget. In your case, the class would be Button
.
Your code might look something like this:
root=Tk()
app=Controller(root)
root.bind_class("Button", "<ButtonPress-1>", app.press)
root.bind_class("Button", "<ButtonRelease-1>", app.release)