【问题标题】:How to periodically update Gtk3 Label text?如何定期更新 Gtk3 标签文本?
【发布时间】:2018-10-19 17:58:38
【问题描述】:

我正在 Ubuntu 18.04 上编写应用程序指示器。开始是最困难的部分。文档帮助不大。我找到了this blog,并且我有一个 POC,它只在我的应用程序栏上显示一个固定文本,如下所示 -

我无法弄清楚的是如何定期或动态更新此文本以显示我需要的实际信息,例如:CPU 频率、温度 等。

我看过以下地方,但我认为我遗漏了一些东西。
https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Label.html
https://askubuntu.com/questions/108035/writing-indicators-with-python-gir-and-gtk3
https://lazka.github.io/pgi-docs/AppIndicator3-0.1/classes/Indicator.html#AppIndicator3.Indicator.set_label

我拥有的工作代码是 -

import os
import signal
from gi.repository import Gtk as gtk
from gi.repository import AppIndicator3 as appindicator

APPINDICATOR_ID = 'myappindicator'

def main():
    indicator = appindicator.Indicator.new(APPINDICATOR_ID, gtk.STOCK_INFO, appindicator.IndicatorCategory.SYSTEM_SERVICES)
    indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
    indicator.set_menu(build_menu())
    indicator.set_label('world', '8.8')
    gtk.main()

def build_label():
    label = gtk.Label()
    return label

def build_menu():
    menu = gtk.Menu()
    item_quit = gtk.MenuItem('Quit')
    item_quit.connect('activate', quit)
    menu.append(item_quit)
    menu.show_all()
    return menu

def quit(source):
    gtk.main_quit()

if __name__ == "__main__":
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    main()

编辑:
参考 this 类似的 SO 帖子和 this apparently working 示例,我尝试添加 timeout_add_secondstimeout_add 但是文本根本没有改变,它只显示第一个呼叫。我也在那里插入了一个打印语句,令人惊讶的是,它也只打印一次。不知道为什么会这样 -
新代码尝试-

import random
from gi.repository import Gtk, GLib
from gi.repository import AppIndicator3 as appindicator

APPINDICATOR_ID = 'myappindicator'

def cb_exit(w, data):
   Gtk.main_quit()

def change_label(ind_app):
    text = 'Hello World, what a great day'.split()
    t = random.choice(text)
    print(t)
    ind_app.set_label(t , '')

ind_app = appindicator.Indicator.new(APPINDICATOR_ID, Gtk.STOCK_INFO, appindicator.IndicatorCategory.SYSTEM_SERVICES)
ind_app.set_status(appindicator.IndicatorStatus.ACTIVE)

# create a menu
menu = Gtk.Menu()
menu_items = Gtk.MenuItem("Exit")
menu.append(menu_items)
menu_items.connect("activate", cb_exit, '')
menu_items.show_all()
ind_app.set_menu(menu)
GLib.timeout_add(1000, change_label, ind_app)
Gtk.main()

【问题讨论】:

  • 请再次阅读link you provided。它说 您应该通过超时调用所有方法 GLib.timeout_add(ms, method, [arg])
  • @AlexanderDmitriev 请立即查看。它不会更新标签。

标签: python gtk3 pygobject


【解决方案1】:

这是我自己想出来的。我错误地使用了timeout_add 函数。被调用的函数必须返回任何非 false 的值,计时器才能继续。在我的情况下,它什么也没返回,因此计时器正在破坏自己。 docs hereSO post 帮助我解决了这个问题。所以代码最终看起来像这样 -

import random
from gi.repository import Gtk, GLib
from gi.repository import AppIndicator3 as appindicator

APPINDICATOR_ID = 'myappindicator'

def change_label(ind_app):
    text = 'Hello world, what a beautiful day'.split()
    t = random.choice(text)
    print(t)
    ind_app.set_label(t , '')
    return True

def quit(source):
    Gtk.main_quit()

ind_app = appindicator.Indicator.new(APPINDICATOR_ID, Gtk.STOCK_INFO, appindicator.IndicatorCategory.SYSTEM_SERVICES)
ind_app.set_status(appindicator.IndicatorStatus.ACTIVE)

# create a menu
menu = Gtk.Menu()
menu_items = Gtk.MenuItem("Exit")
menu.append(menu_items)
menu_items.connect("activate", quit)
menu_items.show_all()
ind_app.set_menu(menu)
GLib.timeout_add(1000, change_label, ind_app)
Gtk.main()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多