在 pygtk 的主循环中定期调用一个函数 [英] Periodically call a function in pygtk's main loop

查看:48
本文介绍了在 pygtk 的主循环中定期调用一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tkinter 中 after 方法的 pygtk 等价物是什么?

What's the pygtk equivalent for after method in tkinter?

我想在主循环中定期调用一个函数.

I want to periodically call a function in the main loop.

实现它的更好方法是什么?

What is the better way to achieve it?

推荐答案

使用 gobject.timeout_add:

import gobject
gobject.timeout_add(milliseconds, callback)

例如这里是一个使用 timeout_add 更新进度 (HScale) 值的进度条:

For example here is a progress bar that uses timeout_add to update the progress (HScale) value:

import gobject
import gtk

class Bar(object):
    def __init__(self,widget):
        self.val=0
        self.scale = gtk.HScale()
        self.scale.set_range(0, 100)
        self.scale.set_update_policy(gtk.UPDATE_CONTINUOUS)
        self.scale.set_value(self.val)
        widget.add(self.scale)
        gobject.timeout_add(100, self.timeout)
    def timeout(self):
        self.val +=1
        self.scale.set_value(self.val)
        return True

if __name__=='__main__':
    win = gtk.Window()
    win.set_default_size(300,50)
    win.connect("destroy", gtk.main_quit)
    bar=Bar(win)
    win.show_all()
    gtk.main()

这篇关于在 pygtk 的主循环中定期调用一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆