DavalliTV DavalliTV - 5 months ago 22
Python Question

(Python) Update background via radio button python

So I am trying to change the background of this frame when a certain radiobutton is selected.

My frame is in a class and the functions for the radiobuttons or outside of this class. (This is so I can call them on all the other frames.)

The problem is that i get the following error whenever I select a radiobutton:

configure() missing 1 required positional argument: 'self'

However, when placing
inside or
, it says that
is not defined.

I'm not 100% on when to use
so any help would be apprectiated.

The functions for changing the background:

def bgLight():
Options.configure(self, bg="#fff")
Options.configure(self, fg="#000")

def bgDark():

And the code for the radio buttons:

class Options(tk.Frame):

def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller

label = tk.Label(self, text="Options", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
label = tk.Label(self, text="Change it up a bit!", font=SUBTITLE_FONT)
label.pack(fill="x", pady=10)

#These are the two radio buttons that will change the background color
radio = tk.Radiobutton(self, text="One", value=1, command=bgLight)
radio = tk.Radiobutton(self, text="Two", value=0, command=bgDark)

button = tk.Button(self, text="Back To Main Menu",
command=lambda: controller.show_frame("Menu"))

Like I said, I'm not 100% confident on when or where to use
so I have mainly been experimenting on where to put it.

Answer Source

The method configure needs to be applied to instances, not to the base class.

To make it work at the Options class level, you can declare the functions bgLight and bgDark as methods of the class :

def bgLight(self):
    #self.configure(fg="#000") no foreground on Frame

def bgDark(self):

Don't forget to change the calls in Radiobutton commands :

radio = tk.Radiobutton(self, text="One", value=1, command=self.bgLight)
radio = tk.Radiobutton(self, text="Two", value=0, command=self.bgDark)

A way to handle this at the application level could be to define an attribute background and foreground in the controler , then call the method configure when you need, by giving the parameter from controler. Careful, some widgets don't have a foreground attribute, like tkinter.Frame.

In Options, the methods could be :

def bgLight(self):
    self.controller.background = "#fff"
    self.controller.foreground = "#000"

def bgDark(self):
    self.controller.background = "#000"
    self.controller.foreground = "#fff"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download