如何根据 PyQt5 中的另一个 QComboBox 更改一个 QComboBox 的内容? [英] How can I change the contents of one QComboBox depending on another QComboBox in PyQt5?

查看:54
本文介绍了如何根据 PyQt5 中的另一个 QComboBox 更改一个 QComboBox 的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I write program on python3 that will calculate thermodynamic properties. This is piece of GUI

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

"Выбранное вещество" is a selected substance like 1-Butene, water, ammonia and etc.

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

"Первый параметр" is a first parameter. User will choose parameters like pressure, temperature, density and etc, and unit of measurement like Pa, MPa, bar (if it's pressure) and etc. So I don't know one thing: i want that if user chose pressure('Давление (P)') in top combobox, depending on this, suitable units of measure were chosen in small combobox.

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

What have i already done: create 2 files

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

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

So in CalcProp.py i wrote class that contains function that returns the list of parameters:

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

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

After i created class that contains function that selects unit of measurements:

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

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

Everything works, but that's only when the program starts, when I change the pressure to another value, then unit of measurement does not change. And I'm interested in how to change the contents of one combobox depending on another in real time.

推荐答案

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

You must use the signal currentTextChanged, this is activated every time you choose an item, returning the text, we must also validate if it is a list or a single element. All of the above is implemented in the following code:

    [...]
    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)

这篇关于如何根据 PyQt5 中的另一个 QComboBox 更改一个 QComboBox 的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆