[Tutor] General object-oriented procedure question

Abel Daniel abli@freemail.hu
Fri Jul 18 16:03:02 2003


> I have a program that has several classes, each with multiple functions
> defined. I have mapped my way through enough to basically understand the
> flow of things, but I am not sure what happens when it finishes the
> mainloop (standard garden-variety mainloop).
You mean tkinter mainloop? Please explicitly mention tkinter when your
problem has something to do with it, as there are several graphical
bindings for python, and most likely several modules that have
mainloops. 
I'll assume that you are using tkinter.
> Say for instance that one of
> the classes includes a button that I press to initiate some sequence of
> activities. I finish the sequence fine, but then I get back to the class
> and the program hangs.
Hangs in what sence? The gui becomes unresponsive, you can't click on
any widget, and if you cover the window with something else ad re-expose
it it doesn't get redrawn? Post a 'working' example. (where 'working'
means shows the problem, so it might be better to call it 'broken' :) )
> I have a pulldown menu which includes an exit, and
> that works, but what if I simply want to end after the button activity
> ceases. How do I do that?
What do you mean 'button activity ceases'? After you release a button on
the keyboard? Then bind to the KeyRelease event, and call the quit()
method of a widget from the event callback.
> I've inserted print statements after the
> mainloop call, but they are never reached unless I exit from the menu.
In tkinter you set up your app, call mainloop, and from then on most of
the time is spent in the mainloop which does all the houskeeping stuff
(reedrawing the screen when needed, handling keypresses, etc.) and calls
your event callbacks occasionally. Your event callbacks return to the
mainloop. When you exit the mainloop, the gui is teared down, and your
graphical widgets are gone. Most likely you don't want to do anything
after the mainloop. What are you trying to achieve?

> I need to add something to the end of the mainloop to end when finished,
> but I am unsure where to insert it or exactly what to insert.
I think the mainloop can be ended by two things:
you explicitly end it, with, say the .quit() method of a widget
or
the window manager closes the app down. (The user closes the window by
clicking the icon in the titlebar.)

In the first case, you simply add your code to immedatelly before that
call to the .quit() method.
In the second case, the app is actually sent a message from the
windowmanager to close itself. (At least thats my understanding of who
the thing works.) You can 'intercept' this by using the .protocol()
method of the root window. See
http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm
(near the bottom)

Abel Daniel