1、安装Pyside6
- 输入下列命令安装Pyside6。
-
pip install Pyside6
-
2、设计UI
- 打开Qt设计工具(在安装Pyside6包的目录下)。
- 【注】我这用的是anaconda虚拟环境,所以我的路径是D:\App\Anaconda3\envs\snake\Lib\site-packages\PySide6。
- 设计一个界面,如何设计这里不详细讲解。
- 设计完后保存,得到一个ui文件。(另存到项目的目录下,方便查找)
3、转换为py文件
- 打开终端,进入ui文件所在的路径。输入命令进行转换:
-
PySide6-uic SedentaryReminder.ui -o ui_SedentaryReminder.py
-
- 得到py文件。
-
# -*- coding: utf-8 -*- ################################################################################ ## Form generated from reading UI file 'SedentaryReminder.ui' ## ## Created by: Qt User Interface Compiler version 6.6.1 ## ## 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, QCheckBox, QLCDNumber, QLabel, QMainWindow, QPushButton, QSizePolicy, QSpinBox, QSplitter, QWidget) class Ui_MainWindow(object): def setupUi(self, MainWindow): if not MainWindow.objectName(): MainWindow.setObjectName(u"MainWindow") MainWindow.resize(260, 300) MainWindow.setMinimumSize(QSize(260, 300)) MainWindow.setMaximumSize(QSize(260, 300)) self.centralwidget = QWidget(MainWindow) self.centralwidget.setObjectName(u"centralwidget") self.label_2 = QLabel(self.centralwidget) self.label_2.setObjectName(u"label_2") self.label_2.setGeometry(QRect(37, 143, 48, 20)) font = QFont() font.setPointSize(12) font.setBold(True) self.label_2.setFont(font) self.label = QLabel(self.centralwidget) self.label.setObjectName(u"label") self.label.setGeometry(QRect(31, 21, 64, 20)) self.label.setFont(font) self.label.setMouseTracking(True) self.ifLock = QCheckBox(self.centralwidget) self.ifLock.setObjectName(u"ifLock") self.ifLock.setGeometry(QRect(60, 169, 151, 20)) self.ifLock.setMouseTracking(True) self.ok = QPushButton(self.centralwidget) self.ok.setObjectName(u"ok") self.ok.setGeometry(QRect(90, 200, 71, 23)) self.splitter = QSplitter(self.centralwidget) self.splitter.setObjectName(u"splitter") self.splitter.setGeometry(QRect(61, 50, 161, 21)) self.splitter.setOrientation(Qt.Horizontal) self.label_3 = QLabel(self.splitter) self.label_3.setObjectName(u"label_3") self.splitter.addWidget(self.label_3) self.workTime = QSpinBox(self.splitter) self.workTime.setObjectName(u"workTime") self.splitter.addWidget(self.workTime) self.label_5 = QLabel(self.splitter) self.label_5.setObjectName(u"label_5") self.splitter.addWidget(self.label_5) self.splitter_2 = QSplitter(self.centralwidget) self.splitter_2.setObjectName(u"splitter_2") self.splitter_2.setGeometry(QRect(61, 79, 161, 21)) self.splitter_2.setOrientation(Qt.Horizontal) self.label_4 = QLabel(self.splitter_2) self.label_4.setObjectName(u"label_4") self.splitter_2.addWidget(self.label_4) self.restTime = QSpinBox(self.splitter_2) self.restTime.setObjectName(u"restTime") self.splitter_2.addWidget(self.restTime) self.label_6 = QLabel(self.splitter_2) self.label_6.setObjectName(u"label_6") self.splitter_2.addWidget(self.label_6) self.time = QLCDNumber(self.centralwidget) self.time.setObjectName(u"time") self.time.setGeometry(QRect(60, 240, 131, 41)) MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) QMetaObject.connectSlotsByName(MainWindow) # setupUi def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"\u4e45\u5750\u63d0\u9192", None)) self.label_2.setText(QCoreApplication.translate("MainWindow", u"\u9009\u9879\uff1a", None)) self.label.setText(QCoreApplication.translate("MainWindow", u"\u8ba1\u65f6\u5668\uff1a", None)) self.ifLock.setText(QCoreApplication.translate("MainWindow", u"\u4f11\u606f\u65f6\u5c4f\u853d\u952e\u76d8\u548c\u9f20\u6807", None)) self.ok.setText(QCoreApplication.translate("MainWindow", u"\u5f00\u59cb", None)) self.label_3.setText(QCoreApplication.translate("MainWindow", u"\u5de5\u4f5c\u65f6\u95f4\uff1a", None)) self.label_5.setText(QCoreApplication.translate("MainWindow", u"\uff08\u5206\u949f\uff09", None)) self.label_4.setText(QCoreApplication.translate("MainWindow", u"\u4f11\u606f\u65f6\u95f4\uff1a", None)) self.label_6.setText(QCoreApplication.translate("MainWindow", u"\uff08\u5206\u949f\uff09", None)) # retranslateUi
-
4、显示界面
- 使用Pycharm创建一个新的项目,并创建一个python文件。
-
from PySide6.QtWidgets import QApplication, QMainWindow from ui_SedentaryReminder import Ui_MainWindow class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.ui = Ui_MainWindow() # UI类的实例化 self.ui.setupUi(self) if __name__ == '__main__': app = QApplication([]) # 启动一个应用 window = MainWindow() # 实例化主窗口 window.show() # 展示主窗口 app.exec() # 避免程序执行到这一行后直接退出
-
- 运行显示界面。
- 【注】这只是简单地显示了UI界面,并没有具体的功能实现。
文章来源地址https://www.toymoban.com/news/detail-806786.html
文章来源:https://www.toymoban.com/news/detail-806786.html
到了这里,关于Pyside6入门教学——编写一个UI界面并显示的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!