I've been trying to bind a mouseclick to a grid in a certain frame, after this question I made a new class and bound the event to that, which works. But the function of the mouse event needs a variable that is made in the MainApp class, I can't get it to work properly. Depending on the arrangement either I can't get the widgets
variable to be used in the function, or the order of functions/classes is wrong and thus the program won't find something because it is referenced too soon.
So my main question is how do I get the function working, which I think mostly involves where do I put that thing? As a function in the Schedule_Label
class? as a function of the MainApp
? A loose function? Is it smarter to put all the event handlers in a seperate Python file and import that?
Excluding some code this is what is happening:
class Schedule_Label(tk.Label):
def __init__(self, parent, *args, **kwargs):
tk.Label.__init__(self, parent, *args, **kwargs)
self.bind("<Button-1>", mouse_1)
...
class Schedule, this class uses Schedule_Label
class MainApp(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
#class schedule is used here
...
schedule_widgets = self.schedule.New(date, stafflist)
...
def mouse_1(event):
r = event.widget.grid_info()['row']
c = event.widget.grid_info()['column']
schedule_widgets[(r,c)].configure(state="active")
if __name__ == "__main__":
root = tk.Tk()
app = MainApp(root)
app.pack()
root.mainloop()
You are calling schedule_widgets
from mouse_1
, which is in the name space of MainApp
, to be able to use it like that use global
keyword.
schedule_widgets = None#-------------------------------------------------------- new ---
class Schedule_Label(tk.Label):
def __init__(self, parent, *args, **kwargs):
tk.Label.__init__(self, parent, *args, **kwargs)
self.bind("<Button-1>", mouse_1)
...
class Schedule, this class uses Schedule_Label
class MainApp(tk.Frame):
def __init__(self, parent, *args, **kwargs):
global schedule_widgets #----------------------------------------------- new ---
tk.Frame.__init__(self, parent, *args, **kwargs)
#class schedule is used here
...
schedule_widgets = self.schedule.New(date, stafflist)
...
def mouse_1(event):
global schedule_widgets #--------------------------------------------------- new ---
r = event.widget.grid_info()['row']
c = event.widget.grid_info()['column']
schedule_widgets[(r,c)].configure(state="active")
if __name__ == "__main__":
root = tk.Tk()
app = MainApp(root)
app.pack()
root.mainloop()
There is an other way to do it, but schedule_widgets
should be an attributes of MainApp
and when you instantiate schedule_Label
you should give parent reference so that mouse_1
can access schedule_widgets
with parent reference
class Schedule_Label(tk.Label):
def __init__(self, parent, *args, **kwargs):
tk.Label.__init__(self, parent, *args, **kwargs)
self.bind("<Button-1>", lambda event, p=parent: mouse_1(event, p)) # --- new ---
...
class Schedule, this class uses Schedule_Label
class MainApp(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
#class schedule is used here
...
self.schedule_widgets = self.schedule.New(date, stafflist) #------------ new ---
...
def mouse_1(event, parent): #--------------------------------------------------- new ---
r = event.widget.grid_info()['row']
c = event.widget.grid_info()['column']
parent.schedule_widgets[(r,c)].configure(state="active") #------------------ new ---
if __name__ == "__main__":
root = tk.Tk()
app = MainApp(root)
app.pack()
root.mainloop()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.