利用pyuic5将ui文件转换为py文件

操作系统上正确配置python环境之后,pyuic5也是一个可以识别的命令行指令

到.ui文件的目录下,直接cmd进入,输入pyuic5 -o 转换的py文件 待转换的ui文件

此时,需要对login.py添加一点代码使得设计好的UI能够出现在我们面前

 
  1. import sys
  2. if __name__ == "__main__":
  3. app = QtWidgets.QApplication(sys.argv) # 创建一个QApplication,也就是你要开发的软件app
  4. MainWindow = QtWidgets.QMainWindow() # 创建一个QMainWindow,用来装载你需要的各种组件、控件
  5. ui = Ui_Form() # ui是你创建的ui类的实例化对象
  6. ui.setupUi(MainWindow) # 执行类中的setupUi方法,方法的参数是第二步中创建的QMainWindow
  7. MainWindow.show() # 执行QMainWindow的show()方法,显示这个QMainWindow
  8. sys.exit(app.exec_()) # 使用exit()或者点击关闭按钮退出QApplication
完整代码段如下:
  1. # -*- coding: utf-8 -*-
  2. # Form implementation generated from reading ui file 'login.ui'
  3. #
  4. # Created by: PyQt5 UI code generator 5.6
  5. #
  6. # WARNING! All changes made in this file will be lost!
  7. from PyQt5 import QtCore, QtGui, QtWidgets
  8. import sys
  9. class Ui_Form(object):
  10. def setupUi(self, Form):
  11. Form.setObjectName("Form")
  12. Form.resize(400, 300)
  13. self.pushButton = QtWidgets.QPushButton(Form)
  14. self.pushButton.setGeometry(QtCore.QRect(70, 220, 75, 23))
  15. self.pushButton.setObjectName("pushButton")
  16. self.pushButton_2 = QtWidgets.QPushButton(Form)
  17. self.pushButton_2.setGeometry(QtCore.QRect(220, 220, 75, 23))
  18. self.pushButton_2.setObjectName("pushButton_2")
  19. self.checkBox = QtWidgets.QCheckBox(Form)
  20. self.checkBox.setGeometry(QtCore.QRect(70, 180, 141, 16))
  21. self.checkBox.setObjectName("checkBox")
  22. self.lineEdit = QtWidgets.QLineEdit(Form)
  23. self.lineEdit.setGeometry(QtCore.QRect(130, 56, 181, 20))
  24. self.lineEdit.setObjectName("lineEdit")
  25. self.lineEdit_2 = QtWidgets.QLineEdit(Form)
  26. self.lineEdit_2.setGeometry(QtCore.QRect(130, 110, 181, 20))
  27. self.lineEdit_2.setObjectName("lineEdit_2")
  28. self.label = QtWidgets.QLabel(Form)
  29. self.label.setGeometry(QtCore.QRect(70, 60, 54, 12))
  30. self.label.setObjectName("label")
  31. self.label_2 = QtWidgets.QLabel(Form)
  32. self.label_2.setGeometry(QtCore.QRect(70, 110, 54, 12))
  33. self.label_2.setObjectName("label_2")
  34. self.retranslateUi(Form)
  35. QtCore.QMetaObject.connectSlotsByName(Form)
  36. def retranslateUi(self, Form):
  37. _translate = QtCore.QCoreApplication.translate
  38. Form.setWindowTitle(_translate("Form", "Form"))
  39. self.pushButton.setText(_translate("Form", "取消"))
  40. self.pushButton_2.setText(_translate("Form", "确定"))
  41. self.checkBox.setText(_translate("Form", "记住用户名和密码"))
  42. self.label.setText(_translate("Form", "用户名:"))
  43. self.label_2.setText(_translate("Form", "密码:"))
  44. if __name__ == "__main__":
  45. app = QtWidgets.QApplication(sys.argv) # 创建一个QApplication,也就是你要开发的软件app
  46. MainWindow = QtWidgets.QMainWindow() # 创建一个QMainWindow,用来装载你需要的各种组件、控件
  47. ui = Ui_Form() # ui是你创建的ui类的实例化对象
  48. ui.setupUi(MainWindow) # 执行类中的setupUi方法,方法的参数是第二步中创建的QMainWindow
  49. MainWindow.show() # 执行QMainWindow的show()方法,显示这个QMainWindow
  50. sys.exit(app.exec_()) # 使用exit()或者点击关闭按钮退出QApplication
结果显示如下:
(0)

相关推荐