Using Qt designer

1. Configure Qt designer external tools

The essence is that Qt\binin the tooldesigner.exe

Insert image description here

Please check out Getting Started with PyQt5 , pyQt5 + pycharm already explained

2. Using Qt designer

Insert image description here

2.1 Create save file ui

Fileapptest.ui This is PyQt5the layout file of ;

Insert image description here
documentapptest.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget"/>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

2.2 pyuic5.exe 工具Convert to py file

Can pyuic5.exe 工具be converted using apptest.py文件(see Getting Started with PyQt5 , pyQt5 + pycharm ):

pyuic5 apptest.ui -o apptest.py

documentapptest.py

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

# Form implementation generated from reading ui file 'apptest.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        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, 800, 22))
        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"))

Add main entrance using:

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

2.3 Directly import UI files

uic.loadUi(r'C:\Users\Administrator\Desktop\apptest.ui', self)Import UI files
from PyQt5.uic import loadUi

  • <widget class="QMainWindow" name="MainWindow">Default in UI file self; corresponding codeclass Ui_MainWindow(QMainWindow)
  • Other widgets can be used directly name, such as self.statusbar.showMessage('Ready')
    adding a menu bar as an exampleself.menubar.addAction(openAct)

apptest.py

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QFileDialog
from PyQt5.uic import loadUi


class Ui_MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        loadUi(r'C:\Users\Administrator\Desktop\apptest.ui', self)
        self.setupUi()

    def setupUi(self):
        print("setupUI")
        self.resize(800, 600)
        self.statusbar.showMessage('Ready')
        self.menuBarUI()
        QtCore.QMetaObject.connectSlotsByName(self)

    def menuBarUI(self):
        openAct = QAction(self.menubar)
        openAct.setCheckable(False)
        openAct.setObjectName('openFileAction')
        openAct.triggered.connect(self.opendir)
        openAct.setText('打开')
        self.menubar.addAction(openAct)

    def opendir(self):
        dir = QFileDialog.getExistingDirectory(self, r"Open Directory",
                                               "./",
                                               QFileDialog.ShowDirsOnly
                                               | QFileDialog.DontResolveSymlinks)
        print(str(dir))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = Ui_MainWindow()
    ui.show()
    sys.exit(app.exec_())

2. qrc resource manager

**qrc file:** See more PyQt5 resource management

<!DOCTYPE RCC>
<RCC version="1.0">
	<qresource>
		<file>images/icon.ico</file>
	</qresource>
</RCC>

Insert image description here

Guess you like

Origin blog.csdn.net/qq_23452385/article/details/131868442