0

Hi there, using TKinter

Is it possible to change a buttons text after you've clicked it?

So if a button has text of "hi" and when clicked has text of "Goodbye"?

button_switch = True

def click():
    global button_switch

    if button_switch:
        button_switch = False
    else:
        button_switch = True

This would switch the state of the button but can i change the buttons text to an image or different text?

Thanks in advance

PS: thats a cropped piece of code, the buttons i've used is all basic TKinter stuff.

3
Contributors
3
Replies
15
Views
7 Years
Discussion Span
Last Post by thehivetyrant
Featured Replies
  • 2

    Use the [ICODE]button_object.config(text=my_text)[/ICODE] option. [URL="http://effbot.org/tkinterbook/button.htm#Tkinter.Button.config-method"]Source[/URL] Read More

  • Actually quite simple using button["text"]: [code]# explore Tkinter button as simple toggle import Tkinter as tk # for Python3 use import tkinter as tk def toggle_text(): """toggle button text between Hi and Goodbye""" if button["text"] == "Hi": # switch to Goodbye button["text"] = "Goodbye" else: # reset to Hi button["text"] … Read More

2

Actually quite simple using button["text"]:

# explore Tkinter button as simple toggle

import Tkinter as tk  # for Python3 use import tkinter as tk

def toggle_text():
    """toggle button text between Hi and Goodbye"""
    if button["text"] == "Hi":
        # switch to Goodbye
        button["text"] = "Goodbye"
    else:
        # reset to Hi
        button["text"] = "Hi"


root = tk.Tk()
root.title("Click the Button")

button = tk.Button( text="Hi", width=12, command=toggle_text)
button.pack(padx=100, pady=10)

root.mainloop()
Comments
very good example
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.