Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Tkinter event loop question

Reply
Thread Tools

Tkinter event loop question

 
 
gordon
Guest
Posts: n/a
 
      08-27-2008
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
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      08-27-2008
gordon wrote:

> 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>

 
Reply With Quote
 
 
 
 
gordon
Guest
Posts: n/a
 
      08-28-2008
On Aug 27, 10:42 pm, Fredrik Lundh <(E-Mail Removed)> 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?

thanks
gordon


 
Reply With Quote
 
gordon
Guest
Posts: n/a
 
      08-29-2008
On Aug 29, 4:45 am, "Russell E. Owen" <(E-Mail Removed)> 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
 
Reply With Quote
 
gordon
Guest
Posts: n/a
 
      08-30-2008
On Aug 29, 10:46 pm, "Russell E. Owen" <(E-Mail Removed)> wrote:
you
> can safely compute stuff with a background thread and display it from> the main thread). But cross that bridge later.>
> -- Russell


thanks Russel
gordon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
TkInter bind() event is not firing event trigger Anthony Papillion Python 3 06-23-2010 12:55 AM
Use threads or Tkinter event loop? Kevin Walzer Python 2 03-27-2007 02:26 PM
Tkinter, main loop question. Exod Python 2 11-23-2006 01:59 PM
Only One Tkinter Entry can receive key event event in Linux RH8.0 wang xiaoyu Python 0 06-25-2003 04:44 AM



Advertisments