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
Photo Tkinter TEXT background image _ShevaKadu 5 793 Nov-02-2020, 10:34 AM
Last Post: joe_momma
  Tkinter - How can I remove the background borders from ttk.Button? TurboC 4 223 Oct-18-2020, 10:58 AM
Last Post: TurboC
  Tkinter - How can I extend a label widget? TurboC 2 210 Oct-13-2020, 12:15 PM
Last Post: zazas321
  tkinter | Button color text on Click Maryan 2 225 Oct-09-2020, 08:56 PM
Last Post: Maryan
  Tkinter - How can I change the default Notebook border color? TurboC 4 280 Oct-06-2020, 10:35 PM
Last Post: TurboC
  [tkinter] color change for hovering over button teacher 4 857 Jul-04-2020, 06:33 AM
Last Post: teacher
  TKINTER - Change font color for night or day Ayckinn 2 683 May-24-2020, 09:25 PM
Last Post: Ayckinn
  [Tkinter] Tkinter custom widget styling and creating custom theme karolp 6 983 May-06-2020, 06:11 PM
Last Post: karolp
  Tkinter calendar widget scratchmyhead 4 868 May-03-2020, 07:01 PM
Last Post: scratchmyhead
  Tkinter help (color) Florent 2 547 Mar-01-2020, 02:59 PM
Last Post: Florent

Forum Jump:

User Panel Messages

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