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
        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.

Recommended Answers

All 3 Replies

Use the button_object.config(text=my_text) option. Source

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"
        # 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)

commented: very good example +10

Thanks, Simple it good.

I appreciate your help on this basic question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.