Pho*_*nix 3 python label tkinter button python-3.x
我有这个代码,它意味着在Instruction按下项目按钮时更改标签的文本.它不是出于某种原因,我不完全确定原因.我尝试在press()函数中创建另一个按钮,除了不同的文本外,它们具有相同的名称和参数.
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()
Run Code Online (Sandbox Code Playgroud)
iCo*_*dez 11
这样做:
message = 'Button Pressed'
Run Code Online (Sandbox Code Playgroud)
不会影响标签小部件.它所做的只是将全局变量重新分配message给新值.
要更改标签文本,可以使用其.config()方法(也称为.configure()):
def press():
Instruction.config(text='Button Pressed')
Run Code Online (Sandbox Code Playgroud)
此外,pack在创建标签时,您需要在单独的行上调用该方法:
Instruction = tkinter.Label(Tk, text=message, font='size, 20')
Instruction.pack()
Run Code Online (Sandbox Code Playgroud)
否则,Instruction将被分配,None因为这是方法的返回值.