| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| 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> |
|
#3
| |||
| |||
| In article <mailman.93.1219859138.3487.python-list@python.org>, Fredrik Lundh <fredrik@pythonware.com> wrote: >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 |
|
#4
| |||
| |||
| On Aug 27, 10:42 pm, Fredrik Lundh <fred...@pythonware.com> 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 |
|
#5
| |||
| |||
| In article <c180cdc1-66b3-45b5-9ece-e606755d8e81@r15g2000prd.googlegroups.com>, gordon <nodrogbrown@gmail.com> wrote: > On Aug 27, 10:42 pm, Fredrik Lundh <fred...@pythonware.com> 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? Normally MVC applies within one application with one event loop. So you set up your GUI and then start the event loop. The GUI will process user events (e.g. typing text, pressing buttons, selecting menus) as callbacks that do things like create Controller objects and execute methods on them. So for starters your Controller object should not create root nor should it call mainloop to start the event loop. Those two actions should be done once by the main script that launches your application. As to where to go from here...it would help to know more about what you are trying to do. -- Russell |
|
#6
| |||
| |||
| On Aug 29, 4:45 am, "Russell E. Owen" <ro...@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 |
|
#7
| |||
| |||
| In article <759a12fe-75c8-429a-9a64-19dc73a0b779@b30g2000prf.googlegroups.com>, gordon <nodrogbrown@gmail.com> wrote: > On Aug 29, 4:45 am, "Russell E. Owen" <ro...@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 |
|
#8
| |||
| |||
| On Aug 29, 10:46 pm, "Russell E. Owen" <ro...@u.washington.edu> 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 |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.