開啟designer
1.開啟一個widget

2.用滑鼠拉一個按鈕
隨便拉一個 元件
存檔為 *.ui
3.用pyuic5 -x *.ui -o *.py
會產生 xxxx.py 檔案
內容為
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(“Form”)
Form.resize(400, 300)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(120, 70, 161, 101))
self.pushButton.setObjectName(“pushButton”)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate(“Form”, “Form”))
self.pushButton.setText(_translate(“Form”, “PushButton”))
if name == “main”:
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
4.然後寫個py呼叫 xxxx.py
做兩個動作
- from untiled import Ui_Form
- 寫一個 class
內容如下:
import sys
from PyQt5.QtWidgets import QMainWindow,QApplication
from untitled import Ui_Form
class MyDesiger(QMainWindow,Ui_Form): <----1,2
def init(self,parent=None):
super(MyDesiger,self).init(parent) <----3
self.setupUi(self)
if name == “main”:
app = QApplication(sys.argv)
ui = MyDesiger()
ui.show()
sys.exit(app.exec_())
**** 有個要注意
下面的 if __name 可以不要
後面只要
self.ui = MyDesiger()
self.ui.show()
即可
这篇博客介绍了如何使用Qt Designer创建GUI界面,并通过pyuic5将.ui文件转换为.py文件。接着,展示了如何在Python中导入和运行这个GUI,创建一个MyDesigner类来初始化和展示界面。最后,提供了调用转换后的.py文件的代码示例。
139

被折叠的 条评论
为什么被折叠?



