【问题标题】:How to make QCombobox expandable for hierarchical items? (PyQT5)如何使 QCombobox 可扩展用于分层项目? (PyQT5)
【发布时间】:2020-02-12 09:26:47
【问题描述】:

我有一些字典变量,例如:

{"Courses" : ["Math" , "Science" , "English"]} , 
{"Cars" : ["Ford" , "BMW" , "Mercedes"]} , etc.

当我获得 QCombobox 的选定值时,我失去了它所应用的键!

【问题讨论】:

  • 你能提供一个最小的工作示例吗?
  • codepad.org/MljIT789选择物品时如何获得钥匙(“Courses”、“Cars”)?
  • 感谢您的示例,我添加了一个 aswer,希望它能解决您的问题。整个代码在这里:codepad.org/sraoTV7Y
  • 当我将代码传输到我的项目时出现错误:AttributeError: 'MovingDTranslator' object has no attribute 'sender'
  • 我搞错了,成功了)

标签: python pyqt


【解决方案1】:

解决这个问题的一种方法是在你的类中添加一个辅助函数,它会为你打印密钥:

    def getKey(self):
        current = self.sender()
        for key, val in self.combo.items():
            if val == current:
                print(key, val.currentText())

然后我将此功能连接到您的组合框:

    for k in lst:
        self.combo[k] = QComboBox()
        self.combo[k].addItems(lst[k])
        self.combo[k].currentIndexChanged.connect(self.getKey)
        self.layout.addWidget(self.combo[k])

【讨论】:

    猜你喜欢
    • 2019-03-13
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多