当前位置:   article > 正文

将Qt Designer的ui文件转换为PySide2使用的.py文件_qt designer导出成.py

qt designer导出成.py

20201206修订:在Pyside2的5.15.2版本中,从ui生成py文件过程,命令从“uic”变为“pyside2-uic”

Qt Designer 设计的ui文件,就是一个xml文件,通过pyside-uic可以转换成标准的.py文件 。通过QtDesigner创建的ui如图:

我们切入正题:

1:首先要保证将pyside2-uic.exe加到环境变量Path中。根据我的Python,PySide2安装路径。设置具体如下图:

                       

2:使用PySide2中的QtDesigner工具创建具体的ui文件。QtDesigner路径如下:

3:将创建的ui文件使用命令生成对应的.py文件。先从系统的cmd进入到ui文件的位置,然后输入命令:

pyside2-uic ****.ui -o ****.py

进行文件转换。注:PySide2 5.15.2版本中“pyside2-uic”变为“uic”,但是cmd中的命令uic无效,只能用“pyside2-uic”命令

运行正常。生成的py文件代码如下:

  1. # -*- coding: utf-8 -*-
  2. # Form implementation generated from reading ui file 'untitled.ui',
  3. # licensing of 'untitled.ui' applies.
  4. #
  5. # Created: Mon May 11 20:42:28 2020
  6. # by: pyside2-uic running on PySide2 5.12.6
  7. #
  8. # WARNING! All changes made in this file will be lost!
  9. from PySide2 import QtCore, QtGui, QtWidgets
  10. class Ui_MainWindow(object):
  11. def setupUi(self, MainWindow):
  12. MainWindow.setObjectName("MainWindow")
  13. MainWindow.resize(800, 600)
  14. self.centralwidget = QtWidgets.QWidget(MainWindow)
  15. self.centralwidget.setObjectName("centralwidget")
  16. self.widget = QtWidgets.QWidget(self.centralwidget)
  17. self.widget.setGeometry(QtCore.QRect(0, 0, 801, 571))
  18. self.widget.setObjectName("widget")
  19. self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
  20. self.verticalLayout.setContentsMargins(0, 0, 0, 0)
  21. self.verticalLayout.setObjectName("verticalLayout")
  22. self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
  23. self.horizontalLayout_2.setObjectName("horizontalLayout_2")
  24. self.horizontalLayout = QtWidgets.QHBoxLayout()
  25. self.horizontalLayout.setObjectName("horizontalLayout")
  26. self.pushButton = QtWidgets.QPushButton(self.widget)
  27. self.pushButton.setObjectName("pushButton")
  28. self.horizontalLayout.addWidget(self.pushButton)
  29. self.pushButton_2 = QtWidgets.QPushButton(self.widget)
  30. self.pushButton_2.setObjectName("pushButton_2")
  31. self.horizontalLayout.addWidget(self.pushButton_2)
  32. self.horizontalLayout_2.addLayout(self.horizontalLayout)
  33. self.label = QtWidgets.QLabel(self.widget)
  34. self.label.setObjectName("label")
  35. self.horizontalLayout_2.addWidget(self.label)
  36. self.verticalLayout.addLayout(self.horizontalLayout_2)
  37. self.plainTextEdit = QtWidgets.QPlainTextEdit(self.widget)
  38. self.plainTextEdit.setObjectName("plainTextEdit")
  39. self.verticalLayout.addWidget(self.plainTextEdit)
  40. MainWindow.setCentralWidget(self.centralwidget)
  41. self.menubar = QtWidgets.QMenuBar(MainWindow)
  42. self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
  43. self.menubar.setObjectName("menubar")
  44. MainWindow.setMenuBar(self.menubar)
  45. self.statusbar = QtWidgets.QStatusBar(MainWindow)
  46. self.statusbar.setObjectName("statusbar")
  47. MainWindow.setStatusBar(self.statusbar)
  48. self.retranslateUi(MainWindow)
  49. QtCore.QMetaObject.connectSlotsByName(MainWindow)
  50. def retranslateUi(self, MainWindow):
  51. MainWindow.setWindowTitle(QtWidgets.QApplication.translate("MainWindow", "MainWindow", None, -1))
  52. self.pushButton.setText(QtWidgets.QApplication.translate("MainWindow", "PushButton", None, -1))
  53. self.pushButton_2.setText(QtWidgets.QApplication.translate("MainWindow", "PushButton", None, -1))
  54. self.label.setText(QtWidgets.QApplication.translate("MainWindow", "TextLabel", None, -1))

4:然后将生成的.py文件放到具体路径下,通过py进行加载,具体示例代码如下:

  1. import sys
  2. from PySide2.QtWidgets import QApplication
  3. from PySide2.QtWidgets import QMainWindow
  4. from ch06.Untitled import Ui_MainWindow
  5. class LoginForm(QMainWindow):
  6. def __init__(self, parent=None):
  7. QMainWindow.__init__(self, parent)
  8. self.ui = Ui_MainWindow()
  9. self.ui.setupUi(self)
  10. if __name__ == '__main__':
  11. app = QApplication(sys.argv)
  12. form = LoginForm()
  13. form.show()
  14. sys.exit(app.exec_())

运行,结果如下图:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/651825
推荐阅读
相关标签
  

闽ICP备14008679号