Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 128205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:35:54+00:00 2026-05-11T05:35:54+00:00

As someone new to GUI development in Python (with pyGTK), I’ve just started learning

  • 0

As someone new to GUI development in Python (with pyGTK), I’ve just started learning about threading. To test out my skills, I’ve written a simple little GTK interface with a start/stop button. The goal is that when it is clicked, a thread starts that quickly increments a number in the text box, while keeping the GUI responsive.

I’ve got the GUI working just fine, but am having problems with the threading. It is probably a simple problem, but my mind is about fried for the day. Below I have pasted first the trackback from the Python interpreter, followed by the code. You can go to http://drop.io/pxgr5id to download it. I’m using bzr for revision control, so if you want to make a modification and re-drop it, please commit the changes. I’m also pasting the code at http://dpaste.com/113388/ because it can have line numbers, and this markdown stuff is giving me a headache.

Update 27 January, 15:52 EST: Slightly updated code can be found here: http://drop.io/threagui/asset/thread-gui-rev3-tar-gz

Traceback

crashsystems@crashsystems-laptop:~/Desktop/thread-gui$ python threadgui.pybtnStartStop clicked Traceback (most recent call last):   File 'threadgui.py', line 39, in on_btnStartStop_clicked     self.thread.stop()   File 'threadgui.py', line 20, in stop     self.join()   File '/usr/lib/python2.5/threading.py', line 583, in join     raise RuntimeError('cannot join thread before it is started') RuntimeError: cannot join thread before it is started btnStartStop clicked threadStop = 1 btnStartStop clicked threadStop = 0 btnStartStop clicked Traceback (most recent call last):   File 'threadgui.py', line 36, in on_btnStartStop_clicked     self.thread.start()   File '/usr/lib/python2.5/threading.py', line 434, in start     raise RuntimeError('thread already started') RuntimeError: thread already started btnExit clicked exit() called 

Code

#!/usr/bin/bash import gtk, threading  class ThreadLooper (threading.Thread):     def __init__ (self, sleep_interval, function, args=[], kwargs={}):         threading.Thread.__init__(self)         self.sleep_interval = sleep_interval         self.function = function         self.args = args         self.kwargs = kwargs         self.finished = threading.Event()      def stop (self):         self.finished.set()         self.join()      def run (self):         while not self.finished.isSet():             self.finished.wait(self.sleep_interval)             self.function(*self.args, **self.kwargs)  class ThreadGUI:     # Define signals     def on_btnStartStop_clicked(self, widget, data=None):         print 'btnStartStop clicked'         if(self.threadStop == 0):             self.threadStop = 1             self.thread.start()         else:             self.threadStop = 0             self.thread.stop()         print 'threadStop = ' + str(self.threadStop)      def on_btnMessageBox_clicked(self, widget, data=None):         print 'btnMessageBox clicked'         self.lblMessage.set_text('This is a message!')         self.msgBox.show()      def on_btnExit_clicked(self, widget, data=None):         print 'btnExit clicked'         self.exit()      def on_btnOk_clicked(self, widget, data=None):         print 'btnOk clicked'         self.msgBox.hide()      def on_mainWindow_destroy(self, widget, data=None):         print 'mainWindow destroyed!'         self.exit()      def exit(self):         print 'exit() called'         self.threadStop = 1         gtk.main_quit()      def threadLoop(self):         # This will run in a thread         self.txtThreadView.set_text(str(self.threadCount))         print 'hello world'         self.threadCount += 1      def __init__(self):         # Connect to the xml GUI file         builder = gtk.Builder()         builder.add_from_file('threadgui.xml')          # Connect to GUI widgets         self.mainWindow = builder.get_object('mainWindow')          self.txtThreadView = builder.get_object('txtThreadView')         self.btnStartStop = builder.get_object('btnStartStop')         self.msgBox = builder.get_object('msgBox')         self.btnMessageBox = builder.get_object('btnMessageBox')         self.btnExit = builder.get_object('btnExit')         self.lblMessage  = builder.get_object('lblMessage')         self.btnOk = builder.get_object('btnOk')          # Connect the signals         builder.connect_signals(self)          # This global will be used for signaling the thread to stop.         self.threadStop = 1          # The thread         self.thread = ThreadLooper(0.1, self.threadLoop, (1,0,-1))         self.threadCounter = 0  if __name__ == '__main__':     # Start GUI instance     GUI = ThreadGUI()     GUI.mainWindow.show()     gtk.main() 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T05:35:54+00:00Added an answer on May 11, 2026 at 5:35 am

    Threading with PyGTK is bit tricky if you want to do it right. Basically, you should not update GUI from within any other thread than main thread (common limitation in GUI libs). Usually this is done in PyGTK using mechanism of queued messages (for communication between workers and GUI) which are read periodically using timeout function. Once I had a presentation on my local LUG on this topic, you can grab example code for this presentation from Google Code repository. Have a look at MainWindow class in forms/frmmain.py, specially for method _pulse() and what is done in on_entry_activate() (thread is started there plus the idle timer is created).

    def on_entry_activate(self, entry):     text = entry.get_text().strip()     if text:         store = entry.get_completion().get_model()         if text not in [row[0] for row in store]:             store.append((text, ))         thread = threads.RecommendationsFetcher(text, self.queue)# <- 1         self.idle_timer = gobject.idle_add(self._pulse)# <- 2         tv_results = self.widgets.get_widget('tv_results')         model = tv_results.get_model()         model.clear()         thread.setDaemon(True)# <- 3         progress_update = self.widgets.get_widget('progress_update')         progress_update.show()         thread.start()# <- 4 

    This way, application updates GUI when is ‘idle’ (by GTK means) causing no freezes.

    • 1: create thread
    • 2: create idle timer
    • 3: daemonize thread so the app can be closed without waiting for thread completion
    • 4: start thread
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 114k
  • Answers 114k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To show trace information only in trace.axd you should enable… May 11, 2026 at 10:11 pm
  • Editorial Team
    Editorial Team added an answer One idea would be to remove the click event at… May 11, 2026 at 10:11 pm
  • Editorial Team
    Editorial Team added an answer You can cheat by simply having a composite function that… May 11, 2026 at 10:11 pm

Related Questions

As someone new to GUI development in Python (with pyGTK), I've just started learning
Hello there and Merry Christmas !!! I am new to WPF and I am
I'm currently writing a very basic Java game based on the idea of Theme
I was looking for a portable script or command line program that can synchronize
Vista puts out a new security preventing Session 0 from accessing hardware like the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.