综述:在编写GUI,图形和逻辑分离的程序代码中,图形界面代码作父类,逻辑代码中写子类继承。逻辑代码文件中导入图形界面代码模块。在父类中写信号连接槽函数,在子类中写槽函数。在子类逻辑中修改父类图形界面。下面分几条扩展下心得,分享给大家并希望指出有待完善的问题。
1. 文件结构图下图,1为逻辑文件,gui 为界面文件,没有建__init__.py文件,目前没发现有什么影响。
2. 父类程序代码中连接语句如下,tb为工具栏的实例化对象,self.tb_cmd是判断点击的工具栏命令槽函数,注意此函数写在逻辑文件中的继承类中,作为子类的扩展方法,并可判断后继续写其他代码。
self.tb.actionTriggered[QAction].connect(self.tb_cmd)
3. 子类继承父类属性后,使用控件对象的自带方法给增加下拉选项内容,代码如下。其中self.vbo_path_0是读取文件后,取第一个文件路径,传入方法self.channelnames()中,获取通道名称,然后返回赋值给self.col_names属性,使用继承的属性comboBox_1的自带方法.addItems()改变了下拉选项。
self.col_names = self.channelnames(self.vbo_path_0)
self.comboBox_1.addItems(self.col_names)
self.comboBox_2.addItems(self.col_names)
self.comboBox_3.addItems(self.col_names)
self.comboBox_4.addItems(self.col_names)
self.comboBox_5.addItems(self.col_names)
self.comboBox_6.addItems(self.col_names)
self.comboBox_7.addItems(self.col_names)
self.comboBox_8.addItems(self.col_names)
4. 注意逻辑文件中导入import要有导入自写模块代码所附带的包,在copy方法时也要注意import包。执行界面如下图
5. 全部代码如下,主要看注释的语句,图形界面的父类是QtDesigner改写出来的。安装好包,可以run。
图形界面文件源代码
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction
import sys
class Ui_MainWindow(QMainWindow):
def __init__(self):
print('实例化类:class Ui_MainWindow')
print('*'*100)
super(QMainWindow, self).__init__()
self.x1_comboBox = 30
self.x2_comboBox = 150
self.x3_comboBox = 390
self.x4_comboBox = 510
self.y1_comboBox = 280
self.y2_comboBox = 320
self.setupUi(self)
self.retranslateUi(self)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setWindowTitle("MainWindow")
MainWindow.resize(642, 398)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
MainWindow.setFont(font)
self.centralwidget = QtWidgets.QWidget(MainWindow)
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.centralwidget.setFont(font)
self.centralwidget.setToolTipDuration(5)
self.centralwidget.setObjectName("centralwidget")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(10, 11, 621, 211))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setItalic(False)
font.setUnderline(True)
font.setWeight(75)
self.textBrowser.setFont(font)
self.textBrowser.setObjectName("textBrowser")
self.line = QtWidgets.QFrame(self.centralwidget)
self.line.setGeometry(QtCore.QRect(10, 220, 621, 20))
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.comboBox_1 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_1.setGeometry(QtCore.QRect(self.x1_comboBox, self.y1_comboBox, 110, 20))
font = QtGui.QFont()
font.setPointSize(13)
font.setBold(True)
font.setWeight(75)
self.comboBox_1.setFont(font)
self.comboBox_1.setObjectName("comboBox_1")
self.comboBox_1.addItem("")
self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_2.setGeometry(QtCore.QRect(self.x1_comboBox, self.y2_comboBox, 110, 20))
font = QtGui.QFont()
font.setPointSize(13)
font.setBold(True)
font.setWeight(75)
self.comboBox_2.setFont(font)
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
self.comboBox_3 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_3.setGeometry(QtCore.QRect(self.x2_comboBox, self.y1_comboBox, 110, 20))
font = QtGui.QFont()
font.setPointSize(13)
font.setBold(True)
font.setWeight(75)
self.comboBox_3.setFont(font)
self.comboBox_3.setObjectName("comboBox_3")
self.comboBox_3.addItem("")
self.comboBox_4 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_4.setGeometry(QtCore.QRect(self.x2_comboBox, self.y2_comboBox, 110, 20))
font = QtGui.QFont()
font.setPointSize(13)
font.setBold(True)
font.setWeight(75)
self.comboBox_4.setFont(font)
self.comboBox_4.setObjectName("comboBox_4")
self.comboBox_4.addItem("")
self.comboBox_5 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_5.setGeometry(QtCore.QRect(self.x3_comboBox, self.y1_comboBox, 110, 20))
font = QtGui.QFont()
font.setPointSize(13)
font.setBold(True)
font.setWeight(75)
self.comboBox_5.setFont(font)
self.comboBox_5.setObjectName("comboBox_5")
self.comboBox_5.addItem("")
self.comboBox_6 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_6.setGeometry(QtCore.QRect(self.x3_comboBox, self.y2_comboBox, 110, 20))
font = QtGui.QFont()
font.setPointSize(13)
font.setBold(True)
font.setWeight(75)
self.comboBox_6.setFont(font)
self.comboBox_6.setObjectName("comboBox_6")
self.comboBox_6.addItem("")
self.comboBox_7 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_7.setGeometry(QtCore.QRect(self.x4_comboBox, self.y1_comboBox, 110, 20))
font = QtGui.QFont()
font.setPointSize(13)
font.setBold(True)
font.setWeight(75)
self.comboBox_7.setFont(font)
self.comboBox_7.setObjectName("comboBox_7")
self.comboBox_7.addItem("")
self.comboBox_8 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_8.setGeometry(QtCore.QRect(self.x4_comboBox, self.y2_comboBox, 110, 20))
font = QtGui.QFont()
font.setPointSize(13)
font.setBold(True)
font.setWeight(75)
self.comboBox_8.setFont(font)
self.comboBox_8.setObjectName("comboBox_8")
self.comboBox_8.addItem("")
self.label_1 = QtWidgets.QLabel(self.centralwidget)
self.label_1.setGeometry(QtCore.QRect(100, 240, 181, 41))
font = QtGui.QFont()
font.setPointSize(13)
font.setBold(True)
font.setItalic(False)
font.setUnderline(False)
font.setWeight(75)
font.setStrikeOut(False)
self.label_1.setFont(font)
self.label_1.setObjectName("label_1")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(460, 240, 181, 41))
font = QtGui.QFont()
font.setPointSize(13)
font.setBold(True)
font.setItalic(False)
font.setUnderline(False)
font.setWeight(75)
font.setStrikeOut(False)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.line_2 = QtWidgets.QFrame(self.centralwidget)
self.line_2.setGeometry(QtCore.QRect(self.y2_comboBox, 260, 20, 91))
font = QtGui.QFont()
font.setPointSize(13)
font.setBold(True)
font.setWeight(75)
self.line_2.setFont(font)
self.line_2.setFrameShape(QtWidgets.QFrame.VLine)
self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_2.setObjectName("line_2")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
self.statusbar.showMessage('维护@白泽文', 100000)
MainWindow.setStatusBar(self.statusbar)
self.tb = self.addToolBar('工具条')
tb_open = QAction('1.读取文件', self)
self.tb.addAction(tb_open)
tb_export = QAction('2.生成文件', self)
self.tb.addAction(tb_export)
self.tb.actionTriggered[QAction].connect(self.tb_cmd)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "1型试验绘图软件"))
self.comboBox_1.setItemText(0, _translate("MainWindow", "FLTP"))
self.comboBox_2.setItemText(0, _translate("MainWindow", "RLTP"))
self.comboBox_3.setItemText(0, _translate("MainWindow", "FRTP"))
self.comboBox_4.setItemText(0, _translate("MainWindow", "RRTP"))
self.comboBox_5.setItemText(0, _translate("MainWindow", "RRT"))
self.comboBox_6.setItemText(0, _translate("MainWindow", "RLT"))
self.comboBox_7.setItemText(0, _translate("MainWindow", "FLT"))
self.comboBox_8.setItemText(0, _translate("MainWindow", "FRT"))
self.label_1.setText(_translate("MainWindow", "制动盘温度"))
self.label_2.setText(_translate("MainWindow", "摩擦片温度"))
"""self.menu_1.setTitle(_translate("MainWindow", "读取文件"))
self.menu_2.setTitle(_translate("MainWindow", "生成文件"))"""
def tb_cmd(self):
pass
if __name__=='__main__':
app = QApplication(sys.argv)
mywin = Ui_MainWindow()
mywin.show()
sys.exit(app.exec_())
逻辑文件子类代码部分如下:
class MyMainWindow(Ui_MainWindow):
'继承类 修改属性 增加方法'
def __init__(self):
'实例化时执行'
print('实例化类:class MyMainWindow')
print('*'*100)
super().__init__()
self.vbo_path = []
self.pf = []
self.vbo_path_0 = []
self.col_names = []
def tb_cmd(self, a):
cmd = a.text()
print('按下 ', cmd)