How do you update the button text in Tkinter?

To update the text displayed on a button in Tkinter, you can use the config() method or the configure() method to modify the button’s text attribute. Here’s an example:

import tkinter as tk

def update_text():
    button.config(text="New Text")  # Update the button text

root = tk.Tk()

button = tk.Button(root, text="Old Text")
button.pack()

update_button = tk.Button(root, text="Update Text", command=update_text)
update_button.pack()

root.mainloop()

In the above code, we create a button widget initially with the text “Old Text”. The update_text() function is bound to another button labeled “Update Text” using the command parameter. When the “Update Text” button is clicked, it triggers the update_text() function, which updates the text attribute of the button using the config() method. In this case, the button text is changed to “New Text”.

You can use the config() method or configure() method with other attributes of the button as well, such as bg (background color), font (font style), and state (state of the button).