python + pyqt5 UI和信号槽分离方法

时间:2021-12-28 23:02:50

初级菜鸟,知识点记录。

每次重新生成UI.py文件的时候,里面的按钮方法都会被清除,想一个方法可以把按钮响应方法放到外面,利于维护。

 

新建一个按钮文件并继承UI代码,把信号槽及按钮响应方法写在按钮文件里面,这样每次重新生成UI文件时,只需要注释掉信号槽部分,就可以了。

UI 生成py文件,注释掉信号槽部分代码

mainUI.py

 1 # -*- coding: utf-8 -*-
 2 
 3 # Form implementation generated from reading ui file 'mainUI.ui'
 4 #
 5 # Created by: PyQt5 UI code generator 5.11.3
 6 #
 7 # WARNING! All changes made in this file will be lost!
 8 
 9 from PyQt5 import QtCore, QtGui, QtWidgets
10 
11 class Ui_MainWindow(object):
12     def setupUi(self, MainWindow):
13         MainWindow.setObjectName("MainWindow")
14         MainWindow.resize(548, 604)
15         MainWindow.setFocusPolicy(QtCore.Qt.StrongFocus)
16         self.centralwidget = QtWidgets.QWidget(MainWindow)
17         self.centralwidget.setObjectName("centralwidget")
18         self.nav = QtWidgets.QTreeWidget(self.centralwidget)
19         self.nav.setGeometry(QtCore.QRect(0, 0, 131, 551))
20         self.nav.setObjectName("nav")
21         item_0 = QtWidgets.QTreeWidgetItem(self.nav)
22         item_1 = QtWidgets.QTreeWidgetItem(item_0)
23         icon = QtGui.QIcon()
24         icon.addPixmap(QtGui.QPixmap("../../../../../用户/*****/My Documents/My Pictures/猫儿皱眉.gif"), QtGui.QIcon.Disabled, QtGui.QIcon.Off)
25         item_1.setIcon(0, icon)
26         item_1 = QtWidgets.QTreeWidgetItem(item_0)
27         item_1 = QtWidgets.QTreeWidgetItem(item_0)
28         item_0 = QtWidgets.QTreeWidgetItem(self.nav)
29         item_1 = QtWidgets.QTreeWidgetItem(item_0)
30         item_0 = QtWidgets.QTreeWidgetItem(self.nav)
31         self.frame_3 = QtWidgets.QFrame(self.centralwidget)
32         self.frame_3.setGeometry(QtCore.QRect(140, 10, 391, 541))
33         self.frame_3.setAutoFillBackground(True)
34         self.frame_3.setFrameShape(QtWidgets.QFrame.Panel)
35         self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
36         self.frame_3.setObjectName("frame_3")
37         self.label_3 = QtWidgets.QLabel(self.frame_3)
38         self.label_3.setGeometry(QtCore.QRect(60, 30, 54, 12))
39         self.label_3.setObjectName("label_3")
40         self.frame_4 = QtWidgets.QFrame(self.centralwidget)
41         self.frame_4.setGeometry(QtCore.QRect(140, 10, 391, 541))
42         self.frame_4.setAutoFillBackground(True)
43         self.frame_4.setFrameShape(QtWidgets.QFrame.Panel)
44         self.frame_4.setFrameShadow(QtWidgets.QFrame.Raised)
45         self.frame_4.setObjectName("frame_4")
46         self.label_5 = QtWidgets.QLabel(self.frame_4)
47         self.label_5.setGeometry(QtCore.QRect(30, 30, 54, 12))
48         self.label_5.setObjectName("label_5")
49         self.frame_5 = QtWidgets.QFrame(self.centralwidget)
50         self.frame_5.setGeometry(QtCore.QRect(140, 10, 391, 541))
51         self.frame_5.setAutoFillBackground(True)
52         self.frame_5.setFrameShape(QtWidgets.QFrame.WinPanel)
53         self.frame_5.setFrameShadow(QtWidgets.QFrame.Raised)
54         self.frame_5.setObjectName("frame_5")
55         self.label_4 = QtWidgets.QLabel(self.frame_5)
56         self.label_4.setGeometry(QtCore.QRect(40, 60, 54, 12))
57         self.label_4.setObjectName("label_4")
58         MainWindow.setCentralWidget(self.centralwidget)
59         self.menubar = QtWidgets.QMenuBar(MainWindow)
60         self.menubar.setGeometry(QtCore.QRect(0, 0, 548, 23))
61         self.menubar.setObjectName("menubar")
62         MainWindow.setMenuBar(self.menubar)
63         self.statusbar = QtWidgets.QStatusBar(MainWindow)
64         self.statusbar.setObjectName("statusbar")
65         MainWindow.setStatusBar(self.statusbar)
66         
67         #注释掉信号槽部分
68         # self.retranslateUi(MainWindow)
69         # self.nav.itemClicked['QTreeWidgetItem*','int'].connect(self.nav.clear)
70         # QtCore.QMetaObject.connectSlotsByName(MainWindow)
71 
72     def retranslateUi(self, MainWindow):
73         _translate = QtCore.QCoreApplication.translate
74         MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
75         self.nav.headerItem().setText(0, _translate("MainWindow", "测试工具"))
76         __sortingEnabled = self.nav.isSortingEnabled()
77         self.nav.setSortingEnabled(False)
78         self.nav.topLevelItem(0).setText(0, _translate("MainWindow", "个贷相关"))
79         self.nav.topLevelItem(0).child(0).setText(0, _translate("MainWindow", "绑房贷"))
80         self.nav.topLevelItem(0).child(1).setText(0, _translate("MainWindow", "查询用户数据"))
81         self.nav.topLevelItem(0).child(2).setText(0, _translate("MainWindow", "删除客户数据"))
82         self.nav.topLevelItem(1).setText(0, _translate("MainWindow", "外联相关"))
83         self.nav.topLevelItem(1).child(0).setText(0, _translate("MainWindow", "绑定平安保单"))
84         self.nav.topLevelItem(2).setText(0, _translate("MainWindow", "iiii相关"))
85         self.nav.setSortingEnabled(__sortingEnabled)
86         self.frame_3.setToolTip(_translate("MainWindow", "farme3"))
87         self.label_3.setText(_translate("MainWindow", "frame3"))
88         self.frame_4.setToolTip(_translate("MainWindow", "farme4"))
89         self.label_5.setText(_translate("MainWindow", "frame4"))
90         self.frame_5.setToolTip(_translate("MainWindow", "frame5"))
91         self.label_4.setText(_translate("MainWindow", "frame5"))

 

按钮文件,继承UI文件,并定义信号槽

mainButton.py

 1 __author__ = 'EX_WLJR_ZHANGYAMEI'
 2 from PyQt5 import QtCore, QtGui, QtWidgets
 3 from PyQt5.QtWidgets import QApplication,QMainWindow
 4 from mainUI import Ui_MainWindow
 5 import sys
 6 
 7 
 8 class mainButton(Ui_MainWindow):
 9     #继承UI,并添加信号槽
10     def setupUi(self, MainWindow):
11         #继承UI
12         super(mainButton,self).setupUi(MainWindow)
13         # Ui_MainWindow.setupUi(MainWindow)
14         self.frame_3.close()
15         self.frame_4.close()
16         self.frame_5.close()
17 
18         #添加信号槽
19         self.retranslateUi(MainWindow)
20         # self.tabWidget.setCurrentIndex(0)
21         self.nav.itemClicked['QTreeWidgetItem*','int'].connect(self.click01)
22         # self.tabWidget.tabBarDoubleClicked['int'].connect(self.tabRemove)
23 
24         QtCore.QMetaObject.connectSlotsByName(MainWindow)
25 
26     def __init__(self):
27         pass
28 
29     def click01(self,item,n):
30         # print(self.frame_3.wheel())
31         print(item.text(n),n,item)
32         if item.text(n)=='绑房贷':
33             print('******绑房贷**开始**********')
34             # self.tabWidget.setCurrentIndex(0)
35             # print(self.tabWidget.currentIndex())
36             # self.tabWidget.show()
37             # self.frame.show()
38             # self.frame_2.close()
39             self.frame_3.show()
40             self.frame_4.close()
41             self.frame_5.close()
42             print('******绑房贷**结束**********')
43         elif item.text(n)=='查询用户数据':
44             print('******查询用户数据**开始**********')
45             # self.tabWidget.setCurrentIndex(1)
46             # print(self.tabWidget.currentIndex())
47             # self.tabWidget.show()
48             # self.frame.close()
49             # self.frame_2.show()
50             self.frame_3.close()
51             self.frame_4.show()
52             self.frame_5.close()
53             print('******查询用户数据**结束**********')
54         else:
55             self.frame_3.close()
56             self.frame_4.close()
57             self.frame_5.show()
58             pass
59             # self.frame.close()
60             # self.frame_2.close()
61 
62     def tabRemove(self,index):
63         # self.tabWidget.tabBar(0).hide()
64         self.tabWidget.removeTab(index)
65 
66 if __name__ == '__main__':
67      app = QApplication(sys.argv)
68      #创建窗口
69      MainWindow=QtWidgets.QMainWindow()
70      #加载窗口
71      main = mainButton()
72      main.setupUi(MainWindow)
73      #窗口显示
74      MainWindow.show()
75      sys.exit(app.exec_())