Reputation: 107
working on GUI Python at the moment. I've created a button that should change what a label says, however, I can't 'export' the new value of lbtext
back into the global namespace. How do I do that? Here is my code:
from tkinter import *
lbtext = ""
def llb():
global lbtext
lbtext = "Hi"
master = Tk()
top = Canvas(master, name="gui")
but = Button(top, text="This is a button", command=llb)
but1 = Button(top, text='Meaning of life is: ', command=llb)
lb = Label(top, text=lbtext)
objs = [top, but, but1, lb]
for i in objs:
i.pack()
mainloop()
Thanks!
Upvotes: 0
Views: 984
Reputation: 1068
Accessing the global namespace works as you intended. The problem is the label updating.
This could only work if you are using a textvariable like:
v = StringVar()
lb = Label(top, textvariable=v)
In your case you have to actively update the label text in the llb callback:
lb.config(text=lbtext)
Upvotes: 2