[Tkinter-discuss] Issues converting TKinter application to Ttk

Michael Lange klappnase at web.de
Fri Jan 3 09:42:49 CET 2014


Hi,

On Thu, 2 Jan 2014 20:59:44 +0000
"Fleming, Land D. (JSC-ER111)[Jacobs Technology, Inc.]"
<land.d.fleming at nasa.gov> wrote:

> First problem:
> 
> I found that the styles I defined for widgets in the application were
> ignored when I made the application's master an instance of Tk() as
> follows:
> 
>       root = Tk()
>       app = App(root, style='My.TPanedwindow', orient=VERTICAL)
> 
> After considerable trial and error (mostly error) I found that the
> styles worked as advertised, if I changed the application master to an
> instance of Toplevel rather than of Tk:
> 
>       top = Toplevel()
>       app = App(top, style='My.TPanedwindow', orient=VERTICAL)
> top.mainloop()

I haven't worked much with custom styles so far, so I cannot tell why this
happens. Maybe you could try to define the My.TPanedwindow style within
the App class to work around this?


> Second Problem:
> 
> When the mainloop executed as above, what appeared to be an extraneous
> blank window was stacked on top of the application window. This window
> didn't appear when I was using basic Tkinter.  I noticed that if I
> killed the extraneous window by clicking on the "X" in the window's
> upper right corner, it also killed the application, which suggested the
> "extraneous" window was really the root window. After searching the
> documentation and much trial and error, I found that I could get the
> root window to go away without killing my app by explicitly calling
> withdraw() on the root before running the mainloop:
> 
>       Tkinter._default_root.withdraw()
>       app.mainloop()
> 
> This would have been a show stopper if I hadn't been able to work
> around it, because I couldn't give an application to end users that
> pops up a meaningless empty window that they have to close themselves.
> I wasn't able to locate much documentation on the root window, and I
> only found out about _default_root by looking at Tkinter.py. Further, I
> found nothing to indicate that the root had a withdraw method, and just
> tried it after running out of all other options I could think of.
> Luckily, it worked.

The reason that _default_root is not documented is that it is an internal
or "private" attribute which at least in theory might be changed by the
Tkinter developers, which is told us by the leading underscore character
(although I believe in the case of _default_root it is rather safe to
use it anyway).

The "proper" way to have a Toplevel as main application window is
something like:

root = Tk()
root.withdraw()
top = Toplevel(root)
# make sure clicking the window's "X" button does not only
# close the Toplevel but the application altogether:
top.protocol('WM_DELETE_WINDOW', root.quit)
(...)
root.mainloop()
root.destroy()

As far as documentation is concerned, there are at least two excellent
reference manuals available online:

http://effbot.org/tkinterbook/tkinter-index.htm by Frederik Lundh and
http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html by John W.
Shipman.
(the latter however doesn't mention explicitely at the Toplevel page that
the Toplevel's methods are also valid for Tk instances, the first one
does ;) .

Regards

Michael


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

One does not thank logic.
		-- Sarek, "Journey to Babel", stardate 3842.4


More information about the Tkinter-discuss mailing list