我用QtDesigner构建了一个带有组合框和几个选项卡的GUI。我要这样做,以便根据组合框中选择的项目,仅启用某些选项卡。
下面是一个示例,其中有一个包含4个项目的组合框和一个包含4个选项卡的选项卡小部件。我想做的是选择“ 1”时,仅启用选项卡1,选择“ 2”时,则启用选项卡1和选项卡2,等等。
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(295, 177)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.formLayout_5 = QtWidgets.QFormLayout(self.centralwidget)
self.formLayout_5.setObjectName("formLayout_5")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setObjectName("label_5")
self.formLayout_5.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label_5)
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.formLayout_5.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.comboBox)
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setEnabled(True)
self.tab.setObjectName("tab")
self.formLayout_4 = QtWidgets.QFormLayout(self.tab)
self.formLayout_4.setObjectName("formLayout_4")
self.label = QtWidgets.QLabel(self.tab)
self.label.setObjectName("label")
self.formLayout_4.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label)
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.formLayout_3 = QtWidgets.QFormLayout(self.tab_2)
self.formLayout_3.setObjectName("formLayout_3")
self.label_2 = QtWidgets.QLabel(self.tab_2)
self.label_2.setObjectName("label_2")
self.formLayout_3.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label_2)
self.tabWidget.addTab(self.tab_2, "")
self.tab_3 = QtWidgets.QWidget()
self.tab_3.setObjectName("tab_3")
self.formLayout_2 = QtWidgets.QFormLayout(self.tab_3)
self.formLayout_2.setObjectName("formLayout_2")
self.label_3 = QtWidgets.QLabel(self.tab_3)
self.label_3.setObjectName("label_3")
self.formLayout_2.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label_3)
self.tabWidget.addTab(self.tab_3, "")
self.tab_4 = QtWidgets.QWidget()
self.tab_4.setObjectName("tab_4")
self.formLayout = QtWidgets.QFormLayout(self.tab_4)
self.formLayout.setObjectName("formLayout")
self.label_4 = QtWidgets.QLabel(self.tab_4)
self.label_4.setObjectName("label_4")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label_4)
self.tabWidget.addTab(self.tab_4, "")
self.formLayout_5.setWidget(2, QtWidgets.QFormLayout.SpanningRole, self.tabWidget)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 295, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_5.setText(_translate("MainWindow", "ComboBox"))
self.comboBox.setItemText(0, _translate("MainWindow", "1"))
self.comboBox.setItemText(1, _translate("MainWindow", "2"))
self.comboBox.setItemText(2, _translate("MainWindow", "3"))
self.comboBox.setItemText(3, _translate("MainWindow", "4"))
self.label.setText(_translate("MainWindow", "Some text."))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.label_2.setText(_translate("MainWindow", "Some text."))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
self.label_3.setText(_translate("MainWindow", "Some text."))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("MainWindow", "Tab 3"))
self.label_4.setText(_translate("MainWindow", "Some text."))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_4), _translate("MainWindow", "Tab 4"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
您必须使用currentIndexChanged
信号来获取选定的索引,然后必须遍历应启用或不使用setTabEnabled()
的选项卡设置:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
# ...
# https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html#using-the-generated-code
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.comboBox.currentIndexChanged.connect(self.onCurrentIndexChanged)
self.onCurrentIndexChanged(self.comboBox.currentIndex())
@QtCore.pyqtSlot(int)
def onCurrentIndexChanged(self, index):
for i in range(self.tabWidget.count()):
self.tabWidget.setTabEnabled(i, index >= i)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())