Basic use of QT Pyside2 Designer

Insert image description here


Preface

PySide2

QT PySide2 is an open source framework for the Python programming language. It provides binding to the QT C++ framework, allowing developers to use Python for cross-platform graphical user interface (GUI) application development. QT is a powerful cross-platform application development framework with a rich set of GUI components and tools, as well as a wide range of application areas.

PySide2 is the official Python binding for QT, maintained and supported by The Qt Company. It allows developers to build GUI applications using Python's concise syntax and powerful features without having to learn C++. PySide2 provides complete access to QT, including QT's core libraries, GUI components, network functions, multimedia support, etc.

PySide2 Designer

Designer is a visual interface design tool that allows developers to create and edit graphical user interfaces (GUI) through drag and drop. Designers can use various QT controls and layouts to build interfaces, set properties and styles, define interaction logic, and preview the appearance and behavior of the interface.

QT PySide2 Designer provides an intuitive and user-friendly interface, making interface design easy and fast. Developers can select and place various QT controls in the designer, such as buttons, labels, text boxes, list boxes, etc., and then set the properties of the controls, such as size, position, color, font, etc., through the property editor. At the same time, the designer also provides a layout manager for managing the layout and automatic adjustment of controls.

After designing the interface, developers can save the design file in .ui format, which is an XML file format that contains the layout, attributes, and signal slot definitions of the interface. Design files can be loaded and used by PySide2 code, allowing developers to dynamically create and manage interfaces in applications.

In summary, QT PySide2 Designer is a powerful interface design tool that allows developers to easily create and edit the interface of QT GUI applications


提示:以下是本篇文章正文内容,下面案例可供参考

1. Install PySide2, PyQt5

pip install pyside2 -i https://pypi.douban.com/simple/
pip install pyqt5-tools -i https://pypi.douban.com/simple/

After installation, find the pyside2 module directory locally and mainly use this tool: designer.exe. It is recommended to create a shortcut;Insert image description here

2. Use designer.exe

2.1 General introduction to the tool

  • 1. For the components required by the tool, you can click on a component and drag it into the created interface;
  • 2. Check the approximate GUI interface effect. Select the component and double-click some components to directly modify the component's title or attribute value;
  • 3. View, modify, delete, select, selected components and component names. The key is to operate the layout of various components;
  • 4. Manipulate the properties, title, horizontal distance, vertical distance, width and height, font, and other styles of the component;

Insert image description here

2.2 Create a new UI

Click: File/New
Insert image description here

2.3 Save UI file as/save (Ctrl+S)

Click: File/Save As
The file suffix is:ui
Insert image description here

2.4 Use python to operate UI files

Methods for reading UI files, modifying properties of object components, and binding buttons

import sys
from PySide2.QtWidgets import QApplication, QWidget, QTableWidgetItem
from PySide2.QtUiTools import loadUiType, QUiLoader
from PySide2.QtCore import QFile, Qt
from PySide2.QtGui import QIcon


class Gui(QWidget):
    def __init__(self):
        # 加载ui文件,创建qt文件对象,加载文件对象并创建ui对象
        QtFileObj = QFile("yk.ui")
        QtFileObj.open(QFile.ReadOnly)
        QtFileObj.close()
        self.ui = QUiLoader().load(QtFileObj)

        # 设置界面图标
        icon = QIcon("yk.ico")
        self.ui.setWindowIcon(icon)

        # 变量定义、ui组件对象属性设置
        self.index = 0
        self.ui.tableWidgetAnswer.horizontalHeader().setVisible(True)  # 设置tableWidget组件的标题显示为True
        self.ui.startButton.clicked.connect(self.logger_show)  # 绑定按钮的方法
        
    def logger_show(self):
        # 插入内容
        logger_item = {
    
    
            'one': '-' * 20, 'two': '-' * 20, 'three': '-' * 20, 'four': '-' * 20,
            'five': '程序已经开始运行,请勿多次点击开始运行按钮'
        }
        self.ui.tableWidgetAnswer.insertRow(int(self.ui.tableWidgetAnswer.rowCount()))
        self.index += 1
        new_item_one = QTableWidgetItem(logger_item['one'])
        new_item_one.setTextAlignment(Qt.AlignCenter)
        new_item_two = QTableWidgetItem(logger_item['two'])
        new_item_two.setTextAlignment(Qt.AlignCenter)
        new_item_three = QTableWidgetItem(logger_item['three'])
        new_item_three.setTextAlignment(Qt.AlignCenter)
        new_item_four = QTableWidgetItem(logger_item['four'])
        new_item_four.setTextAlignment(Qt.AlignCenter)
        new_item_five = QTableWidgetItem(logger_item['five'])
        new_item_five.setTextAlignment(Qt.AlignCenter)
        self.ui.tableWidgetAnswer.setItem(self.index - 1, 0, new_item_one)
        self.ui.tableWidgetAnswer.setItem(self.index - 1, 1, new_item_two)
        self.ui.tableWidgetAnswer.setItem(self.index - 1, 2, new_item_three)
        self.ui.tableWidgetAnswer.setItem(self.index - 1, 3, new_item_four)
        self.ui.tableWidgetAnswer.setItem(self.index - 1, 4, new_item_five)
        # 定位至最新行
        self.ui.tableWidgetAnswer.verticalScrollBar().setSliderPosition(self.index)
        # 刷新
        QApplication.processEvents()


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

Summarize

This article is suitable for beginners. It is enough to make a simple GUI tool. It mainly focuses on continuous in-depth learning and progress according to your own needs. Here are some precautions and learning resources for everyone. If you have questions and Suggestions can be made in the comments;
1. The properties of the component can not only be set in the designer tool, but also in python. In the end, the properties set by python will prevail; 2. A
common problem when using GUI is to execute The interface is stuck during the function. I will introduce this problem in the following article;
QT pyside2 thread nested sub-thread implementation starts and stops running: https://blog.csdn.net/EXIxiaozhou/article/details/131401531
3. For introductory documents, please see Baiyue Heiyu: https://www.byhy.net/tut/py/gui/qt_01
4. For introductory videos, please see Baiyue Heiyu: https://www.bilibili.com/video /BV19A411H7dS/
5. Recommended by bloggers for in-depth study - domestic site: https://gitee.com/feiyangqingyun
6. Recommended by bloggers for in-depth study - international site: https://github.com/feiyangqingyun
7. CSDN personal homepage: https://gitee.com/feiyangqingyun ://blog.csdn.net/feiyangqingyun
8. Official website: https://www.pythonguis.com/topics/pyside2/

Guess you like

Origin blog.csdn.net/EXIxiaozhou/article/details/131401574