Search code examples
pythonvariablestkinter

What is the purpose of the "master" parameter in the tkinter Variable class/subclasses?


I've been looking for some more detailed information regarding the Variable subclasses in tkinter, namely BooleanVar, DoubleVar, IntVar, and StringVar. I'm hoping someone with broader knowledge can point me in the right direction.

Given the constructor: tkinter.Variable(master=None, value=None, name=None) I'm curious what the utility of the master parameter is for these classes. I understand that it's equivalent to the master parameter for other tkinter widgets, but I'm not sure I understand how it affects these variable classes specifically; it's a bit more intuitive when the widget is the child of a given class.

I typically ignore this value, as I understand that (assuming this is in my root class that inherits from Tk) self.var = tk.StringVar() is equivalent to self.var = tk.StringVar(self), or self.var = tk.StringVar(None). Should I be including this? Is it providing some functionality I would otherwise be missing? I'm not necessarily looking for "What is the best practice here", but rather an explanation of the intended use.

Any info is much appreciated!

Here's a link to what little information I've been able to find, if anyone else is curious TkDocs - Variable


Solution

  • When you create an instance of Tk, you are doing more than just creating a widget. For each instance, you are also creating an embedded Tcl interpreter. This tcl interpreter is where all of the widgets and variables and image objects exist. The objects within this interpreter are only available to that interpreter and cannot be shared with other interpreters.

    If you create multiple instances of Tk, the master parameter lets you tell tkinter which interpreter each variable belongs to. Without it, the variables and widgets will be created in the interpreter of the first instance of Tk.