pyqt任务栏图标问题

今天在运行一个pyqt程序的时候发现setWindowIcon这个函数起了一半的作用,学Qt的都知道这个函数同时设置了程序的两个地方的图标显示,

第一处在程序的左上角一个小图标(只实用于window和linux,mac没有的),第二处就是任务栏的图标,如下图:



在使用pyqt的时候你发现只有第一处设置成功了,而第二处则显示的是pythonw.exe的图标,

谷歌找了一下,解决了,下面就是详细描述,英文稍差一点的同学就直接看解决方法吧,话说博主的英文也是烂的一塌糊涂哭

I've found the answer, after some digging.

In Windows 7, the taskbar is not for "Application Windows" per se, it's for "Application User Models". For example, if you have several different instances of your application running, and each instance has its own icon, then they will all be grouped under a single taskbar icon. Windows uses various heuristics to decide whether different instances should be grouped or not, and in this case it decided that everything hosted by Pythonw.exe should be grouped under the icon for Pythonw.exe.

The correct solution is for Pythonw.exe to tell Windows that it is merely hosting other applications. Perhaps a future release of Python will do this. Alternatively, you can add a registry key to tell Windows that Pythonw.exe is just a host rather than an application in its own right. See MSDN documentation forAppUserModelIDs.

Alternatively, you can use a Windows call from Python, to explicitly tell Windows what the correct AppUserModelID is for this process:

简而言之,我的理解就是pyqt是要给python解释的,也就是说windows在运行的时候认识到的是python,而你写的程序则被看做是python这个主程序的一个子程序,这个时候你就要站出来告诉windows,我这个窗口强制使用单独的AppUserModelID ,现在这个窗口拥有了一个新资源支配权限,更多的可定制。

如果我的理解有误,还请留言指正.

那到底要怎么告诉windows呢:

import ctypes
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("myappid")

以上只适用于windows,

但如果你不幸用的linux怎么办,其实更简单,用过ubuntu的都知道每一个桌面软件在/usr/share/applications目录下面都对应一个图标文件 .desktop

等你软件做好打包的时候自动添加这么一个文件就行了

----------------------------------------------------

今天在家用linux试了一下才发现  ,在linux下setWindowIcon这个函数是没有问题的,也就是说这个问题只有在windows下面才有,所以上面linux解决办法,当我没说。。。


PyQt5 能够通过 QSystemTrayIcon 类来创建一个任务栏图标,以方便用户随时控制应用程序。任务栏图标可以用于提醒用户有新消息、进入特定模式或临时关闭程序等。在此类应用程序中,“状态托盘图标”用于在系统通知区域中显示应用程序的状态。 要创建任务栏图标,我们首先要在代码中导入 QSystemTrayIcon 类。通常情况下,在主窗口中建立一个 QTimer 对象,通过 QTimer 定时器来检查新消息等,并显示在任务栏图标的 tooltip 信息中。 代码实现的方法如下: ```python from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QMenu from PyQt5.QtCore import QTimer class SystemTrayIcon(QSystemTrayIcon): def __init__(self, parent=None): super(SystemTrayIcon, self).__init__(parent) self.setIcon(QIcon('icon.ico')) self.setToolTip('System Tray Icon Demo') menu = QMenu() action1 = menu.addAction("打开主窗口") action2 = menu.addAction("退出") self.activated.connect(self.onTrayIconActivated) self.setContextMenu(menu) def onTrayIconActivated(self, reason): if reason == QSystemTrayIcon.DoubleClick: self.parent().show() if __name__ == '__main__': app = QApplication([]) w = MainWindow() tray = SystemTrayIcon(w) timer = QTimer() timer.timeout.connect(tray.showMessage('提示', '你有新消息')) timer.start(5000) tray.show() app.exec_() ``` 在上面的代码中,我们首先定义了一个 SystemTrayIcon 类,然后在该类中初始化了 QIcon、QMenu、QAction 对象等。在 QMenu 中,我们定义了如何在任务栏图标上右键单击时弹出的菜单,并使用了 self.activated 信号来定义单击任务栏图标时的操作。在 onTrayIconActivated 方法中,我们可以根据 reason 参数来判断是怎么单击的,并执行不同的操作。最后,我们在 main 函数中使用 QTimer 定时器来检查新消息,并显示在任务栏图标的 tooltip 信息中。 在 PyQt5 中创建一个任务栏图标并不难,但是如何实现具体的用户交互体验以及利用任务栏图标进行实际功能呢,这就需要开发者自己进行设计和思考了。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值