#! /usr/bin/env python
'''
Created on Apr 23, 2012
@author: stedy
'''
import threading
import time
import gobject
import gtk
from gtk import gdk
gtk.threads_init()
class MyThread(threading.Thread):
def __init__(self,button):
super(MyThread, self).__init__()
self.quit = False
self.button = button
def update_label(self, counter):
# self.label.set_text("Counter: %i" % counter)
self.button.set_label("%d" % counter)
return False
def run(self):
counter = 0
while not self.quit:
counter += 1
gobject.idle_add(self.update_label, counter)
time.sleep(2)
def stop(self):
self.quit = True
class mainUi(gtk.Window,MyThread):
def __init__(self):
super(mainUi,self).__init__()
win = gtk.Window()
self.btn1 = gtk.Button()
self.btn2 = gtk.Button()
handler1 = self.btn1.connect("clicked",self.onBtnClicked)
handler2 = self.btn2.connect("clicked",self.onBtnClicked)
self.btn1.set_label("btn1")
self.btn2.set_label("btn2")
fixed = gtk.Fixed()
fixed.put(self.btn1,100,30)
fixed.put(self.btn2,50,50)
win.add(fixed)
win.set_size_request(300,200)
win.set_position(gtk.WIN_POS_CENTER)
win.show_all()
win.connect("destroy", lambda _: gtk.main_quit())
def onBtnClicked(self,widget,data = None):
print '\n button is on clicked'
self.t1.stop()
self.btn1.set_label("stoped")
#gtk.main_quit()
def main(self):
self.t1 = MyThread(self.btn1)
self.t1.start()
self.t2 = MyThread(self.btn2)
self.t2.start()
gtk.main()
if __name__ == '__main__':
tmp = mainUi()
tmp.main()
pygtk多线程更新main ui 的小程序
最新推荐文章于 2024-06-18 07:14:05 发布