Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > question about the mainloop

Reply
Thread Tools

question about the mainloop

 
 
globalrev
Guest
Posts: n/a
 
      04-20-2008
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?
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      04-21-2008
En Sun, 20 Apr 2008 20:24:04 -0300, globalrev <(E-Mail Removed)> escribió:

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

--
Gabriel Genellina

 
Reply With Quote
 
 
 
 
globalrev
Guest
Posts: n/a
 
      04-21-2008
On 21 Apr, 04:26, "Gabriel Genellina" <(E-Mail Removed)> wrote:
> En Sun, 20 Apr 2008 20:24:04 -0300, globalrev <(E-Mail Removed)> escribió:
>
> > 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.
>
> --
> 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?
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      04-21-2008
En Mon, 21 Apr 2008 00:57:38 -0300, globalrev <(E-Mail Removed)> escribió:
> On 21 Apr, 04:26, "Gabriel Genellina" <(E-Mail Removed)> wrote:
>> En Sun, 20 Apr 2008 20:24:04 -0300, globalrev <(E-Mail Removed)> 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.

--
Gabriel Genellina

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      04-21-2008
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.

 
Reply With Quote
 
Lie
Guest
Posts: n/a
 
      04-22-2008
On Apr 21, 6:24 am, globalrev <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
globalrev
Guest
Posts: n/a
 
      04-22-2008
On 21 Apr, 07:31, "Gabriel Genellina" <(E-Mail Removed)> wrote:
> En Mon, 21 Apr 2008 00:57:38 -0300, globalrev <(E-Mail Removed)> escribió:
>
>
>
> > On 21 Apr, 04:26, "Gabriel Genellina" <(E-Mail Removed)> wrote:
> >> En Sun, 20 Apr 2008 20:24:04 -0300, globalrev <(E-Mail Removed)> 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.
>
> --
> Gabriel Genellina



ty everyone i get it now.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
wxPython MainLoop exception handling problem Kreedz Python 2 09-12-2005 06:56 PM
detect tk mainloop John Hunter Python 0 01-26-2005 03:32 PM
Problem with tkinter mainloop k2riddim Python 2 11-30-2004 02:30 PM
Re: Tk mainloop() dan Python 0 07-20-2003 02:32 PM
Re: Tk mainloop() John Roth Python 0 07-20-2003 11:06 AM



Advertisments