question about the mainloop

G

globalrev

in C?? java etc there is usually:

procedure 1
procedure 2
procedure 3

main {
procedure 1
procedure 2
procedure 3
}

i dont get the mainloop() in python. i mean i have written some
programs, for example a calculator using tkinterGUI.

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?
 
G

Gabriel Genellina

in C?? java etc there is usually:

procedure 1
procedure 2
procedure 3

main {
procedure 1
procedure 2
procedure 3
}

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.
 
G

globalrev

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 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?
 
G

Gabriel Genellina

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.
 
B

Bruno Desthuilliers

globalrev a écrit :
in C?? java etc there is usually:

procedure 1
procedure 2
procedure 3

main {
procedure 1
procedure 2
procedure 3
}

i dont get the mainloop() in python.

The 'main' function (resp. method) in C and Java has nothing to do with
a "mainloop" - it's just the program entry point, IOW the function that
will get called when the program is executed.

Python has no such thing, since all code at the top-level is executed
when the script is passed to the python runtime.

The notion of "mainloop" (or event loop) is specific to event-driven
programs, which, once everything is set up, enter in a loop, wait for
events to come, and dispatch them to appropriate handlers. And FWIW,
there's no loop in your above example - the main() function will call
procedures 1, 2 and 3 sequentially then exit.
i mean i have written some
programs, for example a calculator using tkinterGUI.

if i have some functions i wanna call to run the program

You don't "call some functions" to "run the program" - you pass your
script to the python runtime, and all top-level code will be executed
sequentially.
 
L

Lie

in C?? java etc there is usually:

procedure 1
procedure 2
procedure 3

main {
procedure 1
procedure 2
procedure 3

}

i dont get the mainloop() in python. i mean i have written some
programs, for example a calculator using tkinterGUI.

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?

In Python+Tkinter (and I believe, all event-driven programming model),
the codes you write are all used for setting up the GUI, specifying
where they're placed, what happens when they're clicked, etc, etc,
etc. And entering the main loop means the event "manager" (a.k.a
mainloop) would start checking all the registered events and respond
to the event by calling the associated function you determined at the
setup. This event "manager" loops itself over and over until it
received an event that tells them to stop listening and quit the
program (usually binded with the close button).

In some event-driven programming model (like in Visual Basic, don't
know about Java, never used it), the setup phase is hidden from you
and is done automagically by the GUI designer, and when the setup
phase finishes (the mainloop is called), the first event that happens
is a programhasjuststarted event, which is automagically bound with a
startup function, in VB: the Main Form's OnLoad Event or a Main
function.
 
G

globalrev

En Mon, 21 Apr 2008 00:57:38 -0300, globalrev <[email protected]> escribió:





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.


ty everyone i get it now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top