Tkinter event loop question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gordon

    Tkinter event loop question

    is it possible to send a message to the gui instance while the Tk
    event loop is running?I mean after i create a gui object like

    root=Tk()
    mygui=SomeUI(ro ot)

    and call
    root.mainloop()

    can i send message to mygui without quitting the ui or closing the
    window?i tried some code like
    mygui.someMetho d()
    but it only gets executed after i close the the ui window.Is there a
    way to get this message passing while gui is running ?

    (forgive me ,it is a repeat question..but i am a bit desperate)
    thanks
    gordon
  • Fredrik Lundh

    #2
    Re: Tkinter event loop question

    gordon wrote:
    is it possible to send a message to the gui instance while the Tk
    event loop is running?I mean after i create a gui object like
    >
    root=Tk()
    mygui=SomeUI(ro ot)
    >
    and call
    root.mainloop()
    >
    can i send message to mygui without quitting the ui or closing the
    window?i tried some code like
    mygui.someMetho d()
    but it only gets executed after i close the the ui window.Is there a
    way to get this message passing while gui is running ?
    it's the event loop that keeps Tkinter running, and Tkinter then calls
    your program (typically via command callbacks or event handlers) when
    it's time to do something.

    so I guess the question here is from where you expect to call that
    method, and what you expect Tkinter to do when you call it...

    </F>

    Comment

    • Cameron Laird

      #3
      Re: Tkinter event loop question

      In article <mailman.93.121 9859138.3487.py thon-list@python.org >,
      Fredrik Lundh <fredrik@python ware.comwrote:
      >gordon wrote:
      >
      >is it possible to send a message to the gui instance while the Tk
      >event loop is running?I mean after i create a gui object like

      Comment

      • gordon

        #4
        Re: Tkinter event loop question

        On Aug 27, 10:42 pm, Fredrik Lundh <fred...@python ware.comwrote:
        so I guess the question here is from where you expect to call that
        method, and what you expect Tkinter to do when you call it...
        thanks for the reply

        i was planning to write a controller (as in MVC) that will instantiate
        a gui class and show the ui.the gui will send user input back to the
        controller which in turn will process it and send a result to the gui
        to be displayed

        something like

        controllermodul e.py
        --------------------
        class Controller:
        def createGUI(self) :
        root=Tk()
        self.mygui=uimo dule.MyGUI(root )
        root.mainloop()
        def sendMessageToUI (self):
        self.mygui.disp layResult(somer esultValue)



        i don't know if this is the right way to do this..when i call
        sendMessage() it tries to call displayResult() on the gui instance
        which is already in an event loop.displayRes ult() gets called only
        after the event loop is finished(when i close gui window).

        is there a way to keep the gui window open and have the controller
        send a message to the gui instance so that i can pass the processed
        result value to the gui instance?

        thanks
        gordon


        Comment

        • Russell E. Owen

          #5
          Re: Tkinter event loop question

          In article
          <c180cdc1-66b3-45b5-9ece-e606755d8e81@r1 5g2000prd.googl egroups.com>,
          gordon <nodrogbrown@gm ail.comwrote:
          On Aug 27, 10:42 pm, Fredrik Lundh <fred...@python ware.comwrote:
          so I guess the question here is from where you expect to call that
          method, and what you expect Tkinter to do when you call it...
          >
          thanks for the reply
          >
          i was planning to write a controller (as in MVC) that will instantiate
          a gui class and show the ui.the gui will send user input back to the
          controller which in turn will process it and send a result to the gui
          to be displayed
          >
          something like
          >
          controllermodul e.py
          --------------------
          class Controller:
          def createGUI(self) :
          root=Tk()
          self.mygui=uimo dule.MyGUI(root )
          root.mainloop()
          def sendMessageToUI (self):
          self.mygui.disp layResult(somer esultValue)
          >
          >
          >
          i don't know if this is the right way to do this..when i call
          sendMessage() it tries to call displayResult() on the gui instance
          which is already in an event loop.displayRes ult() gets called only
          after the event loop is finished(when i close gui window).
          >
          is there a way to keep the gui window open and have the controller
          send a message to the gui instance so that i can pass the processed
          result value to the gui instance?
          Normally MVC applies within one application with one event loop. So you
          set up your GUI and then start the event loop. The GUI will process user
          events (e.g. typing text, pressing buttons, selecting menus) as
          callbacks that do things like create Controller objects and execute
          methods on them.

          So for starters your Controller object should not create root nor should
          it call mainloop to start the event loop. Those two actions should be
          done once by the main script that launches your application.

          As to where to go from here...it would help to know more about what you
          are trying to do.

          -- Russell

          Comment

          • gordon

            #6
            Re: Tkinter event loop question

            On Aug 29, 4:45 am, "Russell E. Owen" <ro...@u.washin gton.eduwrote:
            >your Controller object should not create root nor should
            it call mainloop to start the event loop.
            guys
            thanks for the helpful replies..I rewrote the code as you advised. It
            creates a controller object and a gui object from main script.However
            i had to chain some method calls in my code.i am
            wondering if that can be avoided in some way.

            this is the sequence
            1.user enters some value in the textfield,and clicks OKbutton
            2.on OKbuttonclick ,the gui takes userinput value and quits
            eventloop.It then calls controller and pass the userinputvalue.
            3.controller does some calculation(i shd have an external program to
            do calculation,but for simplicity i just wrote a method inside
            controller) with the given value,obtains the result and calls gui's
            updateDisplay() , passing the result value.
            4.the gui creates the result as text on a canvas.then the mainloop()
            is called to resume event loop

            again user enters some value in the textfield,and clicks OKbutton ...

            I exit the application by clicking quitButton that calls destroy() on
            top level window.
            I tried my application by entering some value on text field and then
            clicking OKbutton ,the calculated result is displayed on canvas .I do
            this process say 3 times ..Then i click the Quit button and the window
            closes.
            I have put a print statement inside the gui's updateDisplay() method
            right after the resuming of event loop by parent.mainloop ().This print
            statement gets printed as many times as i had clicked
            the OK button(here in this case 3 times).Is this due to clearing of
            the stack ?
            I feel that it was not a good design ..I would be grateful if someone
            would tell me how i could improve it..can those chaining of method
            calls be avoided?

            this is how i restructured the code

            moduleA.py
            -----------
            import moduleB
            from Tkinter import Tk
            class MyController(ob ject):
            def __init__(self):
            print "controller ()"
            def validateSelecti on(self,userinp utVal):
            if(userinputVal 50 or userinputVal==0 ):
            userinputVal=50
            self.doSomeCalc ulation(userinp utVal)

            def doSomeCalculati on(self,userinp utVal):
            resultvalue=2*u serinputVal +10
            self.updateResu lts(resultvalue )
            def updateResults(s elf,resultvalue ):
            self.myapp.upda teDisplay(resul tvalue);

            if __name__ == "__main__":
            controller=MyCo ntroller()
            root = Tk()
            root.wm_title(" MyUIApp")
            controller.myap p =moduleB.MyUI(r oot,controller)
            root.mainloop()

            moduleB.py
            -----------
            from Tkinter import *
            class MyUI(object):
            def __init__(self, parent,controll er):
            self.myParent = parent
            self.mainframe = Frame(parent,ba ckground="grey" )
            self.mainframe. pack(fill=BOTH, expand=YES)
            self.controller =controller
            ....added a canvas and OK,QUIT buttons

            def okBtnClick(self ):
            print "okBtnClick ed"
            self.okButton.c onfigure(state= DISABLED)
            userinputvalue = self.getUserInp utValue()
            self.myParent.q uit() #quits event loop
            self.controller .validateSelect ion(userinputva lue)

            def quitBtnClick(se lf):
            print "Quit Btn clicked"
            self.myParent.d estroy()
            def updateDisplay(s elf,resultValue ):
            message='result is='+str(result Value)
            self.canvresult .delete(ALL)
            self.canvresult .create_text(1, 40, anchor=W, text=message,
            width=280)
            self.okButton.c onfigure(state= NORMAL)
            self.myParent.m ainloop() # resumes event loop
            print 'in updateDisplay() :called mainloop'

            thanks
            gordon

            Comment

            • Russell E. Owen

              #7
              Re: Tkinter event loop question

              In article
              <759a12fe-75c8-429a-9a64-19dc73a0b779@b3 0g2000prf.googl egroups.com>,
              gordon <nodrogbrown@gm ail.comwrote:
              On Aug 29, 4:45 am, "Russell E. Owen" <ro...@u.washin gton.eduwrote:
              your Controller object should not create root nor should
              it call mainloop to start the event loop.
              >
              guys
              thanks for the helpful replies..I rewrote the code as you advised. It
              creates a controller object and a gui object from main script.However
              i had to chain some method calls in my code.i am
              wondering if that can be avoided in some way.
              >
              this is the sequence
              1.user enters some value in the textfield,and clicks OKbutton
              2.on OKbuttonclick ,the gui takes userinput value and quits
              eventloop.It then calls controller and pass the userinputvalue.
              3.controller does some calculation(i shd have an external program to
              do calculation,but for simplicity i just wrote a method inside
              controller) with the given value,obtains the result and calls gui's
              updateDisplay() , passing the result value.
              4.the gui creates the result as text on a canvas.then the mainloop()
              is called to resume event loop
              >
              again user enters some value in the textfield,and clicks OKbutton ...
              >
              I exit the application by clicking quitButton that calls destroy() on
              top level window.
              I tried my application by entering some value on text field and then
              clicking OKbutton ,the calculated result is displayed on canvas .I do
              this process say 3 times ..Then i click the Quit button and the window
              closes.
              I have put a print statement inside the gui's updateDisplay() method
              right after the resuming of event loop by parent.mainloop ().This print
              statement gets printed as many times as i had clicked
              the OK button(here in this case 3 times).Is this due to clearing of
              the stack ?
              I feel that it was not a good design ..I would be grateful if someone
              would tell me how i could improve it..can those chaining of method
              calls be avoided?
              >
              this is how i restructured the code
              Why do you quite the event loop? Just leave it running.

              On pressing the button you validate the input, perform the computation
              and display the result, all without leaving the event loop.

              If your computation is very slow then you have other issues to deal with
              (in particular background threads cannot safely talk to Tkinter, but you
              can safely compute stuff with a background thread and display it from
              the main thread). But cross that bridge later.

              -- Russell

              Comment

              • gordon

                #8
                Re: Tkinter event loop question

                On Aug 29, 10:46 pm, "Russell E. Owen" <ro...@u.washin gton.eduwrote:
                you
                can safely compute stuff with a background thread and display it fromthe main thread). But cross that bridge later.>
                -- Russell
                thanks Russel
                gordon

                Comment

                Working...