'What is the Tkinter Variable class first argument used for?

The Tkinter Variable subclasses such as StringVar or IntVar allow for a tk Frame widget as the first argument. What is this argument used for?



Solution 1:[1]

It's more about what root window to use. It defaults to the first root created. However, sometimes (very rarely) you may need to use Tk() more than once in a program. In that case it's important to associate all variables with the appropriate root.

Here's an example:

import tkinter as tk

class menu:
    def __init__(self):
        self.game = tk.Tk()
        self.game.geometry('200x200')
        self.var = tk.StringVar()
        #~ self.var = tk.StringVar(master=self.game) # this solves the problem
        ent = tk.OptionMenu(self.game, self.var, 'one', 'two', 'three', 'four')
        ent.pack()
        lbl = tk.Label(self.game, textvariable=self.var)
        lbl.pack()
        btn = tk.Button(self.game, text="new window", command=self.playagain)
        btn.pack()
        self.game.mainloop()

    def playagain(self):
        menu()

menu()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Novel