I have a function that creates a tkinter window with many checkbutton widgets on it. Later, a button on another window saves the states of these checkbuttons. The function that does this is passed the reference to the window the checkbuttons are on, through which (by .winfo_children()) it gets the references to each checkbutton widget.
Even though all the checkbuttons on the window are created, set, and get'd identically, different ones throw an error each time the code is run: _tkinter.TclError: can't read "PY_VAR0": no such variable.
This leads me to believe that some of my BooleanVar() tkinter variables are being garbage-collected while I still need them. How do I stop my tkinter variables from being garbage-collected?
Similar to this question, but I would prefer a solution that stops the garbage-collection in the first place, as changing the structure of my functions would be difficult.
Like with any python object, you need to keep a permanent reference to them. Either you need to save them as an attribute of an object, or you need to make them a global variable.
You might also be seeing the problem if you create more than one instance of
Tk, and create a variable in one root window but try to use it in another.