如何在 py-gtk 窗口中显示连续数据? [英] How to display continuous data in a py-gtk window?

查看:61
本文介绍了如何在 py-gtk 窗口中显示连续数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 py-gtk 构建一个简单的秒表应用程序.这是我的秒表窗口中切换按钮的回调函数.

I was trying to build a simple stopwatch app using py-gtk. Here is the callback function for the toggle button in my stopwatch window.

def start(self,widget):
    if widget.get_active():         
        widget.set_label('Stop')
    else: 
        self.entry.set_text(str(time.time() - s))   
        widget.set_label('Start')

它工作得非常好,除了时间不会连续显示在条目小部件中.如果我在 'If ' 条件中添加一个无限循环,

It works perfectly well except for the fact that time does not get continuously get displayed in the entry widget. If I add an infinite while loop inside the 'If ' condition like ,

while 1:
    self.entry.set_text(str(time.time() - s))

窗口变得无响应.有人可以帮忙吗?

the window becomes unresponsive. Can someone help?

推荐答案

使用 gobject.timeout_add:

gobject.timeout_add(500, self.update)

让 gtk 每 500 毫秒调用一次 self.update().

to have gtk call self.update() every 500 milliseconds.

update 方法中检查秒表是否处于活动状态并调用

In the update method check if the stopwatch is active and call

self.entry.set_text(str(time.time() - s))   

根据需要.

以下是使用 gobject.timeout_add 绘制进度条的相当简短的示例:

Here is fairly short example which draws a progress bar using gobject.timeout_add:

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

class ProgressBar(object):
    def __init__(self):
        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)
        gobject.timeout_add(100, self.timeout)
    def timeout(self):
        self.val += 1
        # time.sleep(1)
        self.scale.set_value(self.val)
        return True

def demo_timeout_add():
    # http://faq.pygtk.org/index.py?req=show&file=faq23.020.htp
    # http://stackoverflow.com/a/497313/190597
    win = gtk.Window()
    win.set_default_size(300, 50)
    win.connect("destroy", gtk.main_quit)
    bar = ProgressBar()
    win.add(bar.scale)
    win.show_all()
    gtk.main()

demo_timeout_add()

这篇关于如何在 py-gtk 窗口中显示连续数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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