【问题标题】:How can I change the contents of one QComboBox depending on another QComboBox in PyQt5?如何根据 PyQt5 中的另一个 QComboBox 更改一个 QComboBox 的内容?
【发布时间】:2021-10-04 20:53:42
【问题描述】:

我在 python3 上编写程序来计算热力学性质。这是一块图形用户界面

“Выбранное вещество”是1-丁烯、水、氨等精选物质。

“Первый параметр”是第一个参数。用户将选择压力,温度,密度等参数,以及Pa,MPa,bar(如果是压力)等测量单位。所以我不知道一件事:如果用户选择压力('Давление (P)') 在顶部组合框中,根据此,在小组合框中选择合适的度量单位。

我已经做了什么:创建 2 个文件

  • testGUI.py - 所有的 GUI(我在 Qt Designer 中创建)
  • CalcProp.py - 程序的所有逻辑(类和函数)

所以在 CalcProp.py 中,我编写了包含返回参数列表的函数的类:

class ChooseParams():
   def paramList(self):
      P = 'Pressure (P)'
      T = 'Temperature (T)'
      D = 'Density (D)'
      V = 'Volume (V)'
      H = 'Enthalpy (h)'
      S = 'Entropy (s)'
      Q = 'Vapor quality (x)'

      allParams = [P, T, D, V, H, S, Q]

      return allParams

在我创建了包含选择测量单位的函数的类之后:

class ChooseUnitOfMeasurement():
    def unitOfMeasurement(self, parameter):

        #Pressure
        Pa = 'Па'
        kPa = 'кПа'
        MPa = 'МПа'
        PressureUnitList = [Pa, kPa, MPa]

        #Temperature
        kelvin = 'К'
        degC = '°C'
        degF = '°F'
        tempUnitList = [kelvin, degC, degF]

        #Enthalpy
        kJdivKg = 'кДж/кг'
        JdivKg = 'Дж/кг'
        enthalpyUnitList = [kJdivKg, JdivKg]

        #Entropy
        kJdivKgKel = 'кДж/(кг-К)'
        JdivKgKel = 'Дж/(кг-К)'
        entropyUnitList = [kJdivKgKel, JdivKgKel]

        #Density
        kgDivMeter = 'кг/м^3'

        #Volume
        meterDivKg = 'м^3/кг'

        #Vapor quality
        vaporQuality = '--'


        if parameter == 'Pressure (P)':
            return PressureUnitList
        elif parameter == 'Temperature (T)':
            return tempUnitList
        elif parameter == 'Density (D)':
            return kgDivMeter
        elif parameter == 'Volume (V)':
            return meterDivKg
        elif parameter == 'Enthalpy (h)':
            return enthalpyUnitList
        elif parameter == 'Entropy (s)':
            return entropyUnitList
        else:
            return vaporQuality

testGUI.py

#Creation combobox for selection first parameter
self.comboBoxInputFirstParam = QtWidgets.QComboBox(self.groupBoxFirstParam)
#put parameters
self.comboBoxInputFirstParam.addItems(CalcProp.ChooseParams.paramList(self))

#Creation combobox for selection unit of measurement (first parameter)
self.comboBoxInputFirstParamUnit = QtWidgets.QComboBox(self.groupBoxFirstParam)
#get text of first combobox
firstParameter = self.comboBoxInputFirstParam.currentText()
#Depending on the content of the first one, add the required list / value in the combobox with units of measurement.
self.comboBoxInputFirstParamUnit.addItems(CalcProp.ChooseUnitOfMeasurement.unitOfMeasurement(self, firstParameter))

一切正常,但这只是在程序启动时,当我将压力更改为另一个值时,测量单位不会改变。 我对如何根据另一个组合框实时更改一个组合框的内容很感兴趣。

【问题讨论】:

  • 什么是self.groupBoxFirstParam
  • 对象名QGroupBox(图中橙色矩形“Первый параметр”)
  • 不必在方法中使用self 作为属性,这是类的责任,因为它在内部传递实例。试试我的代码。
  • 你解决了我的问题吗?
  • @eyllanesc 我试着帮你解决,但我有错误:

标签: python python-3.x pyqt pyqt5 qcombobox


【解决方案1】:

您必须使用信号currentTextChanged,每次选择项目时都会激活该信号,返回文本,我们还必须验证它是列表还是单个元素。以上所有内容都在以下代码中实现:

    [...]
    self.comboBoxInputFirstParam = QtWidgets.QComboBox(self.groupBoxFirstParam)
    self.comboBoxInputFirstParamUnit = QtWidgets.QComboBox(self.groupBoxFirstParam)
    self.comboBoxInputFirstParam.currentTextChanged.connect(self.onCurrentTextChanged)

    self.comboBoxInputFirstParam.addItems(CalcProp.ChooseParams().paramList())

def onCurrentTextChanged(self, text):
    self.comboBoxInputFirstParamUnit.clear()
    elements = CalcProp.ChooseUnitOfMeasurement().unitOfMeasurement(str(text))
    if isinstance(elements, list):
        self.comboBoxInputFirstParamUnit.addItems(elements)
    else:
        self.comboBoxInputFirstParamUnit.addItem(elements)

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 2011-11-06
    • 2016-07-05
    • 2017-02-04
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多