Reputation: 195
I am trying to update the background color of a TopLevel widget via a radio button. What I want is to have the background color change when the user changes the radio button. Currently, the program opens a new window, with a radio button. The back ground color does not change at all.
from tkinter import *
class Example:
def newWindow(self):
top = Toplevel()
v = IntVar()
v.set(-1)
self.aRadioButton = Radiobutton(top, text="Blue",variable = v, value = 0)
self.aRadioButton.grid(row=1, column=1)
self.aRadioButton = Radiobutton(top, text="Red",variable = v, value = 1)
self.aRadioButton.grid(row=1, column=0)
if v == 0:
top.configure(bg="Blue")
elif v == 1:
top.configure(bg="Red")
def __init__(self, master):
frame = Frame(master, width = 50, height = 50)
frame.grid()
self.aLabel = Label(frame, text = "New window bg colour").grid(row=0)
self.aButton = Button(frame, text="To new window", command=self.newWindow)
self.aButton.grid(row=1)
root = Tk()
app = Example(root)
root.mainloop()
Upvotes: 0
Views: 4414
Reputation: 2276
You have to use an event, when you change the radio button. Attach command method to the radiobuttons like this:
self.aRadioButton = Radiobutton(top, text="Blue",variable = v, value = 0, command=lambda: top.configure(bg="Blue"))
self.aRadioButton = Radiobutton(top, text="Red",variable = v, value = 1, command=lambda: top.configure(bg="Red"))
Also when you do this, you don't need the variable v
if you used it only for this purpouse.
Upvotes: 1