I have this code, and its meant to change the text of the Instruction
label when the item button is pressed. It doesn't for some reason, and I'm not entirely sure why. I've tried creating another button in the press()
function with the same names and parameters except a different text.
import tkinter
import Theme
import Info
Tk = tkinter.Tk()
message = 'Not pressed.'
#Sets window Options
Tk.wm_title(Info.Title)
Tk.resizable(width='FALSE', height='FALSE')
Tk.wm_geometry("%dx%d%+d%+d" % (720, 480, 0, 0))
#Method run by item button
def press():
message = 'Button Pressed'
Tk.update()
#item button
item = tkinter.Button(Tk, command=press).pack()
#label
Instruction = tkinter.Label(Tk, text=message, bg=Theme.GUI_hl2, font='size, 20').pack()
#Background
Tk.configure(background=Theme.GUI_bg)
Tk.mainloop()
Doing:
message = 'Button Pressed'
will not affect the label widget. All it will do is reassign the global variable message
to a new value.
To change the label text, you can use its .config()
method (also named .configure()
):
def press():
Instruction.config(text='Button Pressed')
In addition, you will need to call the pack
method on a separate line when creating the label:
Instruction = tkinter.Label(Tk, text=message, font='size, 20')
Instruction.pack()
Otherwise, Instruction
will be assigned to None
because that is the method's return value.
You can make message
a StringVar
to make callback.
message = tkinter.StringVar()
message.set('Not pressed.')
You need to set message
to be a textvariable
for Instruction
:
Instruction = tkinter.Label(Tk, textvariable=message, font='size, 20').pack()
and then
def press():
message.set('Button Pressed')