Tkinter event loop question

Russell E. Owen rowen at u.washington.edu
Fri Aug 29 01:45:23 CEST 2008


In article 
<c180cdc1-66b3-45b5-9ece-e606755d8e81 at r15g2000prd.googlegroups.com>,
 gordon <nodrogbrown at gmail.com> wrote:

> On Aug 27, 10:42 pm, Fredrik Lundh <fred... at pythonware.com> 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?

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



More information about the Python-list mailing list