logo       

threading and TextView: msg#00074

gnome.gtk+.python

Subject: threading and TextView

Hello *!

I am experimenting with threading and GtkTextView and I have run into
troubles. I took Cedric Gustin's example code [1] which works fine and
made the thread change a TextView's buffer instead of a Button's label.
Whenever the thread is run, however, the program hangs. I am at a loss.
Could somebody knowledgeable look over the my example [2]? I included it
below.

Regards,
Felix

PS: I am running Python 2.2.3 and PyGTK 1.99.16 on Linux.

[1] Cedric's original example:
http://www.daa.com.au/pipermail/pygtk/2003-August/005626.html

[2] My non-functional one:

-----------------------
#!/usr/bin/env python

import time
import pygtk
pygtk.require('2.0')
import gtk

from threading import Thread


def start_new_thread (*args):

# Here this works
buf = tview.get_buffer()
iter = buf.get_end_iter()
buf.insert(iter,"here it works")

a = Test(tview)
a.start()

button.set_label("Thread started")


def destroy(*args):
window.hide()
gtk.main_quit()


class Test (Thread):
def __init__ (self,tview):
Thread.__init__(self)
self.tview=tview

def run (self):
for i in range(0,10):
time.sleep(1)
# Acquire and release the lock each time.
gtk.threads_enter()

# Here it does not
buf = self.tview.get_buffer()
iter = buf.get_end_iter()
buf.insert(iter,"here it doesn't")

gtk.threads_leave()


# Initialize threads
gtk.threads_init()

window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.connect("destroy", destroy)
window.set_border_width(10)
window.set_default_size(200,200)

vbox = gtk.VBox()
tview = gtk.TextView()
button = gtk.Button(" Start Thread ")
button.connect("clicked", start_new_thread)

vbox.add(button)
vbox.add(tview)
window.add(vbox)
window.show_all()

gtk.threads_enter()
gtk.main()
gtk.threads_leave()


_______________________________________________
pygtk mailing list pygtk@xxxxxxxxxx
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise