1.运行QtDesigner:在Pycharm中打开Tools->External Tools->QtDesigner
2. 创新一个Widget
3. 拖一个PushButton到Widget中,将Text属性设置为"Quit"
3. 另存为example01.ui
4. 在Pycharm的Project中,选中
example01.ui,按右键,选择
External Tools->Pyuic5.
5.在Project目录中将生成Ui_example01.py
Ui_example01.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'example01.ui' # # Created by: PyQt5 UI code generator 5.6 # # WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets #导入模块
class Ui_Form(object): #建立窗口类,继承object
def setupUi(self, Form):
Form.setObjectName("Form") #设置窗口名
Form.resize(400, 300) #设置窗口大小
self.quitButton = QtWidgets.QPushButton(Form) #建立一个按钮,并将按钮加入到窗口Form中
self.quitButton.setGeometry(QtCore.QRect(280, 240, 75, 23)) #设置按钮大小与位置
self.quitButton.setObjectName("quitButton") #设置按钮名
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form) #关联信号槽
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Test")) #设置窗口标题
self.quitButton.setText(_translate("Form", "Quit")) #设置按钮显示文字
新建一个文件,导入咱们设计的
Ui_example01.py
文件,实现代码与界面分离。
from PyQt5 import QtWidgets
from Ui_example01 import Ui_Form
class mywindow(QtWidgets.QWidget):
def __init__(self):
super(mywindow,self).__init__()
self.ui=Ui_Form()
self.ui.setupUi(self)
if __name__=="__main__":
import sys
app=QtWidgets.QApplication(sys.argv)
myshow=mywindow()
myshow.show()
sys.exit(app.exec_())
直接继承界面类
from PyQt5 import QtWidgets
from untitled import Ui_Form
class mywindow(QtWidgets.QWidget,Ui_Form):
def __init__(self):
super(mywindow,self).__init__()
self.setupUi(self)
if __name__=="__main__":
import sys
app=QtWidgets.QApplication(sys.argv)
myshow=mywindow()
myshow.show()
sys.exit(app.exec_())