It’s possible to show one image when a label is enabled and a different image when a label is disabled.
This can be done with ttk Styles.
s = ttk.Style()
# Here we are telling ttk Styles: if a label is disabled, use the image: 'tk_deny'
# and if a label is not disabled, use the image: 'tk_pizza'.
s.map("TLabel",
image=[("disabled", tk_deny), ("!disabled", tk_pizza)])
The full code
import tkinter as tk
from tkinter import ttk, PhotoImage
img_pizza = r"images/pizza.png"
img_deny = r"images/deny.png"
root = tk.Tk()
# Button handler
def toggle_button():
# If the label is disabled, enable it.
if lbl_greeting.instate(["disabled"]):
# Enable the label.
lbl_greeting.state(["!disabled"])
lbl_greeting.configure(text="This label is enabled.\nWe can see the pizza image on the left.")
else:
# Disable the label.
lbl_greeting.state(["disabled"])
lbl_greeting.configure(text="This label is disabled.")
# Load the images into a format that Tk can use.
tk_pizza = PhotoImage(file=img_pizza)
tk_deny = PhotoImage(file=img_deny)
# Create our Toggle button.
btn_toggle = ttk.Button(root, text="Toggle", command=toggle_button)
s = ttk.Style()
# Here we are telling ttk Styles: if a label is disabled, use the image: 'tk_deny'
# and if a label is not disabled, use the image: 'tk_pizza'.
s.map("TLabel",
image=[("disabled", tk_deny), ("!disabled", tk_pizza)])
# The initial text that the user will see in the label when the program loads.
lbl_greeting = ttk.Label(root,
text="This label is enabled.\nWe can see the pizza image on the left.",
compound=tk.LEFT)
lbl_greeting.pack()
btn_toggle.pack()
root.mainloop()