将文件添加到系统托盘

huangapple go评论64阅读模式
英文:

Adding file to System Tray

问题

我已经创建了一个.exe文件。现在我想让它在启动时作为后台应用程序运行。我不想让它显示在任务栏上,只想让它显示在系统托盘中,这样当你双击系统托盘上的图标时,文件就会打开。

上下文:这是一个套接字客户端程序,它将在启动时连接到服务器,一旦收到请求,它将获取打印作业,下载文件并打印文件。该程序在终端窗口上运行,并显示打印作业、打印状态等信息。我希望用户能够在双击系统托盘中的图标时查看这个终端。一旦他们从托盘中打开应用程序并关闭它,它必须仍然存在于托盘中。唯一关闭它的方法必须是右键单击图标,然后选择退出。

我尝试过搜索实现的方法,甚至使用了ChatGPT,但仍然没有明确的答案。

英文:

I have created an .exe file. Now I want it to run on startup as a background app. I don't want it to display on taskbar, only on the system tray such that when you double-click the icon on the system tray. the file opens up.

Context: This is a socket client program which will connect to the server on startup and once a request is received it will get the print jobs, download the file and print the file. The program runs on terminal window and displays info like the print jobs, status of the print, etc. I want the user to be able to view this terminal when they double click the icon in system tray. Once they open the app from the tray and close it, it must still be in the tray. The only way to close it must be to right click the icon and pressing Exit.

I tried searching for ways to implement it even used chatgpt but still no clear cut answer.

答案1

得分: 0

  1. 通过 Windows API:

这是一个非常底层的方法,我不具备帮助的知识。不过,我曾尝试过一次,并获得了一个部分工作的实现,可以显示/隐藏控制台窗口:tray.py

  1. 使用 PySide/PyQt 库:

您可以创建一个 QtWidgets.QSystemTrayIcon 实例并将其附加到一个窗口。在窗口中,您可以重写 closeEvent 方法以忽略事件并隐藏窗口,可选地告知 QSystemTrayIcon 显示通知,表明您的窗口已最小化到系统托盘。

这种方法会更容易一些,但您可能需要设计一个作为控制台的 Qt 窗口,以使其正常工作。如果需要的话,也可以更新一个小部件以显示来自 stdout 的输出。

英文:

As a starting point, there's 2 ways that I know of.

  1. Via the Windows API:

This is very low level, and I don't have the knowledge to help out. However I did attempt it once and got a somewhat working implementation that can show/hide a console window: tray.py

  1. Using the PySide/PyQt libraries.

You can create a QtWidgets.QSystemTrayIcon instance and attach it to a window. In the window, you override closeEvent to ignore the event and hide the window, optionally telling the QSystemTrayIcon to display a notification that your window has been minimised to the tray.

This way would be a lot easier, but you may need to design Qt window that'll act as the console for it to work properly. It would be possible update a widget with the output from stdout if you wanted to go down that route.

huangapple
  • 本文由 发表于 2023年2月23日 19:16:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定