我有一个PyQt程序,在这个项目中我有一些基于早期设置首选项的计算。
例如,我创建了一个包含以下文件的小型计算器:
example_run_vars.py :
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(324, 332)
self.gridLayout = QtWidgets.QGridLayout(Dialog)
self.gridLayout.setObjectName("gridLayout")
self.B = QtWidgets.QLabel(Dialog)
self.B.setObjectName("B")
self.gridLayout.addWidget(self.B, 0, 0, 1, 1)
self.A_val = QtWidgets.QLineEdit(Dialog)
self.A_val.setObjectName("A_val")
self.gridLayout.addWidget(self.A_val, 0, 1, 1, 1)
self.A = QtWidgets.QLabel(Dialog)
self.A.setObjectName("A")
self.gridLayout.addWidget(self.A, 1, 0, 1, 1)
self.B_val = QtWidgets.QLineEdit(Dialog)
self.B_val.setObjectName("B_val")
self.gridLayout.addWidget(self.B_val, 1, 1, 1, 1)
self.Zerocheck = QtWidgets.QCheckBox(Dialog)
self.Zerocheck.setObjectName("Zerocheck")
self.gridLayout.addWidget(self.Zerocheck, 1, 2, 1, 1)
self.math = QtWidgets.QComboBox(Dialog)
self.math.setObjectName("math")
self.gridLayout.addWidget(self.math, 2, 1, 1, 1)
self.math_else = QtWidgets.QComboBox(Dialog)
self.math_else.setObjectName("math_else")
self.gridLayout.addWidget(self.math_else, 3, 1, 1, 1)
self.Run = QtWidgets.QPushButton(Dialog)
self.Run.setObjectName("Run")
self.gridLayout.addWidget(self.Run, 4, 1, 1, 1)
self.Answer = QtWidgets.QLineEdit(Dialog)
self.Answer.setReadOnly(False)
self.Answer.setObjectName("Answer")
self.gridLayout.addWidget(self.Answer, 5, 1, 1, 1)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.B.setText(_translate("Dialog", "A: "))
self.A.setText(_translate("Dialog", "B: "))
self.Zerocheck.setText(_translate("Dialog", "ZeroCheck"))
self.Run.setText(_translate("Dialog", "Run"))
example.py :
import sys, os
from PyQt5 import QtCore, QtGui, QtWidgets
from example_run_vars import Ui_Dialog
class example(QtWidgets.QDialog):
def __init__(self, parent=None):
# QtGui.QWidget.__init__(self, parent) # PyQt4
super(example, self).__init__(parent) # pyQt5
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.retranslateUi(self)
self.ui.math.addItem('Add')
self.ui.math.addItem('Substract')
self.ui.math.addItem('Multiply')
self.ui.math.addItem('Divide')
self.ui.math.addItem('Power')
self.ui.math.addItem('Sqrt')
self.ui.math.activated[str].connect(self.dosomething)
self.ui.math_else.addItem('math function x')
self.ui.math_else.activated[str].connect(self.dosomething)
self.ui.Zerocheck.setChecked(False)
self.ui.Zerocheck.clicked.connect( self.dosomething)
self.ui.Run.clicked.connect(self.calculate)
def a_changed(self):
pass
def b_changed(self):
pass
def calculate(self):
# return value in Answer text box
pass
def dosomething(self):
# arbritary thing i realy don't know if i need it...
pass
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
exampleDialog = QtWidgets.QDialog()
examplepanel = example()
examplepanel.show()
sys.exit(app.exec_())
我的想法是,当我按下脚本中的运行按钮时,它会在文本框中给出答案。关键是我不知道返回答案的最简单(或程序上最好)方法是什么。
我知道有两个或树选项:
处理各种情况的最佳方法是什么?为什么?
答案 0 :(得分:2)
数据已经存储在小部件中,因此制作它的另一个副本效率很低。此外,您需要为每个单独的窗口小部件设置处理程序,这是不必要的复杂。通常,最好在计算时收集小部件中的所有信息。
在你的例子中,我建议你保持calculate
插槽连接到运行按钮,但摆脱所有其他信号/插槽。然后,您应该为所需的所有数学运算创建函数。这些函数中的每一个都必须有两个参数,这些参数将从行编辑中获取A / B值。你可以使用operator module中的一些函数,或者只编写自己的函数:
def add_func(a, b):
return a + b
然后,您可以将这些功能直接链接到组合框项目,如下所示:
self.ui.math.addItem('Add', add_func)
self.ui.math.addItem('Substract', sub_func)
self.ui.math.addItem('Multiply', mult_func)
有了这个,calculate
插槽就会显示如下:
def calculate(self):
a = float(self.ui.A_val.text())
b = float(self.ui.B_val.text())
# get the linked function from the combo-box
func = self.ui.math.currentData()
# calculate the answer
answer = func(a, b)
# show the new answer
self.ui.Answer.setText(str(answer))
这样做可以很容易地添加,删除或重新排序组合框中的功能(即calculate
槽永远不必更改。)