473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Tkinter event loop question

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
Aug 27 '08 #1
7 3477
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>

Aug 27 '08 #2
In article <ma************************************@python.org >,
Fredrik Lundh <fr*****@pythonware.comwrote:
>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
Aug 27 '08 #3
On Aug 27, 10:42 pm, Fredrik Lundh <fred...@pythonware.comwrote:
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
Aug 28 '08 #4
In article
<c1**********************************@r15g2000prd. googlegroups.com>,
gordon <no*********@gmail.comwrote:
On Aug 27, 10:42 pm, Fredrik Lundh <fred...@pythonware.comwrote:
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
Aug 28 '08 #5
On Aug 29, 4:45 am, "Russell E. Owen" <ro...@u.washington.eduwrote:
>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
Aug 29 '08 #6
In article
<75**********************************@b30g2000prf. googlegroups.com>,
gordon <no*********@gmail.comwrote:
On Aug 29, 4:45 am, "Russell E. Owen" <ro...@u.washington.eduwrote:
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
Aug 29 '08 #7
On Aug 29, 10:46 pm, "Russell E. Owen" <ro...@u.washington.eduwrote:
you
can safely compute stuff with a background thread and display it fromthe main thread). But cross that bridge later.>
-- Russell
thanks Russel
gordon
Aug 30 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Russell E. Owen | last post by:
I have a Python/Tkinter remote control application and want to add support for running user scripts. I would like the scripts to be written in python, would like them to be able to update...
2
by: k2riddim | last post by:
Hello, I'm a beginner with Python and Tkinter development. My application parse links in an html file. And I use Tkinter to implement a GUI. This GUI has a button to launch the parse treatment,...
2
by: Grooooops | last post by:
I've been hacking around this for a few days and have gotten close to what I want... but not quite... The TKinter Docs provide this example: # configure text tag text.tag_config("a",...
9
by: Tuvas | last post by:
I am building a tkinter program. A part of this program is to read data from an incoming interface, and depending on the data, will display a bit of text on the tk dialog, it decodes this data, so...
5
by: linuxnooby | last post by:
Hi I have a tkinter question. In the following script the window will not display until the script has finished executing. It appears at the same time as the output "script finished". How can I...
1
by: apriebe47 | last post by:
Hello, I have created a simple canvas in Tkinter to display a number of PhotoImages, I then bind a key (in my case, <Up>) to start a loop that plays through a list of PhotoImages to make it an...
2
by: Kevin Walzer | last post by:
I'm trying to decide whether I need threads in my Tkinter application or not. My app is a front end to a command-line tool; it feeds commands to the command-line program, then reads its output and...
1
by: vigacmoe | last post by:
Hi all, I'm trying to write a simple tkinter program, then this problem popped up. The followin code will describe the problem. ------------------------------------------ import Tkinter ...
2
by: Kevin Walzer | last post by:
I'm porting a Tkinter application to wxPython and had a question about wxPython's event loop. The Tkinter app provides a GUI to a command-line tool. It gathers user input, and opens an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.