Python指标在Ubuntu 3次后停止更新 [英] Python Indicator stops updating after 3 times on Ubuntu

查看:120
本文介绍了Python指标在Ubuntu 3次后停止更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python编写一个指标,该指标应每5分钟更新一次.不幸的是,标签仅更新了3次,但尽管线程仍在运行,但标签保持不变.

i am writing an Indicator in Python which should update every 5 minutes. Unfortunately the label is updated only 3 times, but then the label stays the same, although the thread still runs.

出于演示目的,我将数据替换为当前时间.

For demonstration purpose, I replaced the data with the current time.

#!/usr/bin/env python3
import signal
import gi
import threading
gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Gtk, AppIndicator3, GObject
import time

class Indicator():
    def __init__(self):
        self.app = 'turtle-mining-indicator'
        self.menu = {}
        self.indicator = AppIndicator3.Indicator.new(
            self.app, ICON,
            AppIndicator3.IndicatorCategory.OTHER)
        self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
        self.indicator.set_menu(self.create_menu())
        self.indicator.set_label("Starting ...", self.app)
        self.scheduler()

    def update(self):
        print("update " + time.asctime())
        self.indicator.set_label(time.asctime(), self.app)
        self.menu["first"].set_label("First: " + time.asctime())

    def scheduler(self):
        self.update()
        self.timer = threading.Timer(3, self.scheduler).start()

    def create_menu(self):
        menu = Gtk.Menu()
        self.menu["first"] = Gtk.MenuItem("First")
        menu.append(self.menu["first"])
        menu.show_all()
        return menu


Indicator()
signal.signal(signal.SIGINT, signal.SIG_DFL)
Gtk.main()

我不明白,为什么会这样.方法update()被执行,但是set_label方法仅工作3次.我在做什么错了?

I don't understand, why this happens. The method update() gets executed but the set_label method only works 3 times. What am I doing wrong?

推荐答案

混合线程和Gtk是完全错误的. GLib.timeout_add_seconds().这是进行及时更新的正确方法:

Mixing threads and Gtk is just plain wrong. GLib.timeout_add_seconds() is made specifically for this reason. Here is the proper way to do timely updates:

#!/usr/bin/env python3
import signal
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Gtk, AppIndicator3, GLib
import time

class Indicator():
    def __init__(self):
        self.app = 'turtle-mining-indicator'
        self.menu = {}
        self.indicator = AppIndicator3.Indicator.new(
            self.app, "my indicator",
            AppIndicator3.IndicatorCategory.OTHER)
        self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
        self.indicator.set_menu(self.create_menu())
        self.indicator.set_label("Starting ...", self.app)
        GLib.timeout_add_seconds (1, self.update)

    def update(self):
        print("update " + time.asctime())
        self.indicator.set_label(time.asctime(), self.app)
        self.menu["first"].set_label("First: " + time.asctime())
        return True

    def create_menu(self):
        menu = Gtk.Menu()
        self.menu["first"] = Gtk.MenuItem("First")
        menu.append(self.menu["first"])
        menu.show_all()
        return menu


Indicator()
signal.signal(signal.SIGINT, signal.SIG_DFL)
Gtk.main()

这篇关于Python指标在Ubuntu 3次后停止更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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