473,508 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multithreading tkinter question

Is there a safe way to run tkinter in a multithreaded app where the
mainloop runs in a background thread ?
Here's some test code demonstrating the problem. I'm running Python2.4
under Windows 2000.
----------------Code snip starts-------------
from Tkinter import *

def GetTkinterThread():
import threading
def TestTkinter():
def greeting():
print "Hello stdout world !"

win = Frame()
win.pack()
Label(win, text="Hello container world").pack(side=TOP)
Button(win, text="Hello", command=greeting).pack(side=TOP)
Button(win, text="Quit", command=win.quit).pack(side=RIGHT)

win.mainloop()
#Run the mainloop in another thread
t = threading.Thread(None, TestTkinter, 'Test Tkinter Thread')
t.setDaemon(1) #Keep going
t.start()
print 'Hi'
return t

t = GetTkinterThread() #This works fine

#Now press the "Hello" button on the Tkinter window
#Now press the return key at the python prompt

----------------Code snip ends-------------
With a debug build the call stack looks like this:
python24_d.dll!Py_FatalError(const char * msg=0x1e23ca14) Line
1513 C
python24_d.dll!PyThreadState_Swap(_ts * new=0x0098d0b8) Line
293 + 0xa C
python24_d.dll!PyEval_RestoreThread(_ts * tstate=0x0098d0b8)
Line 309 + 0x9 C
_tkinter_d.pyd!EventHook() Line 2969 + 0xc C
python24_d.dll!my_fgets(char * buf=0x009ef3e8, int len=100,
_iobuf * fp=0x1027c838) Line 46 C
python24_d.dll!PyOS_StdioReadline(_iobuf * sys_stdin=0x1027c838,
_iobuf * sys_stdout=0x1027c858, char * prompt=0x0087f974) Line 125 +
0x11 C
python24_d.dll!PyOS_Readline(_iobuf * sys_stdin=0x1027c838,
_iobuf * sys_stdout=0x1027c858, char * prompt=0x0087f974) Line 205 +
0x12 C

Is this because of access to sys.stdout ? Or some deeper darker problem
? Is there a right way to achieve this ? Basically I want to be able to
call a function from the Python prompt which creates a Tkinter window
(not necessarily interactive apart from a close button) to display some
data, but leaves the Python prompt active so I can carry on from there.

Haven't found anything about this yet. All sample multithreaded Tkinter
code I've seen uses the main thread as the GUI thread. Also, should I be
posting this to another newsgroup ?

Thanks for any help,
Mark
-----------------------------------------------------------------------
The information contained in this e-mail is confidential and solely
for the intended addressee(s). Unauthorised reproduction, disclosure,
modification, and/or distribution of this email may be unlawful. If you
have received this email in error, please notify the sender immediately
and delete it from your system. The views expressed in this message
do not necessarily reflect those of LIFFE Holdings Plc or any of its subsidiary companies.
-----------------------------------------------------------------------

Jul 18 '05 #1
2 3569
Hello John,
Mark,

I tried your code snippet with Python 2.3.4. Worked fine. Only problem was that the program fell off the end and terminated before the second thread could open the Tkinter window. So I added these lines at the end to make the main thread wait:-

from msvcrt import kbhit, getch
print "\n\nPress key to end"
while not kbhit(): pass
getch()
And I added

print "\n\nPress key to end"
l = sys.stdin.readline()
Both your Hello and Quit buttons worked.
In my case "Hello" works and "Quit" doesn't (GUI stays frozen).
Linux, Python 2.3.3, pygtk-0.6.9.
...

It is not optimal in that 'otherThread' runs continuously even when the label is not being updated. What a waste of cpu cycles! This shows up in that other windows apps slow right down. What is needed is a comms method between threads that causes a thread to block while it's waiting for data rather than my continuous polling approach. Would a Queue help here?


Yes, it should help. A time ago I tried to write a tkinter
application,
and my test code is available:

A complete Python Tkinter sample application for a long operation
http://uucode.com/texts/pylongopgui/pyguiapp.html
Maybe you find it interesting.

--
Oleg

Jul 18 '05 #2
Oleg Paraschenko wrote:
[snip]
In my case "Hello" works and "Quit" doesn't (GUI stays frozen).
Linux, Python 2.3.3, pygtk-0.6.9.


That's not a multithreading issue, but just the way the quit method works. Try:

-------------------------------------------------
import time
from Tkinter import *

root = Tk()
b = Button(root, text='Quit')
b.configure(command=root.quit)
b.pack()

root.mainloop()

time.sleep(2)
-------------------------------------------------

When you click the "Quit" button, the GUI stays there until the time.sleep ends.
root.quit just goes out of the mainloop; it doesn't destroy the widgets. To do
that, you have to add an explicit root.destroy() after root.mainloop()
--
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
Jul 18 '05 #3

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

Similar topics

2
2318
by: Adonis | last post by:
I am creating some widgets by inheriting from Tkinter.Frame and populating the frame with whatever the widget will be then creating certain attributes/methods to be accessed later. My question is,...
1
3836
by: corrado | last post by:
Hello I have an application running several thread to display some financial data; basically I have a thread displaying HTML tables by means of Tkhtml, another implementing a scrolling ticker...
8
4468
by: Erik Johnson | last post by:
I am looking for some input on GUI libraries. I want to build a Python-driven GUI, but don't really understand the playing field very well. I have generally heard good things about wxPython. I...
1
2954
by: syed_saqib_ali | last post by:
Please take a look at and run the code snippet shown below. It creates a canvas with vertical & Horizontal scroll-bars. If you shrink the window to smaller than the area of the canvas, the...
11
4228
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
16
8469
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
2
9552
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...
0
1246
by: Guilherme Polo | last post by:
2008/5/10 Kenneth McDonald <kenneth.m.mcdonald@sbcglobal.net>: I will say no to the first question. Now about the second question.. there are these links you may find interesting: "An...
3
2958
by: joshdw4 | last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes, it's that pesky root window and I have tried withdraw to no avail. I'm assuming this is because of the methods I'm using. I...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
5627
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,...
1
5052
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...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.