简体   繁体   English

通过pyqt用Python更改多个依赖的组合框

[英]Changing multiple depending comboboxes with Python through pyqt

I am trying to add three depending comboboxes in Qt with python. 我试图用python在Qt中添加三个依赖的组合框 That means that you can change one combobox which changes the list of items in the secound combobox. 这意味着您可以更改一个组合框,该组合框将更改secound组合框中的项目列表。 There you can select another item which changes the selection in the third combobox. 您可以在此处选择另一个项目,以更改第三个组合框中的选择。 Therefore i use qtpy as connection between both. 因此,我使用qtpy作为两者之间的连接。

What is really important: The connection of all three comboboxes! 真正重要的是:所有三个组合框的连接!

It worked all fine until you change the first combobox for a second time (It worked all fine while changing the secound & third combobox all the time) 一切正常,直到您第二次更改第一个组合框为止(在始终更改secound和第三个组合框的同时,一切正常)

My goal is to change the values in all three comboboxes depending on the first combobox (and secound) as often as i want to. 我的目标是尽可能多地根据第一个组合框(和secound)更改所有三个组合框的值。

Here is my code: 这是我的代码:

    self.ui.type1CB.currentTextChanged.connect(self.type1_changed)
    self.ui.type2CB.currentTextChanged.connect(self.type2_changed)


def type1_changed(self):
    self.ui.type2CB.clear()

    type1 = self.ui.type1CB.currentText()
    rev = ["Ge", "Er"]
    cos = ["Au", "Fr", "pe", "So", "Wo"]

    if type1 == "Ei":
        self.ui.type2CB.addItems(rev)
    elif type1 == "Au":
        self.ui.type2CB.addItems(cos)
    else:
        self.ui.type2CB.addItems(" ")

def type2_changed(self):
    self.ui.type3CB.clear()

    type2 = self.ui.type2CB.currentText()
    sa = ["Ge", "Li", "To"]
    re = ["Pf", "Ne", "Ve"]
    ca = ["Be", "Re", "Ve"]
    le = ["Au", "Be", "Ur"]
    pr = ["Le", "Ge", "Sü", "Kl", "Hy", "Ge", "Ve"]
    ot = ["An", "Ar", "Fa", "Ba", "Fe"]
    ho = ["Ha", "Ne", "Te", "Mi"]

    if type2 == "Ge":
        self.ui.type3CB.addItems(sa)
    elif type2 == "Er":
        self.ui.type3CB.addItems(re)
    elif type2 == "Au":
        self.ui.type3CB.addItems(ca)
    elif type2 == "Fr":
        self.ui.type3CB.addItems(le)
    elif type2 == "pe":
        self.ui.type3CB.addItems(pr)
    elif type2 == "So":
        self.ui.type3CB.addItems(ot)
    elif type2 == "Wo":
        self.ui.type3CB.addItems(ho)
    else:
        self.ui.type3CB.addItems(" ")

error is in this line: 错误在这一行:

    else:
        self.dlg.type3CB.addItems(" ")

if you are using addItems you need to provide list, and you are providing string 如果使用addItems,则需要提供列表,并且要提供字符串

you can do it in two ways: 您可以通过两种方式做到这一点:

1. use addItem 1.使用addItem

addItem(const QString &text, const QVariant &userData = QVariant()) addItem(const QString&text,const QVariant&userData = QVariant())

    self.dlg.type3CB.addItem(" ")

2. use addItems 2.使用addItems

addItems(const QStringList &texts) addItems(const QStringList&texts)

    self.dlg.type3CB.addItems([" "])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM