[英]Setting the Windows taskbar icon in PyQt
I'm working on an applcation in Python's PyQt4 and cannot find how to change the taskbar icon.我正在 Python 的 PyQt4 中开发一个应用程序,但找不到如何更改任务栏图标。 I made my .ui files in Qt's Designer, where I can change the
windowIcon
properties.我在 Qt 的设计器中创建了我的 .ui 文件,我可以在其中更改
windowIcon
属性。 But that is not what I am looking for.但这不是我要找的。 I want to change the look of the application's icon in windows taskbar.
我想更改 Windows 任务栏中应用程序图标的外观。 For now it is Python logo in a window icon.
现在它是窗口图标中的 Python 标志。
I found some information on SO: link but it's not helping me much.我在 SO: 链接上找到了一些信息,但对我帮助不大。
I tried:我试过了:
app = QtGui.QApplication([])
app.setWindowIcon(QtGui.QIcon('chip_icon_normal.png'))
app.exec_()
But the icon remains unchanged.但图标保持不变。
What i want to change, showing the picture: (This is done calling the setWindowIcon on main window/ dialog, or the application, as shown above.)我想改变什么,显示图片:(这是在主窗口/对话框或应用程序上调用 setWindowIcon 完成的,如上所示。)
This problem is caused by some peculiarities in how taskbar icons are handled on the Windows platform.此问题是由 Windows 平台上处理任务栏图标的某些特殊性引起的。
See this answer for details, along with a workaround using ctypes
.有关详细信息,请参阅此答案以及使用
ctypes
的解决方法。
It seems to me that the problem may be caused by lack of icon with the right size.在我看来,问题可能是由于缺少大小合适的图标造成的。 The following setup worked for me in PyQT4:
以下设置在 PyQT4 中对我有用:
# set app icon
app_icon = QtGui.QIcon()
app_icon.addFile('gui/icons/16x16.png', QtCore.QSize(16,16))
app_icon.addFile('gui/icons/24x24.png', QtCore.QSize(24,24))
app_icon.addFile('gui/icons/32x32.png', QtCore.QSize(32,32))
app_icon.addFile('gui/icons/48x48.png', QtCore.QSize(48,48))
app_icon.addFile('gui/icons/256x256.png', QtCore.QSize(256,256))
app.setWindowIcon(app_icon)
I have got a task bar icon in Windows 7 and correct icons in all windows without any changes to ui files.我在 Windows 7 中有一个任务栏图标,并且在所有窗口中都有正确的图标,而没有对 ui 文件进行任何更改。
You need to call setWindowIcon(...) on the window, not on the application.您需要在窗口上调用 setWindowIcon(...),而不是在应用程序上。
Here's an example, which works for me:这是一个对我有用的示例:
#!/usr/bin/env python3
import os
import sys
import subprocess
import os.path
from PyQt4 import QtGui
from PyQt4 import QtCore
class MyWin(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyWin, self).__init__(parent)
self.setWindowTitle("My Window")
self.setWindowIcon(QtGui.QIcon('test_icon.png'))
self.show()
def main(args):
app = QtGui.QApplication([])
ww= MyWin()
sys.exit(app.exec_())
if __name__ == '__main__':
main(sys.argv[1:])
对我来说,以下代码适用于更改任务栏图标和窗口图标
win.setWindowIcon(QIcon('logo.png'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.