Pyqt5设置窗口图标(任务栏图标)

需求:
1、设置程序窗口图标
2、设置程序任务栏图标
3、自定义图标
demo:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'hello.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
import os

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(384, 178)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 384, 26))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))



if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    # 设置窗口图标
    ico_path = os.path.join(os.path.dirname(__file__), 'my.ico')
    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap(ico_path), QtGui.QIcon.Normal, QtGui.QIcon.Off)
    MainWindow.setWindowIcon(icon)
    # 设置窗口标题
    MainWindow.setWindowTitle('hello标题')
    MainWindow.show()
    sys.exit(app.exec_())

完成这一步在使用pyinstaller -D 打包生成xx.spec文件
在文件中的console下添加iocn=''C:\Users\Administrator\Desktop\comunity\my.ico'
注意如何引用图标路径!
示例:

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(['hello.py'],
            pathex=['C:\\Users\\Administrator\\Desktop\\comunity'],
            binaries=[],
            datas=[('C:\\Users\\Administrator\\Desktop\\comunity\\my.ico','.')],
            hiddenimports=[],
            hookspath=[],
            runtime_hooks=[],
            excludes=[],
            win_no_prefer_redirects=False,
            win_private_assemblies=False,
            cipher=block_cipher,
            noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
            cipher=block_cipher)
exe = EXE(pyz,
         a.scripts,
         [],
         exclude_binaries=True,
         name='hello',
         debug=False,
         bootloader_ignore_signals=False,
         strip=False,
         upx=True,
         console=False,
         icon='C:\\Users\\Administrator\\Desktop\\comunity\\my.ico')
coll = COLLECT(exe,
              a.binaries,
              a.zipfiles,
              a.datas,
              strip=False,
              upx=True,
              upx_exclude=[],
              name='hello')

注意图标如何打包到目标路径!

实现效果:
image.png

使用PythonMargick将图片转icon

第三方包PythonMargick可以到https://www.lfd.uci.edu/~gohlke/pythonlibs/下载

import PythonMagick

img = PythonMagick.Image(r'icon.jpg')
# 这里设置一下尺寸,不然ico尺寸可能会报异常错误
img.sample('128x128')
img.write(r'my.ico')

你可能感兴趣的:(Pyqt5设置窗口图标(任务栏图标))