且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在按下按钮时更改Tkinter标签文本

更新时间:2022-12-19 15:50:38

操作:

message = 'Button Pressed'

不会影响标签小部件.要做的就是将全局变量message重新分配给新值.

will not affect the label widget. All it will do is reassign the global variable message to a new value.

要更改标签文本,可以使用其 .config()方法(也称为.configure()):

To change the label text, you can use its .config() method (also named .configure()):

def press():
    Instruction.config(text='Button Pressed')


此外,创建标签时,您需要在单独的一行上调用pack方法:


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()

否则,会将Instruction分配给None,因为这是该方法的返回值.

Otherwise, Instruction will be assigned to None because that is the method's return value.