'Where to put event handlers and pass variables to it?
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
