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.
QTdesigner
5.在Project目录中将生成Ui_example01.py
QTdesigner
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_())  

相关文章:

  • 2021-10-15
  • 2021-04-09
  • 2022-12-23
  • 2021-12-28
  • 2021-09-16
  • 2022-12-23
  • 2021-07-06
  • 2021-04-11
猜你喜欢
  • 2022-01-08
  • 2021-04-01
  • 2021-08-11
  • 2022-01-17
  • 2021-04-28
  • 2021-09-18
  • 2022-12-23
相关资源
相似解决方案