Python 3, Tkinter, How to update button text

后端 未结 7 1735
孤城傲影
孤城傲影 2020-12-06 08:41

Im trying to make it so that when the user clicks a button, it becomes \"X\" or \"0\" (Depending on their team). How can I make it so that the text on the button is updated?

7条回答
  •  没有蜡笔的小新
    2020-12-06 09:07

    The Button widget, just like your Label, also has a textvariable= option. You can use StringVar.set() to update the Button. Minimal example:

    import tkinter as tk
    
    root = tk.Tk()
    
    def update_btn_text():
        btn_text.set("b")
    
    btn_text = tk.StringVar()
    btn = tk.Button(root, textvariable=btn_text, command=update_btn_text)
    btn_text.set("a")
    
    btn.pack()
    
    root.mainloop()
    

提交回复
热议问题