Bottom Page

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()
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PyGUI] Python 3.8.1 Tkinter Widget stete change sasiap 4 92 Feb-14-2020, 10:38 AM
Last Post: sasiap
  Make Label Text background (default color) transparent using tkinter in python barry76 1 2,098 Nov-28-2019, 10:19 AM
Last Post: Larz60+
  Unable to put background image on Tkinter Frame jenkins43 2 815 Nov-27-2019, 11:38 AM
Last Post: jenkins43
  [Tkinter] changing background color of a button ieee488 2 486 Sep-06-2019, 01:24 PM
Last Post: ieee488
  change background color of button ieee488 3 2,064 Jul-30-2019, 09:49 PM
Last Post: ieee488
  GUI Tkinter Widget Positions punksnotdead 3 526 Jun-12-2019, 06:06 PM
Last Post: Yoriz
  [Tkinter] How to run animations in background with tkinter? Pythenx 1 692 May-20-2019, 07:10 PM
Last Post: Yoriz
  how to insert image into Text widget Tkinter atlass218 5 2,541 Apr-17-2019, 05:28 AM
Last Post: atlass218
  [Tkinter] color a string of characters in tkinter atlass218 6 1,477 Mar-28-2019, 03:17 PM
Last Post: atlass218
  [Tkinter] Problem with tkinter search widget poopcupine 1 668 Mar-25-2019, 08:24 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)