pygtk多线程更新main ui 的小程序

#! /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()
    




 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值