Pyside6 &Pyside2生产GUI界面的三种方式

方法一、手写代码(新手不适合):

新手写GUI时候,不知道界面的布局和控件的使用;这适合后期熟练了编写

# -*- coding:utf-8 -*-
from PySide6.QtWidgets import QMainWindow
from PySide6 import QtWidgets
import sys

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setWindowTitle("扣脚大汉")
        self.resize(400,400)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    gui = MainWindow()
    gui.show()
    sys.exit(app.exec())

Pyside6 &Pyside2生产GUI界面的三种方式_第1张图片


方法二、直接加载QT UI文件:

使用QT创建一个main.ui文件

# -*- coding:utf-8 -*-
from PySide6.QtWidgets import QMainWindow,QApplication
from PySide6 import QtWidgets
from PySide6.QtUiTools import QUiLoader
import sys

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = QUiLoader().load("./main.ui")

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    root = MainWindow()
    root.ui.show()
    app.exit(app.exec())


Pyside6 &Pyside2生产GUI界面的三种方式_第2张图片


方法三、UI文件转换成代码加载:

1、使用QT创建一个ui_main.py文件
CMD窗口转换:


pyside6-uic ui_main.ui > ui_main.py

Pyside6 &Pyside2生产GUI界面的三种方式_第3张图片
2:UI转换成的代码如下:

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

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtWidgets import (QApplication, QMainWindow, QMenuBar, QSizePolicy,
    QStatusBar, QWidget)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")
        MainWindow.resize(669, 468)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName(u"centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QMenuBar(MainWindow)
        self.menubar.setObjectName(u"menubar")
        self.menubar.setGeometry(QRect(0, 0, 669, 22))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QStatusBar(MainWindow)
        self.statusbar.setObjectName(u"statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)

        QMetaObject.connectSlotsByName(MainWindow)
    # setupUi

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"\u6263\u811a\u5927\u6c49", None))
    # retranslateUi


3:在主窗口内导入UI代码

# -*- coding:utf-8 -*-
from PySide6.QtWidgets import QMainWindow
from PySide6 import QtWidgets
import sys
# GUI FILE
from ui_main import Ui_MainWindow  # 导入UI转换成的代码

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()   # 加载UI转换成的代码窗口
        self.ui.setupUi(self)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    gui = MainWindow()
    gui.show()
    sys.exit(app.exec())

Pyside6 &Pyside2生产GUI界面的三种方式_第4张图片

你可能感兴趣的:(pyside6,&pyside2,qt,ui,python)