Tkinter event loop question

gordon nodrogbrown at
Fri Aug 29 19:18:08 CEST 2008

On Aug 29, 4:45 am, "Russell E. Owen" <ro... at> wrote:
>your Controller object should not create root nor should
> it call mainloop to start the event loop.

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
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
import moduleB
from Tkinter import Tk
class MyController(object):
    def __init__(self):
        print "controller()"
    def validateSelection(self,userinputVal):
        if(userinputVal > 50 or userinputVal==0):

    def doSomeCalculation(self,userinputVal):
        resultvalue=2*userinputVal +10
    def updateResults(self,resultvalue):

if __name__ == "__main__":
    root = Tk()
    controller.myapp =moduleB.MyUI(root,controller)
from Tkinter import *
class MyUI(object):
    def __init__(self, parent,controller):
        self.myParent = parent
        self.mainframe = Frame(parent,background="grey")
        ....added a canvas and OK,QUIT buttons

    def okBtnClick(self):
        print "okBtnClicked"
        userinputvalue = self.getUserInputValue()
        self.myParent.quit()  #quits event loop

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


More information about the Python-list mailing list