question about the mainloop

Discussion in 'Python' started by globalrev, Apr 21, 2008.

  1. globalrev

    globalrev Guest

    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?
     
    globalrev, Apr 21, 2008
    #1
    1. Advertising

  2. En Sun, 20 Apr 2008 20:24:04 -0300, globalrev <> 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
     
    Gabriel Genellina, Apr 21, 2008
    #2
    1. Advertising

  3. globalrev

    globalrev Guest

    On 21 Apr, 04:26, "Gabriel Genellina" <> wrote:
    > En Sun, 20 Apr 2008 20:24:04 -0300, globalrev <> 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?
     
    globalrev, Apr 21, 2008
    #3
  4. En Mon, 21 Apr 2008 00:57:38 -0300, globalrev <> escribió:
    > On 21 Apr, 04:26, "Gabriel Genellina" <> wrote:
    >> En Sun, 20 Apr 2008 20:24:04 -0300, globalrev <> 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
     
    Gabriel Genellina, Apr 21, 2008
    #4
  5. 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.
     
    Bruno Desthuilliers, Apr 21, 2008
    #5
  6. globalrev

    Lie Guest

    On Apr 21, 6:24 am, globalrev <> 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.
     
    Lie, Apr 22, 2008
    #6
  7. globalrev

    globalrev Guest

    On 21 Apr, 07:31, "Gabriel Genellina" <> wrote:
    > En Mon, 21 Apr 2008 00:57:38 -0300, globalrev <> escribió:
    >
    >
    >
    > > On 21 Apr, 04:26, "Gabriel Genellina" <> wrote:
    > >> En Sun, 20 Apr 2008 20:24:04 -0300, globalrev <> 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.
     
    globalrev, Apr 22, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Roth

    Re: Tk mainloop()

    John Roth, Jul 20, 2003, in forum: Python
    Replies:
    0
    Views:
    405
    John Roth
    Jul 20, 2003
  2. dan

    Re: Tk mainloop()

    dan, Jul 20, 2003, in forum: Python
    Replies:
    0
    Views:
    482
  3. k2riddim

    Problem with tkinter mainloop

    k2riddim, Nov 30, 2004, in forum: Python
    Replies:
    2
    Views:
    1,454
    k2riddim
    Nov 30, 2004
  4. John Hunter

    detect tk mainloop

    John Hunter, Jan 26, 2005, in forum: Python
    Replies:
    0
    Views:
    340
    John Hunter
    Jan 26, 2005
  5. Kreedz
    Replies:
    2
    Views:
    484
    Kreedz
    Sep 12, 2005
Loading...

Share This Page