python - 从第一个 Combobox 获取值并更改第二个 Combobox 的值
问题描述
这里我有两个组合框,第一个是 Homepage_List,第二个是 Board_List。
我想要的是,当我选择 Homepage_List 之一时,相应的 bd 列表会出现在 Board_List 中。
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import uic,QtGui,QtCore
class Main_App(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui = uic.loadUi("mainwindow_for_parser.ui", self)
self.ui.show()
self.Select_HP()
self.Select_BD()
def get_combobox(self, index):
if index == 1:
cb_bd_list.addItem(" ")
cb_bd_list.addItem("correct")
cb_bd_list.addItem("board")
cb_bd_list.addItem("list")
elif index == 2:
cb_bd_list.addItem(" ")
cb_bd_list.addItem("correct2")
cb_bd_list.addItem("board2")
cb_bd_list.addItem("list2")
def Select_HP(self):
cb_hp_list = self.ui.Homepage_List
cb_hp_list.addItem(" ")
cb_hp_list.addItem("HP address 1",1)
cb_hp_list.addItem("HP address 2",2)
cb_hp_list.addItem("HP address 3",3)
def Select_BD(self):
cb_bd_list = self.ui.Board_List
current_hp = self.ui.Homepage_List.activated.connect(self.get_combobox)
当我运行该代码时,我得到了 cb_bd_list - 无效错误。
解决方案
好吧,很遗憾您没有提供最小可重现示例,那么我本可以展示您可能做错了什么,但这是我认为您正在尝试完成的工作的简约工作版本。
from sys import exit as sysExit
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QComboBox, QHBoxLayout, QLabel
class CentralPanel(QWidget):
def __init__(self, parent):
QWidget.__init__(self)
self.Parent = parent
self.cbxHPList = QComboBox()
self.cbxHPList.currentIndexChanged.connect(self.ChangeBDList)
self.cbxBDList = QComboBox()
HBox = QHBoxLayout()
HBox.addWidget(self.cbxHPList)
HBox.addWidget(QLabel(' '))
HBox.addWidget(self.cbxBDList)
HBox.addStretch(1)
self.setLayout(HBox)
def ChangeBDList(self, Index):
self.Parent.UpdateBDList(Index)
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle('TestIt')
self.resize(100,100)
self.HPList = {0:' ', 1:'HP Address 1', 2:'HP Address 2', 3:'HP Address 3'}
self.BDList1 = [' ', 'First', 'Board1', 'List1']
self.BDList2 = [' ', 'Second', 'Board2', 'List2']
self.BDList3 = [' ', 'Third', 'Board2', 'List3']
self.CenterPane = CentralPanel(self)
self.setCentralWidget(self.CenterPane)
self.SetHome()
def SetHome(self):
self.CenterPane.cbxHPList.clear()
for key in self.HPList.keys():
self.CenterPane.cbxHPList.insertItem(key, self.HPList[key])
def UpdateBDList(self, Index):
ListUsed = []
if Index == 1:
ListUsed = self.BDList1
elif Index == 2:
ListUsed = self.BDList2
elif Index == 3:
ListUsed = self.BDList3
self.CenterPane.cbxBDList.clear()
if len(ListUsed) > 0:
Indx = 0
for Item in ListUsed:
self.CenterPane.cbxBDList.addItem(Item)
Indx += 1
if __name__ == '__main__':
MainThred = QApplication([])
MainGUI = MainWindow()
MainGUI.show()
sysExit(MainThred.exec_())
推荐阅读
- python - 处理其中行的字段多于字段名的 csv 文件?
- visual-studio - Visual Studio C++“计算代码指标”不起作用
- google-sheets - 条件格式以检查填写电子表格时的错误
- java - “IDENTITY_INSERT”设置为 OFF NETBEANS SQL
- plantuml - Plantuml 中的嵌套 if 语句
- redmine - FreeIPA 中 LDAP 用户的 EasyRedmine 登录失败
- prolog - Prolog:存在量化
- python - 为 GitLab 发布创建资产
- javascript - v8 对象中“快速”、“可内联”属性的数量限制是多少?
- flutter - 如何将依赖于另一个流的流映射到依赖于两个流的新模型?