[Tutor] (no subject)

Abel Daniel abli@freemail.hu
Sun Jun 8 12:44:01 2003


bob smith wrote:
> Hi.  I'm just starting to learn to use the Tkinter module for creating
> GUIs.  I'm using a tutorial that gives a basic starting program of:
> 
> from Tkinter import *
> root = Tk()
> app = Frame(root)
> root.mainloop()
> 
> But do I really need root?  It seems like the program could simply be:
> 
> from Tkinter import *
> app = Frame()
> app.mainloop()
> 
> This second program works just as well as the first and seems more
> intuitive to me.  I'm creating an application that is a Frame and I
> start the application by invoking the application's mainloop() method.
> 
> The first example seems more obtuse.  root seems unnecessary and
> invoking root's mainloop() instead of app's seems odd to me too (if I
> want to start app, I should invoke a method of app).
> 
> So, what's wrong with the second version?  Will I run into problems
> later when I add more widgets and get into more complex GUIs?
> 
> Thanks,
> Bob
> 
Well, I don't think there is too much difference between the two. One
thing which the second method makes impossible is to place an other
frame in the same window next to app. (As you would need something like
other_frame = Other_App(root), where you have to pass in root.)

Judging from the tkinter source both mainloop calls call the same code
and thus do exactly the same thing.

I don't think anything is wrong with the second version. And even if
there is, this isn't exactly a major design decision, you can change it
anytime you want. It doesn't affect how you write the code for app, nor
how your users use it.

Abel Daniel
ps. Two remarks:

"import Tkinter" instead of "from Tkinter import *" is usually
considered better as it doesn't pollutes your global namespace.
(of course this means that you have to type "Tkinter." a lot of places,
but I think it's worth it.)

In the example you posted, you don't do any geometry management (like
pack() or grid() ), You didn't make the mistake of pack()-ing app in
it's __init__ method, did you?