開啟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()
即可