为什么在mac上使用PyQt5不能添加图标? [英] why using PyQt5 on mac can not add a icon?
本文介绍了为什么在mac上使用PyQt5不能添加图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import sys
import os
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')
path = os.path.join(os.path.dirname(sys.modules[__name__].__file__), 'icon_1.png')
self.setWindowIcon(QIcon(path))
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
我也使用相对路径,如self.setWindowIcon(QIcon('icon_1.png'))
我确定 icon_1.png 在目录中.但结果总是这样:
I also use a relative path like
self.setWindowIcon(QIcon('icon_1.png'))
I am sure icon_1.png is at the directory.But the result is always like that:
那么我哪里做错了?我是 PyQt 和 StackOverflow 以及英语的新手...希望帖子有效.
So where did I make some wrong? I am a newbie in both PyQt and StackOverflow and English... Hope the post is valid.
提前致谢.
推荐答案
setWindowIcon
是 QApplication
的方法,而不是 QWidget
和朋友的方法
setWindowIcon
is a method for QApplication
, not for QWidget
and friends
这是您的测试脚本的工作版本:
Here is a working version of your test script:
import sys
import os
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300,300,300,220)
self.setWindowTitle('Icon')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
path = os.path.join(os.path.dirname(sys.modules[__name__].__file__), 'icon_1.png')
app.setWindowIcon(QIcon(path))
ex = Example()
sys.exit(app.exec_())
这篇关于为什么在mac上使用PyQt5不能添加图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文