GTK und Threads

Code-Stücke können hier veröffentlicht werden.
Antworten
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

Hier ein Code von Cedric Gustin, der zeigt, wie man Threading und GTK unter Python verbinden kann. Ich habe mir echt einen Wolf gesucht, bis ich eine Lösung gefunden habe und dann möchte ich sie euch nicht vorenthalten:

Code: Alles auswählen

#!/usr/bin/env python

""" Simple pyGTK multithreading example"""

# Adapted from a post by Alif Wahid on the pyGTK mailinglist.
# Cedric Gustin, August 2003

import sys
import time
import gtk

from threading import Thread

threadcount=0

class Test (Thread):
    def __init__ (self,button, count=0):
        Thread.__init__(self)
        self.count = count
        self.button=button

    def run (self):
        for i in range(0,10):
            time.sleep(1)
            # Acquire and release the lock each time.
            gtk.threads_enter()
            self.button.set_label("Thread %002d - %d" % (self.count,i))
            gtk.threads_leave()
        gtk.threads_enter()
        self.button.set_label("  Start Thread  ")
        gtk.threads_leave()

def start_new_thread (button, data=None):
    global threadcount
    threadcount += 1
    a = Test(button,threadcount)
    a.start()


def hello(*args):
    """ Callback function that is attached to the button """
    print "Hello World"
    window.destroy()

def destroy(*args):
    """ Callback function that is activated when the program is destoyed
"""
    window.hide()
    gtk.main_quit()

# Initialize threads
gtk.threads_init()

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

button = gtk.Button("  Start Thread  ")
button.connect("clicked", start_new_thread,button)
window.add(button)
button.show()

window.show_all()
gtk.threads_enter()
gtk.main()
gtk.threads_leave()
Originale Email. Doof nur, dass das alte Mailinglistenarchiv down ist, dann hätte ich es auch in der FAQ gefunden.
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
Antworten