How to change Tkinter label text on button press

后端 未结 2 1120
故里飘歌
故里飘歌 2021-01-14 10:15

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

2条回答
  •  感情败类
    2021-01-14 11:02

    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.

提交回复
热议问题