简体   繁体   English

我有 2 个组合框(comboBox_1,comboBox_2)comboBox_1 并用正确的数据填充组合框_2

[英]I have 2 comboBoxes (comboBox_1, comboBox_2) comboBox_1 and populate comboBox_2 with correct data

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(711, 425)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lineEdit_1 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_1.setGeometry(QtCore.QRect(110, 110, 200, 40))
        font = QtGui.QFont()
        font.setPointSize(18)
        font.setBold(True)
        font.setWeight(75)
        self.lineEdit_1.setFont(font)
        self.lineEdit_1.setObjectName("lineEdit_1")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(390, 110, 200, 40))
        font = QtGui.QFont()
        font.setPointSize(18)
        font.setBold(True)
        font.setWeight(75)
        self.lineEdit_2.setFont(font)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(340, 110, 31, 31))
        font = QtGui.QFont()
        font.setPointSize(28)
        font.setBold(True)
        font.setWeight(75)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox_2.setGeometry(QtCore.QRect(110, 170, 481, 40))
        font = QtGui.QFont()
        font.setPointSize(18)
        font.setBold(True)
        font.setWeight(75)
        self.comboBox_2.setFont(font)
        self.comboBox_2.setObjectName("comboBox_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(8, 260, 101, 31))
        font = QtGui.QFont()
        font.setPointSize(16)
        font.setBold(True)
        font.setWeight(75)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.quitBtn = QtWidgets.QPushButton(self.centralwidget)
        self.quitBtn.setGeometry(QtCore.QRect(547, 320, 121, 51))
        font = QtGui.QFont()
        font.setPointSize(16)
        font.setBold(True)
        font.setWeight(75)
        self.quitBtn.setFont(font)
        self.quitBtn.setObjectName("quitBtn")
        self.clearBtn = QtWidgets.QPushButton(self.centralwidget)
        self.clearBtn.setGeometry(QtCore.QRect(400, 320, 121, 51))
        font = QtGui.QFont()
        font.setPointSize(16)
        font.setBold(True)
        font.setWeight(75)
        self.clearBtn.setFont(font)
        self.clearBtn.setObjectName("clearBtn")
        self.formula_lbl = QtWidgets.QLabel(self.centralwidget)
        self.formula_lbl.setGeometry(QtCore.QRect(107, 260, 561, 31))
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.formula_lbl.setFont(font)
        self.formula_lbl.setText("\tFahrenheit = 9/5 * Celcius + 32")
        self.formula_lbl.setObjectName("formula_lbl")
        self.comboBox_1 = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox_1.setGeometry(QtCore.QRect(110, 50, 481, 40))
        font = QtGui.QFont()
        font.setPointSize(18)
        font.setBold(True)
        font.setWeight(75)
        self.comboBox_1.setFont(font)
        self.comboBox_1.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.comboBox_1.setObjectName("comboBox_1")
        self.label_1 = QtWidgets.QLabel(self.centralwidget)
        self.label_1.setGeometry(QtCore.QRect(250, 0, 211, 32))
        font = QtGui.QFont()
        font.setPointSize(18)
        font.setBold(True)
        font.setWeight(75)
        self.label_1.setFont(font)
        self.label_1.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.label_1.setTextFormat(QtCore.Qt.RichText)
        self.label_1.setObjectName("label_1")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 711, 30))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        #   When quitBtn is clicked quit app
        self.quitBtn.clicked.connect(QtWidgets.qApp.quit)

        """ When clearBtn is clicked lineEdit_1 and lineEdit_2 are cleared """
        self.clearBtn.clicked.connect(self.lineEdit_1.clear)
        self.clearBtn.clicked.connect(self.lineEdit_2.clear)

        #   Add items to combobox_1
        # self.comboBox_1.addItem("Temperature Conversion") # comboBox_1 Index 0
        # self.comboBox_1.addItem("Speed Conversion")      # comboBox_1 Index 1
        self.comboBox_1.addItems(("Temperature", "Speed"))


        #   Add items temperature Items to combobox_2
        self.comboBox_2.addItem("Celsius  to  Fahrenheight")    # comboBox_2 Index 0
        self.comboBox_2.addItem("Fahrenheight  to  Celsius")    # comboBox_2 Index 1
        self.comboBox_2.addItem("Kelvin  to  Fahrenheight")     # comboBox_2 Index 2
        self.comboBox_2.addItem("Fahrenheight  to  Kelvin")     # comboBox_2 Index 3
        self.comboBox_2.addItem("Celsius  to  Kelvin")          # comboBox_2 Index 4
        self.comboBox_2.addItem("Kelvin  to  Celsius")          # comboBox_2 Index 5
        #  Add Speed Items to comboBox_2
        self.comboBox_2.addItem("Miles per hour to Kilometer")  # comboBox_2 Index 6
        self.comboBox_2.addItem("Kilometer to Miles per hour")  # comboBox_2 Index 7
        #---------------------------------------------------------------------

        """ Checks if the current comboBox_1 selection has changed """
        self.comboBox_1.currentIndexChanged.connect(self.combobox1_changed,
        self.comboBox_1.currentIndex())
        self.comboBox_1.setCurrentIndex(0)




        """ Checks if the current comboBox_2 selection has changed """
        self.comboBox_2.currentIndexChanged.connect(self.combobox2_changed,
        self.comboBox_2.currentIndex())
        self.comboBox_2.setCurrentIndex(0)




    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Conversion App"))
        self.label_2.setText(_translate("MainWindow", "="))
        self.label_3.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" color:#ff5500;\">Formula:</span></p></body></html>"))
        self.quitBtn.setText(_translate("MainWindow", "Quit"))
        self.clearBtn.setText(_translate("MainWindow", "Clear"))
        self.label_1.setText(_translate("MainWindow", " "))

        #   This is for my reference from my CLI app
        """ function to convert celcius to fahrenheit """
        """ Celsius = Index 1 """
    # def convert_to_fahrenheit():
    #     celcius_formula = "fahrenheit = 9/5 * celcius + 32"
    #     self.formula_lbl.additem(celcius_formula)
    #     try:
    #         celcius = float(self.lineEdit_1)
    #         fahrenheit = 9/5 * celcius + 32

    #     except ValueError:
    #         print("Please enter numbers only.\n")
    #         return convert_to_fahrenheit()
    #     else:
    #         return self.lineEdit_2.addItem(str(round(fahrenheit, 1)))          
    #     convert_to_fahrenheit()

I would like to select an item in comboBox_1 and populate comboBox_2 with the correct data for my selection我想 select 组合框_1 中的一个项目,并用我选择的正确数据填充组合框_2

First off, you seem to be designing your GUI in Qt Designer and then editing the exported.py file directly.首先,您似乎是在 Qt Designer 中设计您的 GUI,然后直接编辑 export.py 文件。 This is almost always a bad idea since if you ever decide to update your GUI in Qt Designer later on, any changes to the.py file will be lost when you re-export your GUI.这几乎总是一个坏主意,因为如果您以后决定在 Qt 设计器中更新您的 GUI,当您重新导出 GUI 时,对 .py 文件的任何更改都将丢失。 A better approach is to leave the exported.py file intact and import it in another python file where you can use it to setup an instance of QMainWindow .一个更好的方法是保持exported.py 文件完整,并将其导入另一个python 文件,您可以在其中使用它来设置QMainWindow的实例。 One common way to do this is to create a separate class that inherits from both the base widget ( QMainWindow in this case) and the UI class ('Ui_MainWindow') and setup the required signals and slots in there.一种常见的方法是创建一个单独的 class,它继承自基本小部件(在本例中为QMainWindow )和 UI class('Ui_MainWindow')并在其中设置所需的信号和插槽。

As for answering the actual question: in order to dynamically change the items in combobox 2 when an item is selected in combobox 1, you could use a dictionary where you map each items in combobox 1 to the corresponding list of items in combobox 2. You could then use a second dictionary to store the formulas for each possible. As for answering the actual question: in order to dynamically change the items in combobox 2 when an item is selected in combobox 1, you could use a dictionary where you map each items in combobox 1 to the corresponding list of items in combobox 2. You然后可以使用第二个字典来存储每个可能的公式。 This has the added advantage that you don't need a long list of if-elif statements to calculate the result.这有一个额外的好处,即您不需要一长串 if-elif 语句来计算结果。

To give an example, suppose Ui_MainWindow.py is the python file that was exported from Qt Designer, then the main file could be something like举个例子,假设Ui_MainWindow.py是从 Qt Designer 导出的 python 文件,那么主文件可能类似于

from PyQt5 import QtWidgets, QtCore
from Ui_MainWindow import Ui_MainWindow


class MyMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        # use Ui_MainWindow.setupUi to setup ui
        self.setupUi(self)

        #   connect quitBtn.clicked and clearBtn.clicked as before
        self.quitBtn.clicked.connect(QtWidgets.qApp.quit)
        self.clearBtn.clicked.connect(self.lineEdit_1.clear)
        self.clearBtn.clicked.connect(self.lineEdit_2.clear)

        #   add items to combobox 1
        self.comboBox_1.addItems(("Temperature", "Speed"))

        #   dict for storing items in combobox 2 corresponding to items in combobox 1
        self.combobox2_dict = {
            'Temperature': ["Celsius  to  Fahrenheit", "Fahrenheight  to  Celsius",
                            "Kelvin  to  Fahrenheit", "Fahrenheit  to  Kelvin",
                            "Celsius  to  Kelvin", "Kelvin  to  Celsius"],
            'Speed': ["Miles per hour to Kilometer per hour", "Kilometer per hour to Miles per hour"]}

        self.transformation_dict = {
            "Celsius  to  Fahrenheit": (lambda T: 9 / 5 * T + 32, 'fahrenheit = 9/5 * celsius + 32'),
            "Fahrenheight  to  Celsius": (lambda T: (T - 32) * 5 / 9, 'celsius = 5/9 * (fahrenheit - 32)'),
            "Kelvin  to  Fahrenheit": (lambda T: 9 / 5 * (T - 273.15) + 32, 'fahrenheit = ... kelvin'),
            "Fahrenheit  to  Kelvin": (lambda T: (T - 32) * 5 / 9 + 273.15, 'kelvin = ... fahrenheit'),
            "Celsius  to  Kelvin": (lambda T: T + 273.15, 'kelvin = celsius + 273.15'),
            "Kelvin  to  Celsius": (lambda T: T - 273.15, 'celsius = kelvin - 273.15'),
            "Miles per hour to Kilometer per hour": (lambda v: v*1.60934, 'miles/h = 1.60934 * km/h'),
            "Kilometer per hour to Miles per hour": (lambda v: v/1.60934, 'km/h = 0.62137 * miles/h') }

        # connect signals to slots for updating combobox 2, the formula label and calculating the result.
        self.comboBox_1.currentTextChanged.connect(self.combobox1_changed)

        # reset label and recalculate result when comboBox_2 changes
        self.comboBox_2.currentTextChanged.connect(self.calculate_result)
        self.comboBox_2.currentTextChanged.connect(self.set_formula_label)

        # recalculate result when lineEdit_1 changes
        self.lineEdit_1.textChanged.connect(self.calculate_result)

        # initialize formula label
        self.comboBox_1.currentTextChanged.emit(self.comboBox_1.currentText())

    def combobox1_changed(self, text):
        blocked = self.comboBox_2.blockSignals(True)
        self.comboBox_2.clear()
        self.comboBox_2.addItems(self.combobox2_dict.get(text, []))
        self.comboBox_2.blockSignals(blocked)
        self.comboBox_2.setCurrentIndex(0)
        self.comboBox_2.currentTextChanged.emit(self.comboBox_2.currentText())

    def set_formula_label(self, text):
        formula = self.transformation_dict[text][1]
        self.formula_lbl.setText(formula)

    def calculate_result(self):
        # Attempt to calculate result by reading text from self.lineEdit_1 and extracting the
        # transformation function from self.transformation_dict
        try:
            # retrieve transformation function and formula as string from dictionary
            func = self.transformation_dict[self.comboBox_2.currentText()][0]
            val0 = float(self.lineEdit_1.text())
            val1 = func(val0)
            self.lineEdit_2.setText(f'{val1:0.2f}')
        except:
            self.lineEdit_2.setText('')



if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    win = MyMainWindow()
    win.show()
    app.exec()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM