当前位置:   article > 正文

Pyside6入门教学——编写一个UI界面并显示_pyside6教程

pyside6教程

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文件。
      1. # -*- coding: utf-8 -*-
      2. ################################################################################
      3. ## Form generated from reading UI file 'SedentaryReminder.ui'
      4. ##
      5. ## Created by: Qt User Interface Compiler version 6.6.1
      6. ##
      7. ## WARNING! All changes made in this file will be lost when recompiling UI file!
      8. ################################################################################
      9. from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
      10. QMetaObject, QObject, QPoint, QRect,
      11. QSize, QTime, QUrl, Qt)
      12. from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
      13. QFont, QFontDatabase, QGradient, QIcon,
      14. QImage, QKeySequence, QLinearGradient, QPainter,
      15. QPalette, QPixmap, QRadialGradient, QTransform)
      16. from PySide6.QtWidgets import (QApplication, QCheckBox, QLCDNumber, QLabel,
      17. QMainWindow, QPushButton, QSizePolicy, QSpinBox,
      18. QSplitter, QWidget)
      19. class Ui_MainWindow(object):
      20. def setupUi(self, MainWindow):
      21. if not MainWindow.objectName():
      22. MainWindow.setObjectName(u"MainWindow")
      23. MainWindow.resize(260, 300)
      24. MainWindow.setMinimumSize(QSize(260, 300))
      25. MainWindow.setMaximumSize(QSize(260, 300))
      26. self.centralwidget = QWidget(MainWindow)
      27. self.centralwidget.setObjectName(u"centralwidget")
      28. self.label_2 = QLabel(self.centralwidget)
      29. self.label_2.setObjectName(u"label_2")
      30. self.label_2.setGeometry(QRect(37, 143, 48, 20))
      31. font = QFont()
      32. font.setPointSize(12)
      33. font.setBold(True)
      34. self.label_2.setFont(font)
      35. self.label = QLabel(self.centralwidget)
      36. self.label.setObjectName(u"label")
      37. self.label.setGeometry(QRect(31, 21, 64, 20))
      38. self.label.setFont(font)
      39. self.label.setMouseTracking(True)
      40. self.ifLock = QCheckBox(self.centralwidget)
      41. self.ifLock.setObjectName(u"ifLock")
      42. self.ifLock.setGeometry(QRect(60, 169, 151, 20))
      43. self.ifLock.setMouseTracking(True)
      44. self.ok = QPushButton(self.centralwidget)
      45. self.ok.setObjectName(u"ok")
      46. self.ok.setGeometry(QRect(90, 200, 71, 23))
      47. self.splitter = QSplitter(self.centralwidget)
      48. self.splitter.setObjectName(u"splitter")
      49. self.splitter.setGeometry(QRect(61, 50, 161, 21))
      50. self.splitter.setOrientation(Qt.Horizontal)
      51. self.label_3 = QLabel(self.splitter)
      52. self.label_3.setObjectName(u"label_3")
      53. self.splitter.addWidget(self.label_3)
      54. self.workTime = QSpinBox(self.splitter)
      55. self.workTime.setObjectName(u"workTime")
      56. self.splitter.addWidget(self.workTime)
      57. self.label_5 = QLabel(self.splitter)
      58. self.label_5.setObjectName(u"label_5")
      59. self.splitter.addWidget(self.label_5)
      60. self.splitter_2 = QSplitter(self.centralwidget)
      61. self.splitter_2.setObjectName(u"splitter_2")
      62. self.splitter_2.setGeometry(QRect(61, 79, 161, 21))
      63. self.splitter_2.setOrientation(Qt.Horizontal)
      64. self.label_4 = QLabel(self.splitter_2)
      65. self.label_4.setObjectName(u"label_4")
      66. self.splitter_2.addWidget(self.label_4)
      67. self.restTime = QSpinBox(self.splitter_2)
      68. self.restTime.setObjectName(u"restTime")
      69. self.splitter_2.addWidget(self.restTime)
      70. self.label_6 = QLabel(self.splitter_2)
      71. self.label_6.setObjectName(u"label_6")
      72. self.splitter_2.addWidget(self.label_6)
      73. self.time = QLCDNumber(self.centralwidget)
      74. self.time.setObjectName(u"time")
      75. self.time.setGeometry(QRect(60, 240, 131, 41))
      76. MainWindow.setCentralWidget(self.centralwidget)
      77. self.retranslateUi(MainWindow)
      78. QMetaObject.connectSlotsByName(MainWindow)
      79. # setupUi
      80. def retranslateUi(self, MainWindow):
      81. MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"\u4e45\u5750\u63d0\u9192", None))
      82. self.label_2.setText(QCoreApplication.translate("MainWindow", u"\u9009\u9879\uff1a", None))
      83. self.label.setText(QCoreApplication.translate("MainWindow", u"\u8ba1\u65f6\u5668\uff1a", None))
      84. self.ifLock.setText(QCoreApplication.translate("MainWindow", u"\u4f11\u606f\u65f6\u5c4f\u853d\u952e\u76d8\u548c\u9f20\u6807", None))
      85. self.ok.setText(QCoreApplication.translate("MainWindow", u"\u5f00\u59cb", None))
      86. self.label_3.setText(QCoreApplication.translate("MainWindow", u"\u5de5\u4f5c\u65f6\u95f4\uff1a", None))
      87. self.label_5.setText(QCoreApplication.translate("MainWindow", u"\uff08\u5206\u949f\uff09", None))
      88. self.label_4.setText(QCoreApplication.translate("MainWindow", u"\u4f11\u606f\u65f6\u95f4\uff1a", None))
      89. self.label_6.setText(QCoreApplication.translate("MainWindow", u"\uff08\u5206\u949f\uff09", None))
      90. # retranslateUi

4、显示界面

  • 使用Pycharm创建一个新的项目,并创建一个python文件。
      1. from PySide6.QtWidgets import QApplication, QMainWindow
      2. from ui_SedentaryReminder import Ui_MainWindow
      3. class MainWindow(QMainWindow):
      4. def __init__(self):
      5. super(MainWindow, self).__init__()
      6. self.ui = Ui_MainWindow() # UI类的实例化
      7. self.ui.setupUi(self)
      8. if __name__ == '__main__':
      9. app = QApplication([]) # 启动一个应用
      10. window = MainWindow() # 实例化主窗口
      11. window.show() # 展示主窗口
      12. app.exec() # 避免程序执行到这一行后直接退出
  • 运行显示界面。 
    • 【注】这只是简单地显示了UI界面,并没有具体的功能实现。
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/962985
推荐阅读
相关标签
  

闽ICP备14008679号