Problem with tkinter mainloop

Discussion in 'Python' started by k2riddim, Nov 30, 2004.

  1. k2riddim

    k2riddim Guest

    Hello,
    I'm a beginner with Python and Tkinter development.
    My application parse links in an html file. And I use Tkinter to
    implement a GUI. This GUI has a button to launch the parse treatment,
    and a status bar to show the state of the treatment.
    I know that because of the mainloop, my tkinter application freeze
    while my treatment isn't finished. That's why my status bar doesn't
    update herself in real time.
    I wanted to use the after or the after_idle function, but I don't
    really understand why it doesn't work.

    My apps is build approximately like that :

    ---Gui.py---
    class Gui(Frame):
    def __init__(self):
    ...
    def launchTreatment(self):
    b = Treatment()
    self.after(b.treatment.parse)

    ---Treatment.py---
    class Treatment():
    def __init__(self):
    ...
    def parse(self):
    ...
    GUIinstance.status.set("state 1")
    ...
    GUIinstance.status.set("state 2")


    ---Main.py---
    #instanciation of classes
    GUIinstance = Gui
    GUIinstance.mainloop()

    Any help will be very appreciated, excuse my english, this is not my
    native language as you can see.
     
    k2riddim, Nov 30, 2004
    #1
    1. Advertising

  2. k2riddim

    Eric Brunel Guest

    k2riddim wrote:
    > Hello,
    > I'm a beginner with Python and Tkinter development.
    > My application parse links in an html file. And I use Tkinter to
    > implement a GUI. This GUI has a button to launch the parse treatment,
    > and a status bar to show the state of the treatment.
    > I know that because of the mainloop, my tkinter application freeze
    > while my treatment isn't finished. That's why my status bar doesn't
    > update herself in real time.
    > I wanted to use the after or the after_idle function, but I don't
    > really understand why it doesn't work.


    after and after_idle won't help you: the action you register in these methods
    are called only when the main loop gets back the control, and your problem is
    precisely that the main loop does not get it...

    The method you're probably looking for is update_idletasks: it updates the
    display without getting any user event. There's another method called update,
    but this one does process user events, so it may call some of your code if such
    an event is pending. This is usually not what you want to do, except in some
    very rare cases.

    Here is how it may look like in your code, along with a few remarks on your code:

    >
    > My apps is build approximately like that :
    >
    > ---Gui.py---
    > class Gui(Frame):


    Why do you inherit from Frame? A Tkinter Frame is a generic container widget;
    this is not a window. IMHO, you'd better inherit from Tk for your main window or
    from Toplevel for other windows. You'd also get better control on the actual
    window, since some of the methods available on Tk and Toplevel are not available
    on other widgets (e.g. geometry or protocol)

    > def __init__(self):
    > ...
    > def launchTreatment(self):
    > b = Treatment()
    > self.after(b.treatment.parse)


    This cannot be your code; the after methods takes two parameters: the number of
    milliseconds to wait before the action will be called and the action itself. You
    only provide the action here. But again, the after method won't help you to get
    what you want...

    >
    > ---Treatment.py---
    > class Treatment():
    > def __init__(self):
    > ...
    > def parse(self):
    > ...
    > GUIinstance.status.set("state 1")


    This is where the GUIinstance.update_idletasks() should go.

    > ...
    > GUIinstance.status.set("state 2")


    Another GUIinstance.update_idletasks() here.

    >
    >
    > ---Main.py---
    > #instanciation of classes
    > GUIinstance = Gui


    Again, this cannot be your code, since you do not instantiate the class here.
    The correct line should be:

    GUIinstance = Gui()

    It is usually far better to post a working example demonstrating the problem you
    have instead of just extracting a few lines of your whole code. This will help
    people who are willing to help to understand exactly what is going on.

    HTH
    --
    - Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
    PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
     
    Eric Brunel, Nov 30, 2004
    #2
    1. Advertising

  3. k2riddim

    k2riddim Guest

    Many thanks, You're right this wasn't my code, this was just what I
    can remember, because my application is at home, and I'm at work.
    Be sure I'll follow your advices concerning posting rules in this
    group !

    Eric Brunel <> wrote in message news:<41ac53ac$0$30438$>...
    > k2riddim wrote:
    > > Hello,
    > > I'm a beginner with Python and Tkinter development.
    > > My application parse links in an html file. And I use Tkinter to
    > > implement a GUI. This GUI has a button to launch the parse treatment,
    > > and a status bar to show the state of the treatment.
    > > I know that because of the mainloop, my tkinter application freeze
    > > while my treatment isn't finished. That's why my status bar doesn't
    > > update herself in real time.
    > > I wanted to use the after or the after_idle function, but I don't
    > > really understand why it doesn't work.

    >
    > after and after_idle won't help you: the action you register in these methods
    > are called only when the main loop gets back the control, and your problem is
    > precisely that the main loop does not get it...
    >
    > The method you're probably looking for is update_idletasks: it updates the
    > display without getting any user event. There's another method called update,
    > but this one does process user events, so it may call some of your code if such
    > an event is pending. This is usually not what you want to do, except in some
    > very rare cases.
    >
    > Here is how it may look like in your code, along with a few remarks on your code:
    >
    > >
    > > My apps is build approximately like that :
    > >
    > > ---Gui.py---
    > > class Gui(Frame):

    >
    > Why do you inherit from Frame? A Tkinter Frame is a generic container widget;
    > this is not a window. IMHO, you'd better inherit from Tk for your main window or
    > from Toplevel for other windows. You'd also get better control on the actual
    > window, since some of the methods available on Tk and Toplevel are not available
    > on other widgets (e.g. geometry or protocol)
    >
    > > def __init__(self):
    > > ...
    > > def launchTreatment(self):
    > > b = Treatment()
    > > self.after(b.treatment.parse)

    >
    > This cannot be your code; the after methods takes two parameters: the number of
    > milliseconds to wait before the action will be called and the action itself. You
    > only provide the action here. But again, the after method won't help you to get
    > what you want...
    >
    > >
    > > ---Treatment.py---
    > > class Treatment():
    > > def __init__(self):
    > > ...
    > > def parse(self):
    > > ...
    > > GUIinstance.status.set("state 1")

    >
    > This is where the GUIinstance.update_idletasks() should go.
    >
    > > ...
    > > GUIinstance.status.set("state 2")

    >
    > Another GUIinstance.update_idletasks() here.
    >
    > >
    > >
    > > ---Main.py---
    > > #instanciation of classes
    > > GUIinstance = Gui

    >
    > Again, this cannot be your code, since you do not instantiate the class here.
    > The correct line should be:
    >
    > GUIinstance = Gui()
    >
    > It is usually far better to post a working example demonstrating the problem you
    > have instead of just extracting a few lines of your whole code. This will help
    > people who are willing to help to understand exactly what is going on.
    >
    > HTH
     
    k2riddim, Nov 30, 2004
    #3
    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:
    403
    John Roth
    Jul 20, 2003
  2. Kreedz
    Replies:
    2
    Views:
    484
    Kreedz
    Sep 12, 2005
  3. Replies:
    0
    Views:
    493
  4. Peter Otten
    Replies:
    2
    Views:
    784
    John McMonagle
    Mar 15, 2009
  5. Frederic Rentsch

    Tkinter: IDLE can't get out of mainloop

    Frederic Rentsch, Mar 31, 2012, in forum: Python
    Replies:
    0
    Views:
    304
    Frederic Rentsch
    Mar 31, 2012
Loading...

Share This Page