用GTK、python更新标签

用GTK、python更新标签,python,gtk,Python,Gtk,我现在正在学习GTK。感觉好像我在浪费时间在文档上。而且教程非常少 我正在尝试制作一个简单的应用程序,可以近实时地显示我的CPU使用情况和温度,但我一直在更新标签。我知道set_标签(“文本”),但我不知道如何以及在何处使用它。 不用说,我是个彻头彻尾的傻瓜 以下是我的示例代码: import subprocess from gi.repository import Gtk import sys 类来获取CPU数据 寡妇构造函数 标签类 应用程序构造函数 您应该通过超时调用所有方法: GLib

我现在正在学习GTK。感觉好像我在浪费时间在文档上。而且教程非常少

我正在尝试制作一个简单的应用程序,可以近实时地显示我的CPU使用情况和温度,但我一直在更新标签。我知道set_标签(“文本”),但我不知道如何以及在何处使用它。 不用说,我是个彻头彻尾的傻瓜

以下是我的示例代码:

import subprocess
from gi.repository import Gtk
import sys
类来获取CPU数据 寡妇构造函数 标签类 应用程序构造函数
您应该通过超时调用所有方法:

GLib.timeout_add(ms, method, [arg])

其中,
ms
是毫秒,
s
是秒(更新间隔),
method
将是getUsage和getTemp方法。您可以将标签作为
arg
传递

然后您只需调用标签上的
set\u text(txt)
方法

注意:您需要像这样导入GLib:

from gi.repository import GLib
编辑#1 正如@jku指出的,以下方法已经过时,可能只是为了提供与遗留代码的向后兼容性而存在(因此您不应该使用它们):



编辑#2 由于您的数据方法
get_temp
get_usage
更通用,您可以使用一些wrapper函数:

def updateLabels(labels):
    cpuLabel  = labels[0]
    tempLabel = labels[1]
    cpuLabel.set_text(CpuData.get_usage())
    tempLabel.set_text(CpuData.get_usage())
    return False
然后简单地做:

GLib.timeout_add_seconds(1, updateLabels, [cpuLabel, tempLabel]) # Will update both labels once a second
编辑#3 正如我所说,下面是示例代码:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GLib
import time


class TimeLabel(Gtk.Label):
    def __init__(self):
        Gtk.Label.__init__(self, "")
        GLib.timeout_add_seconds(1, self.updateTime)
        self.updateTime()


    def updateTime(self):
        timeStr = self.getTime()
        self.set_text(timeStr)
        return GLib.SOURCE_CONTINUE

    def getTime(self):
        return time.strftime("%c")


window = Gtk.Window()
window.set_border_width(15)
window.connect("destroy", Gtk.main_quit)

window.add(TimeLabel())
window.show_all()
Gtk.main()

实际上它是GLib.timeout\u add(),在这种情况下可能更合适。@jku至少在我的机器上,
GObject.timeout\u add
没有任何问题。但是你是对的,
timeout\u add\u seconds
在这种情况下会更有用,我假设它是有效的,因为他们把GObject版本放在那里,为来自旧pygobject绑定的人提供向后兼容性。它似乎不适用于返回字符串的函数。@user2325445我有一个小误解,将更新答案
GLib.timeout_add_seconds(s, method, [arg])
from gi.repository import GLib
GObject.timeout_add(ms,method,  [arg])
GObject.timeout_add_seconds(s,method,  [arg])
def updateLabels(labels):
    cpuLabel  = labels[0]
    tempLabel = labels[1]
    cpuLabel.set_text(CpuData.get_usage())
    tempLabel.set_text(CpuData.get_usage())
    return False
GLib.timeout_add_seconds(1, updateLabels, [cpuLabel, tempLabel]) # Will update both labels once a second
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GLib
import time


class TimeLabel(Gtk.Label):
    def __init__(self):
        Gtk.Label.__init__(self, "")
        GLib.timeout_add_seconds(1, self.updateTime)
        self.updateTime()


    def updateTime(self):
        timeStr = self.getTime()
        self.set_text(timeStr)
        return GLib.SOURCE_CONTINUE

    def getTime(self):
        return time.strftime("%c")


window = Gtk.Window()
window.set_border_width(15)
window.connect("destroy", Gtk.main_quit)

window.add(TimeLabel())
window.show_all()
Gtk.main()