如何使用PyQt5设置窗口图标? [英] How to set a window icon with PyQt5?

查看:621
本文介绍了如何使用PyQt5设置窗口图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Application(QMainWindow):
    def __init__(self):
        super(Application, self).__init__()
        self.setWindowIcon(QtGui.QIcon('icon.png'))

我正在尝试设置一个窗口图标(在窗口的左上方),但普通图标消失了.

I am trying to set a window icon (top left of the window) but the normal icon disappeared instead.

我尝试了多种图标分辨率(8x8、16x16、32x32、64x64)和扩展名(.png和.ico).

I tried with many icon resolutions (8x8, 16x16, 32x32, 64x64) and extensions (.png and .ico).

我在做什么错了?

推荐答案

答案由问号(隐身图标)给出.我想补充一点,该脚本可能无法在脚本目录中执行.在任何情况下,为了安全起见,您可能需要确保相对于脚本所在的目录加载图标:

The answer has been given by the asker (invisible icon). I wanted to add that the script may not be executed in the script directory. In any case, to be safe, you may want to make sure the icon is loaded relative to the directory in which the script resides:

import os 
# [...]
scriptDir = os.path.dirname(os.path.realpath(__file__))
self.setWindowIcon(QtGui.QIcon(scriptDir + os.path.sep + 'logo.png'))

这篇关于如何使用PyQt5设置窗口图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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