Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 带线程循环的状态图标:循环不循环_Multithreading_Python 3.x_While Loop_Trayicon - Fatal编程技术网

Multithreading 带线程循环的状态图标:循环不循环

Multithreading 带线程循环的状态图标:循环不循环,multithreading,python-3.x,while-loop,trayicon,Multithreading,Python 3.x,While Loop,Trayicon,我创建了一个简单的状态图标。 其目的是在给定的时间间隔内检查文件是否存在 此时将显示状态图标,并且线程已启动。 但是,循环不循环:它进入循环,但在event.wait上停止,并且永远不会继续 我试着用时间来代替睡眠,但结果是一样的 我到处找了,但找不到答案。 我错过了什么 #! /usr/bin/env python3 #-*- coding: utf-8 -*- from gi.repository import Gtk import threading class CheckStatu

我创建了一个简单的状态图标。 其目的是在给定的时间间隔内检查文件是否存在

此时将显示状态图标,并且线程已启动。 但是,循环不循环:它进入循环,但在event.wait上停止,并且永远不会继续

我试着用时间来代替睡眠,但结果是一样的

我到处找了,但找不到答案。 我错过了什么

#! /usr/bin/env python3
#-*- coding: utf-8 -*-

from gi.repository import Gtk
import threading


class CheckStatus(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self.event = threading.Event()

    def run(self):
        cnt = 0
        while not self.event.is_set():
            cnt += 1
            print((">>> check %d" % cnt)) # This shows just once
            self.event.wait(5)
        print(">>> done")

    def stop(self):
        self.event.set()


class MyStatusIcon(object):

    def __init__(self):
        self.statusIcon = Gtk.StatusIcon()
        self.statusIcon.set_from_stock(Gtk.STOCK_ABOUT)

        # Build status icon menu
        menu = Gtk.Menu()
        menuQuit = Gtk.MenuItem("Quit")
        menuQuit.connect('activate', self.quit_tray)
        menu.append(menuQuit)

        self.statusIcon.connect('popup-menu', self.popup_menu, menu)

        # Start thread
        self.check = CheckStatus()
        self.check.start()

    def popup_menu(self, widget, button, time, data):
        data.show_all()
        data.popup(None, None, None, None, button, time)

    def quit_tray(self, widget):
        # Stop the thread and quit
        self.check.stop()
        self.check.join()
        Gtk.main_quit()

if __name__ == '__main__':
    # Create an instance of our GTK application
    try:
        MyStatusIcon()
        Gtk.main()
    except:
        pass
[编辑1]

我将while循环替换为计数器循环(而cnt<10),虽然这没有改变行为,但在我退出状态图标后,它每5秒显示一次打印的计数器

线程似乎正在等待来自状态图标的事件

[编辑2] 我需要从另一个角度来看待这个问题,我想知道GObject.timeout\u add是否可以解决这个问题……嗯,它确实解决了,但我不知道这是否是最优雅的方式

#! /usr/bin/env python3
#-*- coding: utf-8 -*-

from gi.repository import Gtk, GObject


class MyStatusIcon(object):

    def __init__(self):
        self.quit = False
        self.counter = 0
        self.service = GObject.timeout_add(5000, self.checkStatus)

        self.statusIcon = Gtk.StatusIcon()
        self.statusIcon.set_from_stock(Gtk.STOCK_ABOUT)

        # Build status icon menu
        menu = Gtk.Menu()
        menuQuit = Gtk.MenuItem("Quit")
        menuQuit.connect('activate', self.quit_tray)
        menu.append(menuQuit)

        self.statusIcon.connect('popup-menu', self.popup_menu, menu)

    def checkStatus(self):
        self.counter += 1
        if not self.quit:
            print((">>> check %d" % self.counter))
            return True
        print("Done")
        return False

    def popup_menu(self, widget, button, time, data):
        data.show_all()
        data.popup(None, None, None, None, button, time)

    def quit_tray(self, widget):
        # Stop the thread and quit
        self.quit = True
        Gtk.main_quit()

if __name__ == '__main__':
    # Create an instance of our GTK application
    try:
        MyStatusIcon()
        Gtk.main()
    except:
        pass