473,909 Members | 6,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use threads or Tkinter event loop?

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 the blocking to be
non-blocking. I got threading to work, but it seemed a bit complicated.
So, I decided to try something simpler, by using the Tkinter event loop
to force the output to update/display.

it seems to work well enough. Here is my threaded code:

non-threaded:

def insertDump(self ):
self.finkinstal led = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
for line in self.finkinstal led:
self.t.insert(E ND, line)
self.update()
self.t.see(END)

And here is my non-threaded code (needs two functions to work)

def insertDump(self ):
try:
data = self.dataQueue. get(block=False )
for line in data:
self.t.insert(E ND, line)
self.t.see(END)
self.update()
except:
print "error"
raise

def getDump(self):

self.file = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
self.dataQueue. put(self.file)

This brings me to a design, as opposed to coding, question. The
non-threaded version seems to work just as well as the threaded one, in
terms of speed. Moreover, it is simpler to code and debug, because I
don't have to check to make sure the thread queue has data (I sometimes
get an 'Empty' error message when I first start the thread). Simply
using the Tk event loop (self.update) is also how I would have coded
this in Tcl.

So my question is this: under what circumstances in Python are threads
considered "best practice"? Am I wrong to use the Tk event loop instead
of threads?

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Mar 27 '07 #1
2 9589
Kevin Walzer wrote:
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 the blocking to be
non-blocking. I got threading to work, but it seemed a bit complicated.
So, I decided to try something simpler, by using the Tkinter event loop
to force the output to update/display.

it seems to work well enough. Here is my threaded code:

non-threaded:

def insertDump(self ):
self.finkinstal led = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
for line in self.finkinstal led:
self.t.insert(E ND, line)
self.update()
self.t.see(END)

And here is my non-threaded code (needs two functions to work)

def insertDump(self ):
try:
data = self.dataQueue. get(block=False )
for line in data:
self.t.insert(E ND, line)
self.t.see(END)
self.update()
except:
print "error"
raise

def getDump(self):

self.file = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
self.dataQueue. put(self.file)

This brings me to a design, as opposed to coding, question. The
non-threaded version seems to work just as well as the threaded one, in
terms of speed. Moreover, it is simpler to code and debug, because I
don't have to check to make sure the thread queue has data (I sometimes
get an 'Empty' error message when I first start the thread). Simply
using the Tk event loop (self.update) is also how I would have coded
this in Tcl.

So my question is this: under what circumstances in Python are threads
considered "best practice"? Am I wrong to use the Tk event loop instead
of threads?
D'oh, I got the code snippets mixed up:

non-threaded:

def insertDump(self ):
self.finkinstal led = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
for line in self.finkinstal led:
self.t.insert(E ND, line)
self.update()
self.t.see(END)

threaded:

def insertDump(self ):
try:
data = self.dataQueue. get(block=False )
for line in data:
self.t.insert(E ND, line)
self.t.see(END)
self.update()
except:
print "error"
raise

def getDump(self):

self.file = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
self.dataQueue. put(self.file)

Sorry!
--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Mar 27 '07 #2
On Mar 27, 9:07 am, Kevin Walzer <k...@codebykev in.comwrote:
Kevin Walzer wrote:
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 the blocking to be
non-blocking. I got threading to work, but it seemed a bit complicated.
So, I decided to try something simpler, by using the Tkinter event loop
to force the output to update/display.
it seems to work well enough. Here is my threaded code:
non-threaded:
def insertDump(self ):
self.finkinstal led = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
for line in self.finkinstal led:
self.t.insert(E ND, line)
self.update()
self.t.see(END)
And here is my non-threaded code (needs two functions to work)
def insertDump(self ):
try:
data = self.dataQueue. get(block=False )
for line in data:
self.t.insert(E ND, line)
self.t.see(END)
self.update()
except:
print "error"
raise
def getDump(self):
self.file = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
self.dataQueue. put(self.file)
This brings me to a design, as opposed to coding, question. The
non-threaded version seems to work just as well as the threaded one, in
terms of speed. Moreover, it is simpler to code and debug, because I
don't have to check to make sure the thread queue has data (I sometimes
get an 'Empty' error message when I first start the thread). Simply
using the Tk event loop (self.update) is also how I would have coded
this in Tcl.
So my question is this: under what circumstances in Python are threads
considered "best practice"? Am I wrong to use the Tk event loop instead
of threads?

D'oh, I got the code snippets mixed up:

non-threaded:

def insertDump(self ):
self.finkinstal led = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
for line in self.finkinstal led:
self.t.insert(E ND, line)
self.update()
self.t.see(END)

threaded:

def insertDump(self ):
try:
data = self.dataQueue. get(block=False )
for line in data:
self.t.insert(E ND, line)
self.t.see(END)
self.update()

except:
print "error"
raise

def getDump(self):

self.file = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
self.dataQueue. put(self.file)

Sorry!
--
Kevin Walzer
Code by Kevinhttp://www.codebykevin .com
It looks like Tkinter is similar to wxPython in that you're not
supposed to use the mainloop for anything except the GUI and GUI
commands. The following websites have more info on Tkinter and
threads:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/82965
http://www.thescripts.com/forum/thread22536.html
http://forums.devshed.com/python-pro...ds-123001.html

I use the Threading module for threading in wxPython. I think that
would probably serve you well with Tkinter as well. You can use the
join() method to wait for all the threads to exit.

Mike

Mar 27 '07 #3

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

Similar topics

1
1634
by: python-list | last post by:
Hello, I posted this to the tutor list, but didn't get any responses, unless my email client really messed up. So I'll try here. I'm starting to work with threads, but I'm a little confused. I think I understand the concepts, but not the controls. Why doesn't something like this work: ############# import threading def counter(x): while tEvent.isSet(): x+=1
5
3878
by: george.trojan | last post by:
My application consists of Tkinter GUI that has to communicate with a remote server. The communication is bi-directional: the GUI responds to remote requests and user actions uch as pressing a button) should send messages to the server. I want to have the network interface implemented as a separate thread, as it may take a while to proccess incoming messages. My initial approach is the following
3
2344
by: Bob Greschke | last post by:
I have a program where the user pushes a button, a "starting" message is ..inserted to a text field with an associated scroll bar, a thread is started that inserts a "working..." message on to the end of the text field until stopped, or until the loop finishes. The loop sleeps for about 3 seconds every time through (I'm just prototyping at this point). The mainloop just waits for the user to hit the same button again which will set a flag...
2
3328
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, and a status bar to show the state of the treatment. I know that because of the mainloop, my tkinter application freeze while my treatment isn't finished. That's why my status bar doesn't update herself in real time. I wanted to use the after or...
2
5125
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", foreground="blue", underline=1) text.tag_bind("a", "<Enter>", show_hand_cursor) text.tag_bind("a", "<Leave>", show_arrow_cursor) text.tag_bind("a", "<Button-1>", click) text.config(cursor="arrow")
9
3268
by: perchef | last post by:
Hi, I have several files to download and a GUI to update. I know this is a frequently asked question but i can't find an appropriate solution. My Downloader extends threading.Thread and update a wx.Gauge in GUI during the process. for src in urls: downloader = Downloader( src, destination, GUI ) downloader.start()
0
2209
by: Dale Huffman | last post by:
There have been a number of posts about calling gui methods from other threads. Eric Brunel. has reccommended calling the gui's ..event_generate method with data passed thru a queue. This worked great for me until trying to write to the gui from multiple threads. There I had problems: random typesof crashes almost all resulting in seg faults. I thought this info might help anyone trying to do the sameor at least save some time...
1
5141
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 class countdown(Tkinter.Frame):
7
3518
by: gordon | last post by:
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
0
10035
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, 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...
0
9877
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10919
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 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...
1
11046
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,...
0
10538
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 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...
1
8097
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 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...
0
7248
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();...
0
6138
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3357
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.