首先环境需要安装python3和pip,软件使用pycharm,安装也都很简单
首先需要安装pyside6,在终端执行:
pip install pyside6
然后进入可视化编辑界面
pyside6-designer
进入后创建即可
可以从左侧点击鼠标拉组件进入到中间的工作区,然后就能够自定义窗口啦
接下来是将整个界面变成python代码,从而添加一些功能
首先要保存编辑好的窗口
然后就会获得一个xxx.ui格式的文件,在这个文件目录下,执行
pyside6-uic xxx.ui -o xxx.py
就能够生成一个py文件
这是具体实例:
我保存为hello.ui文件,并在这个文件同级目录下执行,然后就得到了一个ui.py文件
然后如何启动呢:
新建一个python文件,去调用生成的py文件,然后连接上按钮和方法,当点击按钮后就可以执行方法了
具体代码:
run.py
from PySide6.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton
from ui import Ui_Formclass MainWindow(QMainWindow):def __init__(self):1super(MainWindow, self).__init__()self.ui = Ui_Form() # UI类的实例化self.ui.setupUi(self)self.ui.pushButton.clicked.connect(self.function1)def function1(self):print("方法被调用了")if __name__ == '__main__':app = QApplication([]) # 启动一个应用window = MainWindow() # 实例化主窗口window.show() # 展示主窗口app.exec() # 避免程序执行到这一行后直接退出
ui.py
# -*- coding: utf-8 -*-################################################################################
## Form generated from reading UI file 'hello.ui'
##
## Created by: Qt User Interface Compiler version 6.7.0
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,QMetaObject, QObject, QPoint, QRect,QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,QFont, QFontDatabase, QGradient, QIcon,QImage, QKeySequence, QLinearGradient, QPainter,QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QLabel, QPushButton, QSizePolicy,QWidget)class Ui_Form(object):def setupUi(self, Form):if not Form.objectName():Form.setObjectName(u"Form")Form.resize(631, 490)self.label = QLabel(Form)self.label.setObjectName(u"label")self.label.setGeometry(QRect(280, 70, 71, 21))self.pushButton = QPushButton(Form)self.pushButton.setObjectName(u"pushButton")self.pushButton.setGeometry(QRect(250, 130, 151, 41))self.retranslateUi(Form)QMetaObject.connectSlotsByName(Form)# setupUidef retranslateUi(self, Form):Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))self.label.setText(QCoreApplication.translate("Form", u"hello world", None))self.pushButton.setText(QCoreApplication.translate("Form", u"\u6309\u94ae", None))# retranslateUi
大功告成~