Tkinter event loop question

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


In article 
<759a12fe-75c8-429a-9a64-19dc73a0b779 at b30g2000prf.googlegroups.com>,
 gordon <nodrogbrown at gmail.com> wrote:

> On Aug 29, 4:45 am, "Russell E. Owen" <ro... at u.washington.edu> 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

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



More information about the Python-list mailing list