区块链技术博客
www.b2bchain.cn

单击按钮后如何从组合框更改变量值? – python程序员分享

D0b2wT.gif

本文介绍了单击按钮后如何从组合框更改变量值? – python程序员分享,有助于帮助完成毕业设计以及求职,是一篇很好的资料。

对技术面试,学习经验等有一些体会,在此分享。

我正在使用未加权GPA计算器,并且对(Py)Qt Designer应用程序还是有点陌生​​。我遇到了一个问题,我不知道如何从ComboBoxes中获取结果以将其计算为名为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_()) 

Python uuid4,如何限制唯一字符的长度 – python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

如何使用pass语句? – python

我正在学习Python,并且已经到达有关pass语句的部分。我正在使用的指南将其定义为通常用作占位符的Null语句。我仍然不完全明白那是什么意思。有人可以告诉我一个简单的/基本情况,在其中使用pass语句以及为什么需要它吗? 参考方案 假设您正在使用尚未实现的某些方法设计一个新类。class MyClass(object): def meth_a(self)…

Python-crontab模块 – python

我正在尝试在Linux OS(CentOS 7)上使用Python-crontab模块我的配置文件如下:{ "ossConfigurationData": { "work1": [ { "cronInterval": "0 0 0 1 1 ?", "attribute&…

Python:检查是否存在维基百科文章 – python

我试图弄清楚如何检查Wikipedia文章是否存在。例如,https://en.wikipedia.org/wiki/Food 存在,但是https://en.wikipedia.org/wiki/Fod 不会,页面只是说:“维基百科没有此名称的文章。”谢谢! 参考方案 >>> import urllib >>> prin…

Python Pandas导出数据 – python

我正在使用python pandas处理一些数据。我已使用以下代码将数据导出到excel文件。writer = pd.ExcelWriter('Data.xlsx'); wrong_data.to_excel(writer,"Names which are wrong", index = False); writer.…

部分转自互联网,侵权删除联系

赞(0) 打赏
部分文章转自网络,侵权联系删除b2bchain区块链学习技术社区 » 单击按钮后如何从组合框更改变量值? – python程序员分享
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

b2b链

联系我们联系我们