Reputation: 332
Is it possible to change the objects in the __init__
method? I want to do it, for example to change all colors of any widgets, by one click:
from tkinter import *
class App(Tk):
def __init__(self, bg="black"): #<----------
super().__init__()
self.geometry("400x400")
self.bg = bg
self.frame = Frame(self, bg=self.bg)
self.frame.pack(fill=BOTH, expand=YES)
self.btn = Button(self, text="Change BG", command=self.chng_bg)
self.btn.pack()
def chng_bg(self):
self.bg = "red" # I want to change the self.bg for example to use it bye Themes
# without configure each widget seperatly
if __name__ == '__main__':
App()
mainloop()
So here I'm trying to change the initialized bg=black
into a red background. I searched for a solution, but I didn't find anything.
Upvotes: 0
Views: 120
Reputation: 386240
Changing the variables doesn't change widgets that use the variables. You will need to explicitly change the widgets yourself.
def chng_bg(self):
self.bg = "red"
self.configure(background=self.bg)
self.frame.configure(bg=self.bg)
...
If you use themed (ttk) widgets, you can change the attributes of the theme and all of the widgets that use the theme will automatically update.
Upvotes: 1