任务是在ComboBox中创建依赖列表。如果我在
UiComponents
的第一个列表中选择推杆,那么第二个列表应从列表 geek_list1
中选择项目,如果我选择第二个项目,则从 geek_list2
中选择项目。
但这并没有发生,并且
UiComponents2
未填充。
如何在 PyQT ComboBox 中创建依赖列表有任何变体吗?
我的代码。
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
# setting title
self.setWindowTitle("Python ")
# setting geometry
self.setGeometry(100, 100, 600, 400)
# calling method
self.UiComponents()
self.UiComponents2()
# showing all the widgets
self.show()
# method for widgets
def UiComponents(self):
# creating a combo box widget
combo_box = QComboBox(self)
# setting geometry of combo box
combo_box.setGeometry(200, 100, 120, 30)
# geek list
geek_list = ["Geek", "Geeky Geek", "Legend Geek", "Ultra Legend Geek"]
# adding list of items to combo box
combo_box.addItems(geek_list)
def UiComponents2(self):
# creating a combo box widget
combo_box = QComboBox(self)
# setting geometry of combo box
combo_box.setGeometry(200, 150, 120, 30)
# geek list
geek_list1 = ["Geek1", "Geeky Geek1", "Legend Geek1", "Ultra Legend Geek1"]
geek_list2 = ["Geek2", "Geeky Geek2", "Legend Geek2", "Ultra Legend Geek2"]
# adding list of items to combo box
if self.UiComponents() == "Geek":
combo_box.addItems(geek_list1)
if self.UiComponents() == "Geeky Geek":
combo_box.addItems(geek_list2)
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())`
我尝试通过
if self.UiComponents()
条件进行操作,但是 UiComponents2
看不到做出了哪个选择。
借助字典解决了我的问题。也许有人会发现它有用。
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtGui import QStandardItemModel, QStandardItem, QFont
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(693, 220)
self.centralwidget = QtWidgets.QWidget(parent=MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.MainComboBox = QtWidgets.QComboBox(parent=self.centralwidget)
self.MainComboBox.setGeometry(QtCore.QRect(30, 30, 211, 41))
font = QtGui.QFont()
font.setPointSize(16)
self.MainComboBox.setFont(font)
self.MainComboBox.setObjectName("MainComboBox")
self.OfComboBox = QtWidgets.QComboBox(parent=self.centralwidget)
self.OfComboBox.setGeometry(QtCore.QRect(150, 90, 261, 41))
font = QtGui.QFont()
font.setPointSize(16)
self.OfComboBox.setFont(font)
self.OfComboBox.setObjectName("OfComboBox")
self.pushButton = QtWidgets.QPushButton(parent=self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(460, 80, 221, 51))
font = QtGui.QFont()
font.setPointSize(16)
self.pushButton.setFont(font)
self.pushButton.setStyleSheet("background-color: rgb(255, 170, 127);")
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(parent=self.centralwidget)
self.label.setGeometry(QtCore.QRect(6, 152, 681, 61))
font = QtGui.QFont()
font.setPointSize(16)
self.label.setFont(font)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "TestComboBox"))
self.pushButton.setText(_translate("MainWindow", "Choice"))
self.label.setText(_translate("MainWindow", "TextLabel"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
layout = QtWidgets.QVBoxLayout(self.centralwidget)
layout.addWidget(self.label)
# add button
layout.addWidget(self.pushButton)
self.pushButton.clicked.connect(self.item_label)
self.model = QStandardItemModel()
# add ComboBox
self.MainComboBox.setFixedSize(210, 40)
font = QtGui.QFont()
font.setPointSize(16)
self.MainComboBox.setFont(font)
self.MainComboBox.setObjectName("MainComboBox")
self.MainComboBox.setModel(self.model)
self.OfComboBox.setFixedSize(210, 40)
font = QtGui.QFont()
font.setPointSize(16)
self.OfComboBox.setFont(font)
self.OfComboBox.setObjectName("OfComboBox")
self.OfComboBox.setModel(self.model)
# add data
for k, v in data.items():
state = QStandardItem(k)
self.model.appendRow(state)
for value in v:
city = QStandardItem(value)
state.appendRow(city)
self.MainComboBox.currentIndexChanged.connect(self.updateItemCombo)
self.updateItemCombo(0)
# add ComboBox in Wiget
layout.addWidget(self.MainComboBox)
layout.addWidget(self.OfComboBox)
self.setLayout(layout)
def updateItemCombo(self, index):
indx = self.model.index(index, 0, self.MainComboBox.rootModelIndex())
self.OfComboBox.setRootModelIndex(indx)
self.OfComboBox.setCurrentIndex(0)
def item_label(self):
text_label = self.MainComboBox.currentText()
text_label2 = self.OfComboBox.currentText()
self.label.setText(text_label + ' ' + text_label2)
data = {
'item 1': ['item 1.1', 'item 1.2', 'item 1.3'],
'item 2': ['item 2.1', 'item 2.2', 'item 2.3'],
'item 3': ['item 3.1', 'item 3.2', 'item 3.3']
}
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())