Tkinter event loop question

Discussion in 'Python' started by gordon, Aug 27, 2008.

  1. gordon

    gordon Guest

    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(root)

    and call
    root.mainloop()

    can i send message to mygui without quitting the ui or closing the
    window?i tried some code like
    mygui.someMethod()
    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
     
    gordon, Aug 27, 2008
    #1
    1. Advertising

  2. 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(root)
    >
    > and call
    > root.mainloop()
    >
    > can i send message to mygui without quitting the ui or closing the
    > window?i tried some code like
    > mygui.someMethod()
    > 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>
     
    Fredrik Lundh, Aug 27, 2008
    #2
    1. Advertising

  3. gordon

    gordon Guest

    On Aug 27, 10:42 pm, Fredrik Lundh <> wrote:
    > 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

    controllermodule.py
    --------------------
    class Controller:
    def createGUI(self):
    root=Tk()
    self.mygui=uimodule.MyGUI(root)
    root.mainloop()
    def sendMessageToUI(self):
    self.mygui.displayResult(someresultValue)



    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.displayResult() 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
     
    gordon, Aug 28, 2008
    #3
  4. gordon

    gordon Guest

    On Aug 29, 4:45 am, "Russell E. Owen" <> wrote:
    >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(object):
    def __init__(self):
    print "controller()"
    def validateSelection(self,userinputVal):
    if(userinputVal > 50 or userinputVal==0):
    userinputVal=50
    self.doSomeCalculation(userinputVal)

    def doSomeCalculation(self,userinputVal):
    resultvalue=2*userinputVal +10
    self.updateResults(resultvalue)
    def updateResults(self,resultvalue):
    self.myapp.updateDisplay(resultvalue);

    if __name__ == "__main__":
    controller=MyController()
    root = Tk()
    root.wm_title("MyUIApp")
    controller.myapp =moduleB.MyUI(root,controller)
    root.mainloop()

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

    def okBtnClick(self):
    print "okBtnClicked"
    self.okButton.configure(state=DISABLED)
    userinputvalue = self.getUserInputValue()
    self.myParent.quit() #quits event loop
    self.controller.validateSelection(userinputvalue)

    def quitBtnClick(self):
    print "Quit Btn clicked"
    self.myParent.destroy()
    def updateDisplay(self,resultValue):
    message='result is='+str(resultValue)
    self.canvresult.delete(ALL)
    self.canvresult.create_text(1, 40, anchor=W, text=message,
    width=280)
    self.okButton.configure(state=NORMAL)
    self.myParent.mainloop() # resumes event loop
    print 'in updateDisplay():called mainloop'

    thanks
    gordon
     
    gordon, Aug 29, 2008
    #4
  5. gordon

    gordon Guest

    On Aug 29, 10:46 pm, "Russell E. Owen" <> wrote:
    you
    > can safely compute stuff with a background thread and display it from> the main thread). But cross that bridge later.>
    > -- Russell


    thanks Russel
    gordon
     
    gordon, Aug 30, 2008
    #5
    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. wang xiaoyu
    Replies:
    0
    Views:
    864
    wang xiaoyu
    Jun 25, 2003
  2. Exod

    Tkinter, main loop question.

    Exod, Nov 23, 2006, in forum: Python
    Replies:
    2
    Views:
    470
    Bjoern Schliessmann
    Nov 23, 2006
  3. Kevin Walzer

    Use threads or Tkinter event loop?

    Kevin Walzer, Mar 27, 2007, in forum: Python
    Replies:
    2
    Views:
    542
  4. Anthony Papillion

    TkInter bind() event is not firing event trigger

    Anthony Papillion, Jun 23, 2010, in forum: Python
    Replies:
    3
    Views:
    1,693
    rantingrick
    Jun 23, 2010
  5. Isaac Won
    Replies:
    9
    Views:
    383
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page