1

Possible Duplicate:
Simple pygtk and threads example please

I have this "problem" with my python script. I make window with pygtk (with one text field) and then my script performs a database query and prepare the data for storage into a txt file. Everything works great but there is only one thing I am facing - this function runs approx. 2-4 minutes and during this time my program window is not "responding", so it looks like program window freezed (but the script is running and window is "live" again after finish).

How could I treat this behaviour? I would like to have my window responsive all time. For example: have a textfield "working...".

Community
  • 1
  • 1
peter
  • 4,289
  • 12
  • 44
  • 67
  • I am by no means an expert on PyGTK, but you probably need to move the processing logic out of the PyGTK's event loop thread, so the latter can do its thing while you're doing yours. – NPE Nov 28 '12 at 13:52

1 Answers1

1

You need to use threading in your application. Any time you have a long running process you need to put that work into a separate thread and send progress updates to the main thread. I've answered a similar question before you can find a working example here. A shorter just dummy kind of example is provided below.

example

import gtk, gobject, urllib, time, threading

def run():
    for i in range(50):
        gobject.idle_add(button.set_label, '%s/50 complete' % i)
        time.sleep(0.1)

def clicked(button):
    threading.Thread(target=run).start()

gtk.gdk.threads_init()
win = gtk.Window()
button = gtk.Button(label="Start")
button.connect('clicked', clicked)
win.add(button)
win.show_all()
gtk.main()
Community
  • 1
  • 1
Marwan Alsabbagh
  • 25,364
  • 9
  • 55
  • 65