关于QtDesigner可以pip也可以GitHub下载第三方的版本
QtDesigner第三方插件版https://github.com/PyQt5/QtDesigner/releases
可以直接生成PyQt代码,省去PyUic
视频教学QtDesigner教程-界面和函数逻辑分离-实时修改布局_哔哩哔哩_bilibili
首先打开QtDesigner生成好一个窗口,然后保存文件
这里是拍视频教学剩下的
打开PyCharm找到Ui文件通过PyUic转成Py文件
QtDesigner插件版内部也是支持直接复制代码的,新建一个文件粘贴上去就好了
这一步就是Ui文件转成Py文件就行了
导入所需的东西
import PyQt5
from PyQt5.QtWidgets import QMainWindow
from untitled import Ui_Form
然后新建一个Py文件,在里边定义一个类继承QMainWindow和你的布局类(打开UI转换的Py文件就可以看到类名)
class MyGui(QMainWindow,Ui_Form):
然后在类里边定义
def __init__(self):
super().__init__()
self.setupUi(self)
这样就初始化了你的窗口布局
然后最后边写上你的主入口
while True
或者
if __name__ == '__main__':
甚至可以不写
推荐第二种写法
import sys
app = PyQt5.QtWidgets.QApplication(sys.argv)
MyUiStart = MyGui()
MyUiStart.show()# ui就会显示出来
sys.exit(app.exec_())
然后就是加载主进程了你可以理解为这个程序一直在重复在窗口布局
这里MyGui是你定义的类名
最后你的界面就能显示出来了,并且实现了逻辑分离
更新布局的话只要用QtDesigner打开Ui文件然后修改内容把原来的布局代码覆盖掉就行了
完整的代码
布局代码
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1100, 857)
self.textBrowser = QtWidgets.QTextBrowser(Form)
self.textBrowser.setGeometry(QtCore.QRect(230, 210, 651, 201))
self.textBrowser.setObjectName("textBrowser")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.textBrowser.setHtml(_translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<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>"))
主程序代码
# 首先导入布局类
import PyQt5
from PyQt5.QtWidgets import QMainWindow
from untitled import Ui_Form
# 这样就能继承这个窗口
# 这两个个是窗口必须要继承的
class MyGui(QMainWindow,Ui_Form):
# 初始化窗口
def __init__(self):
super().__init__()
self.setupUi(self)
if __name__ == '__main__':
import sys
app = PyQt5.QtWidgets.QApplication(sys.argv)
MyUiStart = MyGui()
MyUiStart.show()# ui就会显示出来
sys.exit(app.exec_())