473,688 Members | 3,175 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 GetTkinterThrea d():
import threading
def TestTkinter():
def greeting():
print "Hello stdout world !"

win = Frame()
Label(win, text="Hello container world").pack(si de=TOP)
Button(win, text="Hello", command=greetin g).pack(side=TO P)
Button(win, text="Quit", command=win.qui t).pack(side=RI GHT)

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

t = GetTkinterThrea d() #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(c onst char * msg=0x1e23ca14) Line
1513 C
python24_d.dll! PyThreadState_S wap(_ts * new=0x0098d0b8) Line
293 + 0xa C
python24_d.dll! PyEval_RestoreT hread(_ts * tstate=0x0098d0 b8)
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_StdioReadl ine(_iobuf * sys_stdin=0x102 7c838,
_iobuf * sys_stdout=0x10 27c858, char * prompt=0x0087f9 74) Line 125 +
0x11 C
python24_d.dll! PyOS_Readline(_ iobuf * sys_stdin=0x102 7c838,
_iobuf * sys_stdout=0x10 27c858, char * prompt=0x0087f9 74) 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,
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 3591
Hello John,

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
And I added

print "\n\nPress key to end"
l = sys.stdin.readl ine()
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
and my test code is available:

A complete Python Tkinter sample application for a long operation
Maybe you find it interesting.


Jul 18 '05 #2
Oleg Paraschenko wrote:
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(com mand=root.quit)



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

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, is this a poper way to create widgets or should I take a different approach? Any help is greatly appreciated. Adonis
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 based on a Text widget with embedded windows and a thread running the Tkinter mainloop plus several other thread dealing with the scheduling of the contents and the acquisition of data but not using graphic widgets. I run the same code on Linux...
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 happen to already own John Grayson's book about Tkinter programming, so that is rather handy if I decide to use Tkinter. I have done some things slightly more involved than "Hello World" in Tkinter - I have never used wxPython. So, basically the...
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 scroll-bars work as advertised. That's great. However, if you click the Left Mouse button, it calls code which expands the width of the canvas by 100 pixels. The area being viewed expands correspondingly..... BUT I DON'T WANT IT TO!!
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 threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do not seem to be necessary. <quote> In the default implementation of threaded applications against...
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
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 displays it in a Tkinter text widget. Some of the commands are long-running and/or return thousands of lines of output. I initially thought I needed to use threading, because the GUI would block when reading the output, even when I configured...
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 Introduction to Tkinter" -- http://www.pythonware.com/library/tkinter/introduction/index.htm "Tkinter reference: a GUI for Python" -- http://infohost.nmt.edu/tcc/help/pubs/tkinter/index.html
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 guess my question is two-fold. 1) How do I get rid of that window? 2) Any comments in general? I am just learning python (and coding with classes), so I'm sure there are things I should pound into my head before I learn bad habits. Here's the...
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.