Python Forum
Restoring Tkinter widget background to original color
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Restoring Tkinter widget background to original color
#1
I am trying to restore the widget background back to it's original color if the button is clicked again for the 5 buttons listed below as btn1,btn2,btn3,btn4,btn5. The first time the buttons are clicked
the background color changes to red as you can see with the def on_click function. I want the buttons to change back to the original color if they are clicked again. So basically I want the background color to change to red when the buttons are clicked and back to original color if the buttons are already red. How would I do this?
from tkinter import *
from PIL import ImageTk, Image
import random


root = Tk()
frame = Frame(root)
frame.pack()
root.title('Learn to Code at Codemy.com')

def on_click(event):
    event.widget.config(bg='red')


def open():
    global my_img
    global image2
    global image3
    global image4
    global image5
    global image6
    top = Toplevel()
    top.title('First Roll')
    lbl = Label(top, text="Your first roll is:").pack()
    lbl2 = Label(top, text="Click on the dice you would like to keep for this round.").pack()
    my_img = ImageTk.PhotoImage(Image.open("C:\\Users\\Dan\\Desktop\\Alea_1.png"))
    image2 = ImageTk.PhotoImage(Image.open("C:\\Users\\Dan\\Desktop\\Alea_2.png"))
    image3 = ImageTk.PhotoImage(Image.open("C:\\Users\\Dan\\Desktop\\Alea_3.png"))
    image4 = ImageTk.PhotoImage(Image.open("C:\\Users\\Dan\\Desktop\\Alea_4.png"))
    image5 = ImageTk.PhotoImage(Image.open("C:\\Users\\Dan\\Desktop\\Alea_5.png"))
    image6 = ImageTk.PhotoImage(Image.open("C:\\Users\\Dan\\Desktop\\Alea_6.png"))

    dice = [my_img, image2, image3, image4, image5, image6]

    rdice = random.choice(dice)
    rdice2 = random.choice(dice)
    rdice3 = random.choice(dice)
    rdice4 = random.choice(dice)
    rdice5 = random.choice(dice)


    btn1 = Button(top, image=rdice)
    btn1.pack()
    btn1.bind('<Button-1>', on_click)
    btn2 = Button(top, image=rdice2)
    btn2.pack()
    btn2.bind('<Button-1>', on_click)
    btn3 = Button(top,image=rdice3)
    btn3.pack()
    btn3.bind('<Button-1>', on_click)
    btn4 = Button(top,image=rdice4)
    btn4.pack()
    btn4.bind('<Button-1>', on_click)
    btn5 = Button(top,image=rdice5)
    btn5.pack()
    btn5.bind('<Button-1>', on_click)

btn = Button(frame, text="Open Second Window",command = open).pack()

mainloop()
Reply
#2
You first have to tell the function, on_click, which button was clicked. Then you can use the config option to get the current background color and change it accordingly.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TKinter Widget Attribute and Method Quick Reference zunebuggy 3 772 Oct-15-2023, 05:49 PM
Last Post: zunebuggy
  My Background Image Is Not Appearing (Python Tkinter) HailyMary 2 3,858 Mar-14-2023, 06:13 PM
Last Post: deanhystad
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,580 Jun-26-2022, 06:26 PM
Last Post: menator01
  Tkinter - How can I change the default Notebook border color? TurboC 5 14,562 May-23-2022, 03:44 PM
Last Post: bigmac
  [Tkinter] Background inactivity timer when tkinter window is not active DBox 4 2,838 Apr-16-2022, 04:04 PM
Last Post: DBox
  Can't get tkinter button to change color based on changes in data dford 4 3,314 Feb-13-2022, 01:57 PM
Last Post: dford
  GTK3.0 Color Selection Widget Robib 3 1,928 Nov-04-2021, 04:45 PM
Last Post: Axel_Erfurt
  Tkinter Exit Code based on Entry Widget Nu2Python 6 2,845 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  tkinter text widget word wrap position chrisdb 6 7,401 Mar-18-2021, 03:55 PM
Last Post: chrisdb
Question [Tkinter] Can I set background color for each item in tkinter Combobox? water 1 5,030 Dec-10-2020, 07:48 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020