无法关闭使用while True循环的托盘图标脚本

2024-05-23 22:07:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我想写一个在后台运行的脚本,只要用户不退出程序,脚本就会最小化到托盘图标。我没有也不需要一个漂亮的GUI,所以我只使用infi systray来实现这一点

我需要脚本休眠一段时间,但我需要能够通过右键单击托盘图标来关闭它。当脚本运行时,由于While True语句,我无法执行该操作

from infi.systray import SysTrayIcon
from win10toast import ToastNotifier
import time

starttime = time.time()
toaster = ToastNotifier()

def startTimer(systray):
    while True:
        toaster.show_toast("test1", "test1", duration=5)
        toaster.show_toast("test2", "test2", duration=5)
        time.sleep(60 - ((time.time() - starttime) % 60.0))

menu_options = (("Start", None, startTimer),)
systray = SysTrayIcon("icon.ico", "test", menu_options)
systray.start()

任何提示都将不胜感激


Tags: fromimport脚本truetimeshow图标infi