繁体   English   中英

单击按钮后如何从组合框更改变量值?

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

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

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

如果letter_grade1 ComboBox是A +,则它将gpa加4.0
如果letter_grade2 ComboBox是B,则它将gpa加3.0

然后再除以5,因为会打印出5个ComboBox,计算结果将被打印出来,而这一切都在单击commit_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文件,其逻辑是避免在单个文件中包含多行代码。

解决问题的办法是创建一个“查看表”,其中选项与通过字典的值相关,然后计算平均值。

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

暂无
暂无

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

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