I am creating a GUI using a class object for the window. This window has quite a few buttons to turn on and off different panels of settings. A simple example of my class is something like:
import tkinter as tk
class MyWindow(object):
def __init__(self):
# flags which are used to draw settings panels if True, and not draw if False
self.settings1 = True
self.settings2 = True
# create frames for each panel of settings
self.frame1 = tk.LabelFrame(window, text = "First panel")
self.frame2 = tk.LabelFrame(window, text = "Second panel")
self.frame1.grid()
self.frame2.grid()
# add button widget to each frame
self.button1 = tk.Button(self.frame1, text = "ON", bg = "green", command = lambda: self.changeButtonState(self.button1, self.settings1))
self.button2 = tk.Button(self.frame2, text = "ON", bg = "green", command = lambda: self.changeButtonState(self.button2, self.settings2))
self.button1.grid()
self.button2.grid()
def changeButtonState(self, button, flag):
if button["text"] == "ON":
button["text"] = "OFF"
button["bg"] = "red"
# change the state of the given flag
flag = False
else:
button["text"] = "ON"
button["bg"] = "green"
# change state of flag
flag = True
print("Settings 1: ", self.settings1, "\nSettings 2: ", self.settings2)
# create the window
window = tk.Tk()
x = MyWindow()
window.mainloop()
The button colours and text changes fine, but the value of self.settings1
and self.settings2
remains unchanged when you click the buttons. I do not understand why, since flag
in the function should be one of the class attributes, but it treats it like a new variable!
How do I create a general function that can alter the state of class attributes that are passed to it?