如何根据 PyQt5 中的另一个 QComboBox 更改一个 QComboBox 的内容?

How can I change the contents of one QComboBox depending on another QComboBox in PyQt5?

我在 python3 上编写程序来计算热力学性质。这是 GUI

“Выбранное вещество”是一种选定的物质,如 1-丁烯、水、氨等。

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

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

所以在 CalcProp.py 中我写了 class 包含 returns 参数列表的函数:

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 后,它包含选择测量单位的功能:

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

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

你必须使用信号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)