模板一
构造函数和主方法部分都不用更改,只需要在QT Designer部分生成ui文件,然后在槽函数链接部分进行函数链接就可以,优点是高度模块化,代码很简洁,很适合和大型项目。缺点是,理解起来可能抽象一点点~
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton
import sys
from PyQt5 import uic
class Ui_Form(QWidget):
#构造函数
def __init__(self):
super(Ui_Form, self).__init__()
#self.setupUi()
self.set_ui()
#导入ui文件
def set_ui(self):
uic.loadUi('5.6_test.ui',self)
#槽函数的链接
@QtCore.pyqtSlot()
def on_okbt_clicked(self):
print('ok')
# 主方法,程序从此处启动PyQt设计的窗体
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ui = Ui_Form() # 创建PyQt设计的窗体对象
ui.show()
sys.exit(app.exec_()) # 程序关闭时退出进程
#############################################################################
模板二(汪哥教我的,建议汪哥开个班,我跪着听,/狗头)
先用PYUIC(外部工具)生成一个py文件
还是先用PYUIC(外部工具)生成一个py文件,注意要建立信号和槽的联系,这样就会自动生成链接,方法如下图:
生成的文件如下,有细心的朋友可能注意到了,我上图新建的函数是on_okbt_clicked,但是我链接的时候改成了okbt_clicked,这是因为如果使用on_okbt_clicked,它会按照名字自动链接(可能是因为有一句QtCore.QMetaObject.connectSlotsByName(Form)),实际上使用的时候会print 3 次ok,所以起名字的时候不能这么取。
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.okbt = QtWidgets.QPushButton(Form)
self.okbt.setGeometry(QtCore.QRect(100, 170, 56, 17))
self.okbt.setObjectName("okbt")
self.lcd_num = QtWidgets.QLCDNumber(Form)
self.lcd_num.setGeometry(QtCore.QRect(200, 160, 64, 23))
self.lcd_num.setObjectName("lcd_num")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(140, 90, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Form)
self.okbt.clicked.connect(Form.okbt_clicked) # type: ignore
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.okbt.setText(_translate("Form", "ok_bt"))
然后再写一个main.py文件,把生成的类继承一下
from test import Ui_Form
import sys
from PyQt5.QtWidgets import *
class main_form(QMainWindow,Ui_Form):
def __init__(self):
super(main_form,self).__init__()
self.setupUi(self)
def okbt_clicked(self):
print('ok')
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = main_form() # 创建PyQt设计的窗体对象
ui.show()
sys.exit(app.exec_()) # 程序关闭时退出进程
#############################################################################
模板三(这种方法每次修改ui界面的时候都得再改文件,写法也很累赘,都在一个文件夹里面,但是Qt实例书里面就用的这种,程序比较简单的时候其实这样写也还可以,做个反面教材吧)
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.okbt = QtWidgets.QPushButton(Form)
self.okbt.setGeometry(QtCore.QRect(100, 160, 56, 17))
self.okbt.setObjectName("okbt")
self.lcd_num = QtWidgets.QLCDNumber(Form)
self.lcd_num.setGeometry(QtCore.QRect(200, 160, 64, 23))
self.lcd_num.setObjectName("lcd_num")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(140, 90, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.okbt.setText(_translate("Form", "ok_bt"))
可视化:
在代码上添加main函数和槽链接
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import QApplication, QWidget
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.okbt = QtWidgets.QPushButton(Form)
self.okbt.setGeometry(QtCore.QRect(100, 160, 56, 17))
self.okbt.setObjectName("okbt")
self.lcd_num = QtWidgets.QLCDNumber(Form)
self.lcd_num.setGeometry(QtCore.QRect(200, 160, 64, 23))
self.lcd_num.setObjectName("lcd_num")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(140, 90, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
#增加1
self.okbt.clicked.connect(lambda:self.on_okbt_clicked())#注意要用lambda定义一下,不然会报错
#槽函数的链接(有修改)
#@QtCore.pyqtSlot()#这里这种方式就链接不了了
def on_okbt_clicked(self):
print('ok')
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.okbt.setText(_translate("Form", "ok_bt"))
#增加的语句
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow() # 创建窗体对象
ui = Ui_Form() # 创建PyQt设计的窗体对象
ui.setupUi(MainWindow) # 调用PyQt窗体的方法对窗体对象进行初始化设置
MainWindow.show() # 显示窗体
sys.exit(app.exec_()) # 程序关闭时退出进程