On Thu, 2005-03-31 at 22:12 -0800, Brian wrote: > On Thu, 2005-31-03 at 11:39 +0200, L�szl� Monda wrote: > > On Wed, 2005-03-30 at 19:54 -0800, John Finlay wrote: > > > L�szl� Monda wrote: > > > > >Could anyone shed some lights on what's really going on here? > > > > Well, problem solved, great. There were some minor errors in the code > > beyond thread.start(). I corrected them all and attached the source. > > > > It would make a nice threading example IMO. I don't remember any > > examples where more than one thread would manipulate the GUI > > simultaneously. Someone could add it to some parts of the documentation > > or should I contact with a documentation manintainer? > > > > Here is a different way for threads to communicate between each other. > This way you can keep all GUI stuff in one thread only. I have updated > the example code to include some of your examples features. > > The dispatcher code has been created to be very generic so that it can > pass most any data between threads safely. The majority of the code >
The dispatcher IMO is a very elegant and advanced way of threading.
However unfortunately it doesn't work in every situations as others
mentioned also.
I'd like to send you another example (actually it's the third one, so
don't hit me, please ;) to demonstrate an even more problematic
situation with threading. I've implemented this example a week ago or
so with your dispatcher too, but that didn't work either.
So here's the code which I also attached in the file
pygtk-example-threading-glade.py:
#!/usr/bin/env python
import threading
import signal
import pygtk
pygtk.require('2.0')
import gtk
import gtk.glade
class Application:
def __init__(self):
signals = {
'on_window1_destroy': self.OnWindowDestroy,
'on_button1_clicked': self.OnButtonClicked
}
glade_window = gtk.glade.XML('example.glade', 'window1')
glade_window.signal_autoconnect(signals)
self.store = gtk.ListStore(str)
renderer = gtk.CellRendererText()
column = gtk.TreeViewColumn('Foos', renderer, text=0)
view = gtk.TreeView(self.store)
view.append_column(column)
self.sw = gtk.ScrolledWindow()
self.sw.add(view)
self.vbox = glade_window.get_widget('vbox1')
self.vbox.add(self.sw)
self.vbox.show_all()
self.pbar = gtk.ProgressBar()
signal.signal(signal.SIGINT, signal.SIG_DFL)
gtk.threads_init()
#gtk.threads_enter()
gtk.main()
#gtk.threads_leave()
def OnWindowDestroy(self, widget):
gtk.main_quit()
def OnButtonClicked(self, widget):
self.thread_finished = False
self.vbox.remove(self.sw)
self.vbox.pack_start(self.pbar)
self.vbox.show_all()
gtk.timeout_add(50, self.OnTimerTick)
thread = threading.Thread(target=self.DoThread)
thread.start()
def OnTimerTick(self):
gtk.threads_enter()
if not self.thread_finished:
self.pbar.pulse()
print 'Plusing ProgressBar, since '+ \
'thread_finished is '+`self.thread_finished`
else:
self.vbox.remove(self.pbar)
self.vbox.add(self.sw)
self.vbox.show_all()
gtk.threads_leave()
return not self.thread_finished
def DoThread(self):
for i in range(10000):
string = 'foo '+`i`
gtk.threads_enter()
self.store.append([string])
gtk.threads_leave()
self.thread_finished = True
print 'thread_finished is True'
if __name__ == '__main__':
application = Application()
I attached the glade file which it uses (example.glade) and a screen
shot of the window also. 'vbox1' is the central widget of the table.
The expected behaviour is that when one clicks on the button, the
progress bar should show up in the VBox and pulse while DoThread fills
up the TreeStore, then show up the TreeView in the same position.
When you press the button for the first time, it works magically. But
when you try it after that, it freezes the application. I think I used
threads_enter() and threads_leave() according to the golden rules so I
don't really get what's wrong.
My guess would be some glade bug. The other possible reason is probably
that I'm just plain stupid and messed up something very hard.
Any ideas?
--
L�szl� Monda <http://mondalaci.objectis.net>
pygtk-example-threading-glade.py
Description: application/python
example.glade
Description: application/glade
<<attachment: pygtk-example-threading-glade.png>>
_______________________________________________ pygtk mailing list [email protected] http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
