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

2024-05-20 14:17:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python3上写了一个计算热力学性质的程序。这是一个GUI

enter image description here

“β-丁烯、水、氨等选定物质”

“Паааааааааааааа107。用户将选择压力、温度、密度等参数,以及测量单位,如Pa、MPa、bar(如果是压力)等。因此,我不知道一件事:我想知道,如果用户在顶部组合框中选择压力('Пааааааааааааааа(P)'),则根据

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

  • testGUI.py-有所有GUI(我在Qt设计器中创建)
  • 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))

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


Tags: ofthepyself程序returnparameter单位
1条回答
网友
1楼 · 发布于 2024-05-20 14:17:55

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

相关问题 更多 >