Tkinter event loop question

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


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

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



More information about the Python-list mailing list