question about the mainloop
gagsl-py2 at yahoo.com.ar
Mon Apr 21 07:31:12 CEST 2008
En Mon, 21 Apr 2008 00:57:38 -0300, globalrev <skanemupp at yahoo.se> escribió:
> On 21 Apr, 04:26, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
>> En Sun, 20 Apr 2008 20:24:04 -0300, globalrev <skanem... at yahoo.se> escribió:
>> > i dont get the mainloop() in python. i mean i have written some
>> > programs, for example a calculator using tkinterGUI.
>> What you call the "mainloop" is the event loop (or message loop) used by event-driven applications as a way to dispatch all events as they happen in the system. The concept is independent of Python/C++/whatever language you choose. The key phrase is "event-driven programming":http://en.wikipedia.org/wiki/Event_driven_programming
>> Tkinter provides an event-driven GUI framework. Simple CLI programs are not event-driven, as your pseudo example above.
>> > if i have some functions i wanna call to run the program and i wanna
>> > call them ina specific order and be able to call
>> > them from each other should this just be called in the mainloop and
>> > the mianloop then runs the "mainscript" top
>> > to bottom over and over?
>> If you don't want or don't require a graphical user interface, just write the functions you need and call them from the outermost section in your script.
>> If you do require a GUI, you'll have to write the code in response to user actions: when the user clicks here, do this; when the user chooses that menu option, do that.
> but what i mean i dont understand is sure i can bind a function to a
> buttonpress but if i have a def dox(): dododo
> and i call it with dox() in the mainscript it will be executed once,
> but not again.
> so what does the mainloop do, 1srt time it is executed it runs the
> mainscript then it it sits and wait s for commands?
What's the "mainscript"?
The "mainloop" just waits for system events like: mouse movement, mouse click, key pressed, key released, time elapsed, window is uncovered, etc. And when any event arrives, it's dispatched to its registered handler.
Python executes whatever you wrote at the top level of the script you are running, up to the root.mainloop() line. That function doesn't return until the main window is closed; all the user interaction occurs inside that call. Finally, execution resumes on the next line, if any.
I hope this answers your question; if not, try to be more specific. Maybe an example of what you want to do.
More information about the Python-list