Window icon does not show

依然范特西╮ 提交于 2019-12-17 21:18:39

问题


I study this tutorial http://zetcode.com/gui/pyqt5/firstprograms/ and code here.

import sys
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_())  

Can I show Window icon? I use PyQt5, Python3.4 and Linuxmint 17.2 cinnamon.


回答1:


i am not sure, what you expected: the windowicon is shown as applicationIcon (e.g. here on ubuntu gnome, see my code in the background)

If no icon has been set, windowIcon() returns the application icon

(QApplication::windowIcon()).

http://doc.qt.io/qt-5/qwidget.html#windowIcon-prop

looks on ubuntu like this:

so i think, the code is working correctly




回答2:


Check where the icon file is placed. It should be placed in the execution directory in your case. You can check the execution directroy with the following change:

if __name__ == '__main__':
    import os
    print("Work dir:" + os.getcwd())

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())  



回答3:


try PySide.QtGui.QApplication.setWindowIcon(icon). Like this:

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon('web.png'))
    ex = Example()
    sys.exit(app.exec_())  


来源:https://stackoverflow.com/questions/35272349/window-icon-does-not-show

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!