Tkinter event loop question

G

gordon

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
 
F

Fredrik Lundh

gordon said:
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>
 
G

gordon

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
 
G

gordon

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top