[英]Link my Python GUI script with system tray script pywin32 (tkinter)
I've created an executable file of my GUI python script using Pyinstaller (I used tkinter as the framework, and Anaconda3, Python 3.7 and I am on Windows 10). I've created an executable file of my GUI python script using Pyinstaller (I used tkinter as the framework, and Anaconda3, Python 3.7 and I am on Windows 10). I have set it up to start automatically upon Windows startup, using Task Scheduler.我已使用任务计划程序将其设置为在 Windows 启动时自动启动。
I have been asked to make it run in the system tray, silently, but not as a service (because sometimes it requires user interaction).我被要求让它在系统托盘中静默运行,但不作为服务运行(因为有时它需要用户交互)。
Thanks to the incredibly helpful users from here:感谢这里非常乐于助人的用户:
What's the simplest way to put a python script into the system tray (Windows) 将 python 脚本放入系统托盘(Windows)的最简单方法是什么
Minimise Python to System Tray and Generate Notifications? 将 Python 最小化到系统托盘并生成通知?
How to build a SystemTray app for Windows? 如何为 Windows 构建 SystemTray 应用程序?
I have the win32py script that creates the system tray icon with some menu and simple functions.我有一个win32py脚本,它可以创建带有一些菜单和简单功能的系统托盘图标。 However, I can not figure out the very obvious thing of how to link my tkinter GUI script with this code.. I tried to execute my script using execfile().open() and make it run like a normal menu_action within the script, but it doesn't work.但是,我无法弄清楚如何将我的 tkinter GUI 脚本与此代码链接。我尝试使用 execfile().open() 执行我的脚本并使其像脚本中的普通 menu_action 一样运行,但它不起作用。
Some discussions here:这里的一些讨论:
Hide tkinter window in system tray 在系统托盘中隐藏 tkinter window
show that it is not possible to hide tkinter frames in the system tray.表明无法在系统托盘中隐藏 tkinter 框架。 But as far as I understand, the script from above is independent on this.但据我了解,上面的脚本独立于此。
I found out about this tcl extension tktray ( https://core.tcl-lang.org/jenglish/gutter/packages/tktray.html ) and I am now learning how to build it into tlc., which isn't as trivial as I thought.我发现了这个 tcl 扩展tktray ( https://core.tcl-lang.org/jenglish/gutter/packages/tktray.html ),我现在正在学习如何将它构建到 tlc 中,这不是微不足道的和我想的一样。 However, tktray is 10 years old so I don't have much hopes it will be compatible with current versions.但是,tktray 已有 10 年历史,所以我不希望它与当前版本兼容。
All other solutions I found were for other programming languages, other OS or using PyQ5.我发现的所有其他解决方案都适用于其他编程语言、其他操作系统或使用 PyQ5。 I would hate to rewrite my entire code with wxPython or PyQ5 just for this extra feature, so if anyone can help me out with linking my GUI script with the win32py script from above...我不想为了这个额外的功能而用wxPython或PyQ5重写我的整个代码,所以如果有人可以帮助我将我的 GUI 脚本与上面的 win32py 脚本链接起来......
In Tkinter, there's some helpful functions like:在 Tkinter 中,有一些有用的功能,例如:
root.withdraw() # to hide the window
root.deiconify() # to show the window
root.protocol() # control Tk's *protocols*
root.attributes() # control Tk's attributes that are only available in Tcl
So when I use those functions and this script altogether, I could create a script that can hide and show it's window when you toggle an option to hide/show it因此,当我完全使用这些功能和此脚本时,我可以创建一个脚本,当您切换选项以隐藏/显示它时,它可以隐藏和显示 window
(note: I'm using Windows 10 version 1909) (注意:我使用的是 Windows 10 版本 1909)
from tkinter import *
from systrayicon import SysTrayIcon
def bye(): root.withdraw() # on the top badge of the main window
def toggle(): root.deiconify() # in the system tray
def main():
root = Tk()
root.withdraw() # minimises the main window at first
root.title("Hello, world!")
root.protocol("WM_DELETE_WINDOW", bye) # minimise the script into the tray with the 'X' button
root.attributes('-toolwindow', True) # only shows the destroy button
text = Label("Hello, world!")
text.pack()
SysTrayIcon(open("./icon.ico", "r"), title, ('Open the main window', None, toggle), on_quit=exit) # the function exit() is predefined, no need to worry
root.mainloop()
if __name__ == '__main__':
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.