从第一个组合框获取价值,并更改第二个组合框的价值

时间:2019-09-19 12:25:37

标签: python pyqt pyqt5 python-3.6

我这里有两个组合框,第一个是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-无效错误。

1 个答案:

答案 0 :(得分:1)

不好意思,您没有提供一个可重复生成的示例,那么我可以证明您可能做错了什么,但这是我认为您要完成的工作的一个简化版本。

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_())