我正在努力学习PyQt。在阅读教程以获取基础知识时,我遇到了一个关于QIcon的问题。
下面的代码用于创建一个简单的窗口,其中包含一个来自名为“web.png”的图像的图标:
import os
import sys
import PyQt5
dirname = os.path.dirname(PyQt5.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('Icon')
self.setWindowIcon(QIcon('web.png'))
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())但是,生成的窗口包含一个标准图标,而不是所需的图像:

图像web.png包含在当前工作目录中。我使用的是Python3.5.1和PyQt 5以及Qt 5.6.2。
任何帮助都将不胜感激。
发布于 2020-12-31 00:50:07
您应该改用绝对路径:
self.setWindowIcon(QIcon('c:/root_to_your_application/web.png'))或者:
import pathlib
current_directory = str(pathlib.Path(__file__).parent.absolute())
path = current_directory + '/web.png'
self.setWindowIcon(QIcon(path))发布于 2016-11-02 04:22:28
您尝试在错误的位置更改图标。我遇到了这个问题,here是解决方案。
您需要更改"subWindow“的图标,只需查看上面的解决方案。
https://stackoverflow.com/questions/40330000
复制相似问题