tkinter questions: behavior of StringVar, etc

Alan G Isaac alan.isaac at gmail.com
Mon Mar 30 08:30:27 EDT 2009


On 3/30/2009 3:37 AM Eric Brunel apparently wrote:
> The object traditionally called root is in fact an instance of the tcl
> interpreter that will get the commands generated by the Tkinter module. Due to
> tk architecture, creating this interpreter will also create a window, which is
> inteneded to be your application's main window. This window is called '.' in
> tcl/tk. The root object in Tkinter then represents this '.' window. So the
> instance of Tk is actually 2 things:
> - The interpreter itself;
> - The main window for your application.

OK.

> As for when you should create explicitely an instance of Tk, well, I'd say
> always ;-) Creating an instance of Frame without creating a Tk instance first
> will actually create one, but you'll have no direct access to it.

If I start by creating a frame `f`, then ``f.master`` is the root.
Still, I take your point.

> And you
> might want an access to it for quite a number of reasons: hiding it, make it
> an icon, add menus to it, and so on... All these operations can be done on
> actual windows, not on a Frame which is just a container widget.

Useful.  Thanks.

> All Tkinter widget actually
> reference their interpreter in their tk attribute. The StringVar will probably
> just use that.

Yes, I see how this works now.

> The Tk instance is registered in a hidden variable in the Tkinter module. When
> you don't specify a master, it'll use the latest created Tk instance one by
> default. BTW, the latest should be the only one: it is quite unsafe to create
> several Tk instances in the same application.

I have no desire to do this, but might you pin down "unsafe"?

> I guess
> that having a window automatically created when you just try to instantiate a
> variable has been considered weird. But it's just a guess.

Yes, I am making that same guess.

Thanks!
Alan Isaac



More information about the Python-list mailing list