如何在 Windows 7 中设置应用程序的任务栏图标 [英] How to set application's taskbar icon in Windows 7

查看:80
本文介绍了如何在 Windows 7 中设置应用程序的任务栏图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 PyQt4 中设置应用程序的任务栏图标?

How do I set an application's taskbar icon in PyQt4?

我尝试过 setWindowIcon,它成功地设置了主窗口左上角的图标,但它不影响 Windows 7 任务栏中显示的图标——任务栏图标仍然是默认的 Python pyw 图标.这是我的代码:

I have tried setWindowIcon, and it successfully sets the icon in the top-left of the main window, but it does not affect the icon shown in the Windows 7 taskbar -- the taskbar icon remains the default Python pyw icon. Here is my code:

from PyQt4 import QtGui

app = QtGui.QApplication([])
mainwindow = QtGui.QMainWindow()
mainwindow.show()

app.setWindowIcon(QtGui.QIcon('chalk.ico'))
mainwindow.setWindowIcon(QtGui.QIcon('chalk.ico'))
app.exec_()

[更新] 我试过将 setWindowIcon() 放在 show() 之前.我已经尝试过使用其他图像、ico 和 png.没有任何帮助.

[update] I've tried placing the setWindowIcon() before the show(). I've tried it with other images, ico and png. Nothing helps.

推荐答案

经过一番挖掘,我找到了答案.

I've found the answer, after some digging.

在 Windows 7 中,任务栏本身不是用于应用程序窗口",而是用于应用程序用户模型".例如,如果您的应用程序有多个不同的实例在运行,并且每个实例都有自己的图标,那么它们都将被分组到一个任务栏图标下.Windows 使用各种启发式方法来决定是否应该对不同的实例进行分组,在这种情况下,它决定将 Pythonw.exe 托管的所有内容都分组到 Pythonw.exe 的图标下.

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.

正确的解决方案是让 Pythonw.exe 告诉 Windows 它只是托管其他应用程序.也许未来的 Python 版本会做到这一点.或者,您可以添加一个注册表项来告诉 Windows Pythonw.exe 只是一个主机,而不是一个应用程序本身.有关 AppUserModelIDs,请参阅 MSDN 文档.

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 for AppUserModelIDs.

或者,您可以使用来自 Python 的 Windows 调用,明确告诉 Windows 此进程的正确 AppUserModelID 是什么:

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

import ctypes
myappid = 'mycompany.myproduct.subproduct.version' # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)

请参阅 Ronan 的回答:myappid 字符串应为 unicode.

Please see Ronan's answer: the myappid string should be unicode.

这篇关于如何在 Windows 7 中设置应用程序的任务栏图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆