I started programming a simple GUI application in Python, using Tkinter library. Everything works fine but, to dynamically change any widget (for example a button from "login" to "logout", etc), I have to get the variable containing the widget object itself in the function from outside. I can do this in two ways:
- passing the widget as a argument;
- using global variables.
A simplified example:
1) Passing as argument:
def changeLabel(b):
b.config(text='Logout')
btn1 = Button(f0,text="Login",width=15)
btn1.grid(row=1,column=2,rowspan=1,padx=10,ipadx=10)
changeLabel(btn1)
2) Or using a global variable:
def changeLabel():
global btn1
btn1.config(text='Logout')
btn1 = Button(f0,text="Login",width=15)
btn1.grid(row=1,column=2,rowspan=1,padx=10,ipadx=10)
changeLabel(btn1)
Now, I know that global variables should be avoided but passing every widget as argument through many functions, even in a simple application, it's a mess.
So, what's the best way to manipulate the Tkinter widgets at runtime? Can you suggest me the right way? Thank you