📌  相关文章
📜  如何在 PyQt5 中将图标设置为窗口?

📅  最后修改于: 2022-05-13 01:55:39.331000             🧑  作者: Mango

如何在 PyQt5 中将图标设置为窗口?

当我们设计一个 PyQt5 应用程序时,我们会在左上角看到一个图标,默认情况下它看起来像这样:
pyqt5-默认图标

在本教程中,我们将看到如何根据用户需要更改图标,为此我们使用setWindowIcon()方法并加载图标QIcon将使用属于QtGui class

代码 :

# importing the required libraries
  
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore
from PyQt5 import QtGui
import sys
  
  
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
  
        self.setWindowIcon(QtGui.QIcon('logo.png'))
        # set the title
        self.setWindowTitle("Icon")
  
        # setting  the geometry of window
        self.setGeometry(0, 0, 400, 300)
  
        # creating a label widget
        self.label = QLabel("Icon is set", self)
  
        # moving position
        self.label.move(100, 100)
  
        # setting up border
        self.label.setStyleSheet("border: 1px solid black;")
  
  
  
        # show all the widgets
        self.show()
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :
pyqt-set-icon