当前位置:   article > 正文

QtDeisgner,PyUic详细使用教程界面和函数逻辑分离(保姆教学)

pyuic

关于QtDesigner可以pip也可以GitHub下载第三方的版本

                QtDesigner第三方插件版https://github.com/PyQt5/QtDesigner/releases

可以直接生成PyQt代码,省去PyUic

视频教学QtDesigner教程-界面和函数逻辑分离-实时修改布局_哔哩哔哩_bilibili

 首先打开QtDesigner生成好一个窗口,然后保存文件

这里是拍视频教学剩下的

 打开PyCharm找到Ui文件通过PyUic转成Py文件

QtDesigner插件版内部也是支持直接复制代码的,新建一个文件粘贴上去就好了

这一步就是Ui文件转成Py文件就行了

导入所需的东西

  1. import PyQt5
  2. from PyQt5.QtWidgets import QMainWindow
  3. from untitled import Ui_Form

然后新建一个Py文件,在里边定义一个类继承QMainWindow和你的布局类(打开UI转换的Py文件就可以看到类名)

class MyGui(QMainWindow,Ui_Form):

然后在类里边定义

  1. def __init__(self):
  2. super().__init__()
  3. self.setupUi(self)

这样就初始化了你的窗口布局

然后最后边写上你的主入口

  1. while True
  2. 或者
  3. if __name__ == '__main__':
  4. 甚至可以不写

推荐第二种写法

  1. import sys
  2. app = PyQt5.QtWidgets.QApplication(sys.argv)
  3. MyUiStart = MyGui()
  4. MyUiStart.show()# ui就会显示出来
  5. sys.exit(app.exec_())

然后就是加载主进程了你可以理解为这个程序一直在重复在窗口布局

这里MyGui是你定义的类名

最后你的界面就能显示出来了,并且实现了逻辑分离

更新布局的话只要用QtDesigner打开Ui文件然后修改内容把原来的布局代码覆盖掉就行了

完整的代码

布局代码

  1. # -*- coding: utf-8 -*-
  2. # Form implementation generated from reading ui file 'untitled.ui'
  3. #
  4. # Created by: PyQt5 UI code generator 5.15.4
  5. #
  6. # WARNING: Any manual changes made to this file will be lost when pyuic5 is
  7. # run again. Do not edit this file unless you know what you are doing.
  8. from PyQt5 import QtCore, QtGui, QtWidgets
  9. class Ui_Form(object):
  10. def setupUi(self, Form):
  11. Form.setObjectName("Form")
  12. Form.resize(1100, 857)
  13. self.textBrowser = QtWidgets.QTextBrowser(Form)
  14. self.textBrowser.setGeometry(QtCore.QRect(230, 210, 651, 201))
  15. self.textBrowser.setObjectName("textBrowser")
  16. self.retranslateUi(Form)
  17. QtCore.QMetaObject.connectSlotsByName(Form)
  18. def retranslateUi(self, Form):
  19. _translate = QtCore.QCoreApplication.translate
  20. Form.setWindowTitle(_translate("Form", "Form"))
  21. self.textBrowser.setHtml(_translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
  22. "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
  23. "p, li { white-space: pre-wrap; }\n"
  24. "</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
  25. "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:48pt; font-weight:600; font-style:italic;\">Hi My Firends</span></p></body></html>"))

主程序代码

  1. # 首先导入布局类
  2. import PyQt5
  3. from PyQt5.QtWidgets import QMainWindow
  4. from untitled import Ui_Form
  5. # 这样就能继承这个窗口
  6. # 这两个个是窗口必须要继承的
  7. class MyGui(QMainWindow,Ui_Form):
  8. # 初始化窗口
  9. def __init__(self):
  10. super().__init__()
  11. self.setupUi(self)
  12. if __name__ == '__main__':
  13. import sys
  14. app = PyQt5.QtWidgets.QApplication(sys.argv)
  15. MyUiStart = MyGui()
  16. MyUiStart.show()# ui就会显示出来
  17. sys.exit(app.exec_())

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/76133
推荐阅读
  

闽ICP备14008679号