QT 使用 qtasome图标 (python版)

首先安装 qtawesome 库,然后到图标库找到需要的图标 ( 图标名称为 fa.xxx )。

图标库链接: http://www.fontawesome.com.cn/faicons/

retranslateUi 模块中 对相应 按钮 进行操作。

QT 使用 qtasome图标 (python版)_第1张图片

运行结果

QT 使用 qtasome图标 (python版)_第2张图片

完整源码

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(181, 186)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setMaximumSize(QtCore.QSize(800, 600))
        MainWindow.setStyleSheet("")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(30, 30, 121, 51))
        font = QtGui.QFont()
        font.setPointSize(11)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(30, 110, 121, 51))
        font = QtGui.QFont()
        font.setPointSize(11)
        self.pushButton_2.setFont(font)
        self.pushButton_2.setObjectName("pushButton_2")
        MainWindow.setCentralWidget(self.centralwidget)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        
        self.pushButton.setIcon(qta.icon('fa.video-camera'))  # 设置图标
        self.pushButton.setText(_translate("MainWindow", "Icon1"))
        self.pushButton_2.setIcon(qta.icon('fa.eye', color='red'))  # 设置图标
        self.pushButton_2.setText(_translate("MainWindow", "Icon2"))

import qtawesome as qta
import sys
if __name__ == '__main__':
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
    app = QtWidgets.QApplication(sys.argv)
    widgets = QtWidgets.QMainWindow()
    window = Ui_MainWindow()
    window.setupUi(widgets)
    widgets.show()
    sys.exit(app.exec_())

你可能感兴趣的:(pycharm基础相关,python,pyqt,qtawesome)