单击按钮后如何从 ComboBox 更改变量值?

How to change a variables value from a ComboBox after a button being clicked?

我正在研究未加权的 GPA 计算器,我对 (Py)Qt Designer 应用程序有点陌生。我 运行 遇到了一个问题,我不知道如何从 ComboBoxes 中获取结果并将其计算到名为 gpa 的变量中。

基本上,这就是我想要发生的事情:

如果 letter_grade1 ComboBox 是 A+,那么它会将 4.0 添加到 gpa
如果 letter_grade2 ComboBox 是 B 则它会将 3.0 添加到 gpa

之后,它会除以 5,因为有 5 个组合框正在计算结果被打印出来,这一切都发生在 submit_grades 按钮被点击后。

这是 Ui 的图片:

代码如下所示:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_unweight5(object):

    def setupUi(self, unweight5):
    unweight5.setObjectName("unweight5")
    unweight5.resize(424, 228)
    self.centralwidget = QtWidgets.QWidget(unweight5)
    self.centralwidget.setObjectName("centralwidget")
    self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
    self.gridLayout.setHorizontalSpacing(10)
    self.gridLayout.setVerticalSpacing(5)
    self.gridLayout.setObjectName("gridLayout")
    self.assessment_name = QtWidgets.QLineEdit(self.centralwidget)
    self.assessment_name.setObjectName("assessment_name")
    self.gridLayout.addWidget(self.assessment_name, 1, 0, 1, 1)
    self.label = QtWidgets.QLabel(self.centralwidget)
    self.label.setObjectName("label")
    self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
    self.submit_grades = QtWidgets.QPushButton(self.centralwidget)
    self.submit_grades.setObjectName("submit_grades")
    self.gridLayout.addWidget(self.submit_grades, 6, 0, 1, 3)
    self.assessment_name5 = QtWidgets.QLineEdit(self.centralwidget)
    self.assessment_name5.setObjectName("assessment_name5")
    self.gridLayout.addWidget(self.assessment_name5, 5, 0, 1, 1)
    self.assessment_name2 = QtWidgets.QLineEdit(self.centralwidget)
    self.assessment_name2.setObjectName("assessment_name2")
    self.gridLayout.addWidget(self.assessment_name2, 2, 0, 1, 1)
    self.assessment_name4 = QtWidgets.QLineEdit(self.centralwidget)
    self.assessment_name4.setObjectName("assessment_name4")
    self.gridLayout.addWidget(self.assessment_name4, 4, 0, 1, 1)
    self.assessment_name3 = QtWidgets.QLineEdit(self.centralwidget)
    self.assessment_name3.setObjectName("assessment_name3")
    self.gridLayout.addWidget(self.assessment_name3, 3, 0, 1, 1)
    self.letter_grade5 = QtWidgets.QComboBox(self.centralwidget)
    self.letter_grade5.setObjectName("letter_grade5")
    self.letter_grade5.addItem("")
    self.letter_grade5.addItem("")
    self.letter_grade5.addItem("")
    self.letter_grade5.addItem("")
    self.letter_grade5.addItem("")
    self.letter_grade5.addItem("")
    self.letter_grade5.addItem("")
    self.letter_grade5.addItem("")
    self.letter_grade5.addItem("")
    self.letter_grade5.addItem("")
    self.letter_grade5.addItem("")
    self.letter_grade5.addItem("")
    self.gridLayout.addWidget(self.letter_grade5, 5, 1, 1, 2)
    self.letter_grade3 = QtWidgets.QComboBox(self.centralwidget)
    self.letter_grade3.setObjectName("letter_grade3")
    self.letter_grade3.addItem("")
    self.letter_grade3.addItem("")
    self.letter_grade3.addItem("")
    self.letter_grade3.addItem("")
    self.letter_grade3.addItem("")
    self.letter_grade3.addItem("")
    self.letter_grade3.addItem("")
    self.letter_grade3.addItem("")
    self.letter_grade3.addItem("")
    self.letter_grade3.addItem("")
    self.letter_grade3.addItem("")
    self.letter_grade3.addItem("")
    self.gridLayout.addWidget(self.letter_grade3, 3, 1, 1, 2)
    self.letter_grade4 = QtWidgets.QComboBox(self.centralwidget)
    self.letter_grade4.setObjectName("letter_grade4")
    self.letter_grade4.addItem("")
    self.letter_grade4.addItem("")
    self.letter_grade4.addItem("")
    self.letter_grade4.addItem("")
    self.letter_grade4.addItem("")
    self.letter_grade4.addItem("")
    self.letter_grade4.addItem("")
    self.letter_grade4.addItem("")
    self.letter_grade4.addItem("")
    self.letter_grade4.addItem("")
    self.letter_grade4.addItem("")
    self.letter_grade4.addItem("")
    self.gridLayout.addWidget(self.letter_grade4, 4, 1, 1, 2)
    self.letter_grade2 = QtWidgets.QComboBox(self.centralwidget)
    self.letter_grade2.setObjectName("letter_grade2")
    self.letter_grade2.addItem("")
    self.letter_grade2.addItem("")
    self.letter_grade2.addItem("")
    self.letter_grade2.addItem("")
    self.letter_grade2.addItem("")
    self.letter_grade2.addItem("")
    self.letter_grade2.addItem("")
    self.letter_grade2.addItem("")
    self.letter_grade2.addItem("")
    self.letter_grade2.addItem("")
    self.letter_grade2.addItem("")
    self.letter_grade2.addItem("")
    self.gridLayout.addWidget(self.letter_grade2, 2, 1, 1, 2)
    self.letter_grade1 = QtWidgets.QComboBox(self.centralwidget)
    self.letter_grade1.setObjectName("letter_grade1")
    self.letter_grade1.addItem("")
    self.letter_grade1.addItem("")
    self.letter_grade1.addItem("")
    self.letter_grade1.addItem("")
    self.letter_grade1.addItem("")
    self.letter_grade1.addItem("")
    self.letter_grade1.addItem("")
    self.letter_grade1.addItem("")
    self.letter_grade1.addItem("")
    self.letter_grade1.addItem("")
    self.letter_grade1.addItem("")
    self.letter_grade1.addItem("")
    self.gridLayout.addWidget(self.letter_grade1, 1, 1, 1, 2)
    self.label_3 = QtWidgets.QLabel(self.centralwidget)
    self.label_3.setAlignment(QtCore.Qt.AlignCenter)
    self.label_3.setObjectName("label_3")
    self.gridLayout.addWidget(self.label_3, 0, 1, 1, 2)
    unweight5.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(unweight5)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 424, 22))
    self.menubar.setObjectName("menubar")
    unweight5.setMenuBar(self.menubar)
    self.statusbar = QtWidgets.QStatusBar(unweight5)
    self.statusbar.setObjectName("statusbar")
    unweight5.setStatusBar(self.statusbar)

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

def retranslateUi(self, unweight5):
    _translate = QtCore.QCoreApplication.translate
    unweight5.setWindowTitle(_translate("unweight5", "Unweighted Calculator"))
    self.label.setText(_translate("unweight5", "Course Name"))
    self.submit_grades.setText(_translate("unweight5", "Submit"))
    self.letter_grade5.setItemText(0, _translate("unweight5", "A+"))
    self.letter_grade5.setItemText(1, _translate("unweight5", "A"))
    self.letter_grade5.setItemText(2, _translate("unweight5", "A-"))
    self.letter_grade5.setItemText(3, _translate("unweight5", "B+"))
    self.letter_grade5.setItemText(4, _translate("unweight5", "B"))
    self.letter_grade5.setItemText(5, _translate("unweight5", "B-"))
    self.letter_grade5.setItemText(6, _translate("unweight5", "C+"))
    self.letter_grade5.setItemText(7, _translate("unweight5", "C"))
    self.letter_grade5.setItemText(8, _translate("unweight5", "C-"))
    self.letter_grade5.setItemText(9, _translate("unweight5", "D+"))
    self.letter_grade5.setItemText(10, _translate("unweight5", "D"))
    self.letter_grade5.setItemText(11, _translate("unweight5", "F"))
    self.letter_grade3.setItemText(0, _translate("unweight5", "A+"))
    self.letter_grade3.setItemText(1, _translate("unweight5", "A"))
    self.letter_grade3.setItemText(2, _translate("unweigt5", "A-"))
    self.letter_grade3.setItemText(3, _translate("unweight5", "B+"))
    self.letter_grade3.setItemText(4, _translate("unweight5", "B"))
    self.letter_grade3.setItemText(5, _translate("unweight5", "B-"))
    self.letter_grade3.setItemText(6, _translate("unweight5", "C+"))
    self.letter_grade3.setItemText(7, _translate("unweight5", "C"))
    self.letter_grade3.setItemText(8, _translate("unweight5", "C-"))
    self.letter_grade3.setItemText(9, _translate("unweight5", "D+"))
    self.letter_grade3.setItemText(10, _translate("unweight5", "D"))
    self.letter_grade3.setItemText(11, _translate("unweight5", "F"))
    self.letter_grade4.setItemText(0, _translate("unweight5", "A+"))
    self.letter_grade4.setItemText(1, _translate("unweight5", "A"))
    self.letter_grade4.setItemText(2, _translate("unweight5", "A-"))
    self.letter_grade4.setItemText(3, _translate("unweight5", "B+"))
    self.letter_grade4.setItemText(4, _translate("unweight5", "B"))
    self.letter_grade4.setItemText(5, _translate("unweight5", "B-"))
    self.letter_grade4.setItemText(6, _translate("unweight5", "C+"))
    self.letter_grade4.setItemText(7, _translate("unweight5", "C"))
    self.letter_grade4.setItemText(8, _translate("unweight5", "C-"))
    self.letter_grade4.setItemText(9, _translate("unweight5", "D+"))
    self.letter_grade4.setItemText(10, _translate("unweight5", "D"))
    self.letter_grade4.setItemText(11, _translate("unweight5", "F"))
    self.letter_grade2.setItemText(0, _translate("unweight5", "A+"))
    self.letter_grade2.setItemText(1, _translate("unweight5", "A"))
    self.letter_grade2.setItemText(2, _translate("unweight5", "A-"))
    self.letter_grade2.setItemText(3, _translate("unweight5", "B+"))
    self.letter_grade2.setItemText(4, _translate("unweight5", "B"))
    self.letter_grade2.setItemText(5, _translate("unweight5", "B-"))
    self.letter_grade2.setItemText(6, _translate("unweight5", "C+"))
    self.letter_grade2.setItemText(7, _translate("unweight5", "C"))
    self.letter_grade2.setItemText(8, _translate("unweight5", "C-"))
    self.letter_grade2.setItemText(9, _translate("unweight5", "D+"))
    self.letter_grade2.setItemText(10, _translate("unweight5", "D"))
    self.letter_grade2.setItemText(11, _translate("unweight5", "F"))
    self.letter_grade1.setItemText(0, _translate("unweight5", "A+"))
    self.letter_grade1.setItemText(1, _translate("unweight5", "A"))
    self.letter_grade1.setItemText(2, _translate("unweight5", "A-"))
    self.letter_grade1.setItemText(3, _translate("unweight5", "B+"))
    self.letter_grade1.setItemText(4, _translate("unweight5", "B"))
    self.letter_grade1.setItemText(5, _translate("unweight5", "B-"))
    self.letter_grade1.setItemText(6, _translate("unweight5", "C+"))
    self.letter_grade1.setItemText(7, _translate("unweight5", "C"))
    self.letter_grade1.setItemText(8, _translate("unweight5", "C-"))
    self.letter_grade1.setItemText(9, _translate("unweight5", "D+"))
    self.letter_grade1.setItemText(10, _translate("unweight5", "D"))
    self.letter_grade1.setItemText(11, _translate("unweight5", "F"))
    self.label_3.setText(_translate("unweight5", "Grade"))

    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        unweight5 = QtWidgets.QMainWindow()
        ui = Ui_unweight5()
        ui.setupUi(unweight5)
        unweight5.show()
        sys.exit(app.exec_())

感谢您的宝贵时间!

假设您采样的 .py 文件名为 design.py 您必须创建另一个 .py 文件,其逻辑是避免在单个文件中包含多行代码。

解决这个问题,想法是查看 Table 其中选项通过字典与值相关,然后计算平均值。

from PyQt5 import QtCore, QtGui, QtWidgets
from design import Ui_unweight5

class Unweight5(QtWidgets.QMainWindow, Ui_unweight5):
    def __init__(self, parent=None):
        super(Unweight5, self).__init__(parent)
        self.setupUi(self)

        self.lut = {"A+": 4.0, 
                    "A":  3.5, 
                    "A-": 3.2, 
                    "B+": 3.0, 
                    "B":  2.8, 
                    "B-": 2.7, 
                    "C+": 2.5, 
                    "C":  2.1, 
                    "C-": 1.5, 
                    "D+": 1, 
                    "D":  0.6, 
                    "F":  0.4}

        self.submit_grades.clicked.connect(self.on_clicked)

    def on_clicked(self):
        combos = (self.letter_grade1, self.letter_grade2, self.letter_grade3, self.letter_grade4, self.letter_grade5)
        vals = [self.lut[combo.currentText()] for combo in combos]
        print("results:", sum(vals)/len(vals))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Unweight5()
    w.show()
    sys.exit(app.exec_())