pyqt5作为python中编写界面的一大利器,得到了广泛的应用。不过我们在编写界面的过程中经常需要修改界面,即qtdesigner生成的ui文件,每次修改都会导致由qtdesigner的ui文件生成的python文件重新生成,这给我们在界面python文件中添加自己编写的一些代码带来了困难,因为我们每次修改界面,生成的界面python文件都会将我们自己添加的代码清空。为了在生成的界面python文件方便添加代码,我们可以通过继承的方式来实现。
我们先用qtDesigner写一个简单的界面,在Dialog中添加一个QLabel和QButton,如下:
生成的Python文件如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(150, 80, 101, 51))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(150, 200, 75, 23))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "TextLabel"))
self.pushButton.setText(_translate("Dialog", "PushButton"))
我们希望点击按钮后label上的文字变为hello world,需要添加一个函数helloworld,代码如下:
def helloworld(self):#label上显示文字hello world
self.label.setText("hello world")
然后在setupUI函数中添加一行代码:
self.pushButton.clicked.connect(self.helloworld)#将按钮点击事件和helloworld函数绑定
在界面python文件末尾添加代码使代码可运行:
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_Dialog()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
经过添加代码得到的全部Python代码如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(150, 80, 101, 51))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(150, 200, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.helloworld)#将按钮点击事件和helloworld函数绑定
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "TextLabel"))
self.pushButton.setText(_translate("Dialog", "PushButton"))
def helloworld(self):#label上显示文字hello world
self.label.setText("hello world")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_Dialog()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
运行后生成的界面如下:
点击按钮后界面如下:
很完美是不是?不过如果我们又需要在界面上添加一个QLineEdit呢?先用qtdesigner拖进一个QLineEdit,如下:
重新生成python文件如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(150, 80, 101, 51))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(150, 200, 75, 23))
self.pushButton.setObjectName("pushButton")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(130, 40, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "TextLabel"))
self.pushButton.setText(_translate("Dialog", "PushButton"))
我们发现我们当初添加的函数全部被清空!!!
那么如果我们需要频繁修改界面,岂不是要把自行添加的函数重复很多次?有没有什么好办法避免这种情况的发生呢?我们可以通过继承类的方式解决这个问题。新建一个Python文件,在其中定义一个新类myDialog,继承自创建的界面类UI_Dialog,并在其构造函数中调用代码UI_Dialog的setupUI函数,代码如下:
from PyQt5 import QtCore, QtGui, QtWidgets
import untitled
class myDialog(untitled.Ui_Dialog):
def __init__(self,Dialog):
super().setupUi(Dialog)
然后我们将自己刚刚编写的代码做过少许修改后,都放到这个新类中,代码如下:
from PyQt5 import QtCore, QtGui, QtWidgets
import untitled,sys
class myDialog(untitled.Ui_Dialog):#继承自UI_Diglog类,注意我把UI_Dialog放在了untitled.py中,如果你把这个类放在了XXX.py文件中,就应该是XXX.UI_Dialog
def __init__(self,Dialog):
super().setupUi(Dialog)#调用父类的setupUI函数
self.pushButton.clicked.connect(self.helloworld)#将按钮点击事件和helloworld函数绑定
def helloworld(self):#label上显示文字hello world
self.label.setText("hello world")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = myDialog(MainWindow)#注意把类名修改为myDialog
#ui.setupUi(MainWindow) myDialog类的构造函数已经调用了这个函数,这行代码可以删去
MainWindow.show()
sys.exit(app.exec_())
我们的父类python文件是添加了QLineEdit以后的代码,运行myDialog类所在文件,得到的界面如下:
点击按钮后:
我们再在界面文件中添加一个QLabel,如下:
生成新的界面python文件后,直接运行myDialog类所在python文件,得到的界面:
点击按钮后:
发现了吗?我们只需要生成新的界面python文件而不必做任何其他的工作就可以得到新的界面!!!
以上就是通过继承类的方法解决pyqt5中由qtdesigner的ui文件生成的python文件难以修改问题。总结来说就是在界面python文件外新建一个python文件,定义一个子类继承自界面类,然后将我们自己编写的代码统统放到这个子类中,运行时也直接运行这个子类,这样就可以保证修改界面时不会影响到我们自己添加的代码。
如果还有什么疑问,可以在评论区中留言哦~~ღ( ´・ᴗ・` )比心