pyqt5作為python中編寫界面的一大利器,得到了廣泛的應(yīng)用。不過(guò)我們?cè)诰帉懡缑娴倪^(guò)程中經(jīng)常需要修改界面,即qtdesigner生成的ui文件,每次修改都會(huì)導(dǎo)致由qtdesigner的ui文件生成的python文件重新生成,這給我們?cè)诮缑鎝ython文件中添加自己編寫的一些代碼帶來(lái)了困難,因?yàn)槲覀兠看涡薷慕缑妫傻慕缑鎝ython文件都會(huì)將我們自己添加的代碼清空。為了在生成的界面python文件方便添加代碼,我們可以通過(guò)繼承的方式來(lái)實(shí)現(xiàn)。
我們先用qtDesigner寫一個(gè)簡(jiǎn)單的界面,在Dialog中添加一個(gè)QLabel和QButton,如下:
# -*- 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"))
我們希望點(diǎn)擊按鈕后label上的文字變?yōu)閔ello world,需要添加一個(gè)函數(shù)helloworld,代碼如下:
def helloworld(self):#label上顯示文字hello world self.label.setText("hello world")
然后在setupUI函數(shù)中添加一行代碼:
self.pushButton.clicked.connect(self.helloworld)#將按鈕點(diǎn)擊事件和helloworld函數(shù)綁定
在界面python文件末尾添加代碼使代碼可運(yùn)行:
if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_Dialog() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
經(jīng)過(guò)添加代碼得到的全部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)#將按鈕點(diǎn)擊事件和helloworld函數(shù)綁定 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_())
運(yùn)行后生成的界面如下:
# -*- 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"))
我們發(fā)現(xiàn)我們當(dāng)初添加的函數(shù)全部被清空?。。。ㄈ绻拖朐趗i生成的py文件中編輯自定義函數(shù),可查看Pycharm 如何查看代碼修改歷史|回滾代碼)
那么如果我們需要頻繁修改界面,豈不是要把自行添加的函數(shù)重復(fù)很多次?有沒(méi)有什么好辦法避免這種情況的發(fā)生呢?我們可以通過(guò)繼承類的方式解決這個(gè)問(wèn)題。新建一個(gè)Python文件,在其中定義一個(gè)新類myDialog,繼承自創(chuàng)建的界面類UI_Dialog,并在其構(gòu)造函數(shù)中調(diào)用代碼UI_Dialog的setupUI函數(shù),代碼如下:
from PyQt5 import QtCore, QtGui, QtWidgets import untitled class myDialog(untitled.Ui_Dialog): def __init__(self,Dialog): super().setupUi(Dialog)
然后我們將自己剛剛編寫的代碼做過(guò)少許修改后,都放到這個(gè)新類中,代碼如下:
from PyQt5 import QtCore, QtGui, QtWidgets import untitled,sys class myDialog(untitled.Ui_Dialog):#繼承自UI_Diglog類,注意我把UI_Dialog放在了untitled.py中,如果你把這個(gè)類放在了XXX.py文件中,就應(yīng)該是XXX.UI_Dialog def __init__(self,Dialog): super().setupUi(Dialog)#調(diào)用父類的setupUI函數(shù) self.pushButton.clicked.connect(self.helloworld)#將按鈕點(diǎn)擊事件和helloworld函數(shù)綁定 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類的構(gòu)造函數(shù)已經(jīng)調(diào)用了這個(gè)函數(shù),這行代碼可以刪去 MainWindow.show() sys.exit(app.exec_())
我們的父類python文件是添加了QLineEdit以后的代碼,運(yùn)行myDialog類所在文件,得到的界面如下:
以上就是通過(guò)繼承類的方法解決pyqt5中由qtdesigner的ui文件生成的python文件難以修改問(wèn)題??偨Y(jié)來(lái)說(shuō)就是在界面python文件外新建一個(gè)python文件,定義一個(gè)子類繼承自界面類,然后將我們自己編寫的代碼統(tǒng)統(tǒng)放到這個(gè)子類中,運(yùn)行時(shí)也直接運(yùn)行這個(gè)子類,這樣就可以保證修改界面時(shí)不會(huì)影響到我們自己添加的代碼。
感謝原文博主「日沉云起」
————————————————
版權(quán)聲明:本文為CSDN博主「日沉云起」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/richenyunqi/article/details/80589569
聯(lián)系客服