def __init__(self, *args, **kwargs): super(Calculator, self).__init__(*args, **kwargs) # Single-inheritence approach: http://goo.gl/WNiHc # Calculator class only inherits from QWidget # A specific member attribute self.ui contains all # widgets set up in the designer. self.ui = Ui_Calculator() self.ui.setupUi(self) # Create a validator for each QLineEdit that only # allows a user to enter floats: 123.123 self.ui.inputA.setValidator(QtGui.QDoubleValidator()) self.ui.inputB.setValidator(QtGui.QDoubleValidator()) # instead of using the stock operator values set in the # ui file, lets set the box to match our class attribute self.ui.operatorBox.clear() self.ui.operatorBox.addItems(self.OPS.keys()) self.ui.clearButton.clicked.connect(self.clear) # every time the text is edited in either input field, # calculate the result live self.ui.inputA.textEdited.connect(self.calc) self.ui.inputB.textEdited.connect(self.calc) # also when the operator box is changed self.ui.operatorBox.currentIndexChanged.connect(self.calc)
def __init__(self, *args, **kwargs): super(Calculator, self).__init__(*args, **kwargs) # Single-inheritence approach: http://goo.gl/WNiHc # Calculator class only inherits from QWidget # A specific member attribute self.ui contains all # widgets set up in the designer. self.ui = Ui_Calculator() self.ui.setupUi(self) self.ui.calcButton = QtGui.QPushButton("Calculate") self.ui.horizontalLayout_2.addWidget(self.ui.calcButton) # Create a validator for each QLineEdit that only # allows a user to enter floats: 123.123 self.ui.inputA.setValidator(QtGui.QDoubleValidator()) self.ui.inputB.setValidator(QtGui.QDoubleValidator()) # instead of using the stock operator values set in the # ui file, lets set the box to match our class attribute self.ui.operatorBox.clear() self.ui.operatorBox.addItems(self.OPS.keys()) self.ui.clearButton.clicked.connect(self.clear) self.ui.calcButton.clicked.connect(self.calc)
class Calculator(QtGui.QWidget): OPS = { '+': operator.add, '-': operator.sub, '/': operator.div, '*': operator.mul } def __init__(self, *args, **kwargs): super(Calculator, self).__init__(*args, **kwargs) self.ui = Ui_Calculator() self.ui.setupUi(self) self.ui.inputA.setValidator(QtGui.QDoubleValidator(self)) self.ui.inputB.setValidator(QtGui.QDoubleValidator(self)) self.ui.operatorBox.clear() self.ui.operatorBox.addItems(self.OPS.keys()) self.ui.clearButton.clicked.connect(self.clear) self.ui.inputA.textEdited.connect(self.calc) self.ui.inputB.textEdited.connect(self.calc) self.ui.operatorBox.currentIndexChanged.connect(self.calc) # END def __init__ def clear(self): ''' Slot to clear the fors fields''' self.ui.inputA.clear() self.ui.inputB.clear() self.ui.result.clear() # END def clear def calc(self): op_str = str(self.ui.operatorBox.currentText()) op = self.OPS.get(op_str) if not op: return inputA = self.ui.inputA.text() inputB = self.ui.inputB.text() if not (inputA and inputB): return try: i1 = float(inputA) i2 = float(inputB) result = op(i1, i2) except Exception, e: QtGui.QMessageBox.warning(self, "Could not calculate result", "Result: \n%s" %e) else:
def __init__(self, *args, **kwargs): super(Calculator, self).__init__(*args, **kwargs) self.ui = Ui_Calculator() self.ui.setupUi(self) self.ui.inputA.setValidator(QtGui.QDoubleValidator(self)) self.ui.inputB.setValidator(QtGui.QDoubleValidator(self)) self.ui.operatorBox.clear() self.ui.operatorBox.addItems(self.OPS.keys()) self.ui.clearButton.clicked.connect(self.clear) self.ui.inputA.textEdited.connect(self.calc) self.ui.inputB.textEdited.connect(self.calc) self.ui.operatorBox.currentIndexChanged.connect(self.calc)
class Calculator(QtGui.QWidget): OPS = { '+': operator.add, '-': operator.sub, '/': operator.div, '*': operator.mul, } def __init__(self, *args, **kwargs): super(Calculator, self).__init__(*args, **kwargs) # Single-inheritence approach: http://goo.gl/WNiHc # Calculator class only inherits from QWidget # A specific member attribute self.ui contains all # widgets set up in the designer. self.ui = Ui_Calculator() self.ui.setupUi(self) # Create a validator for each QLineEdit that only # allows a user to enter floats: 123.123 self.ui.inputA.setValidator(QtGui.QDoubleValidator()) self.ui.inputB.setValidator(QtGui.QDoubleValidator()) # instead of using the stock operator values set in the # ui file, lets set the box to match our class attribute self.ui.operatorBox.clear() self.ui.operatorBox.addItems(self.OPS.keys()) self.ui.clearButton.clicked.connect(self.clear) # every time the text is edited in either input field, # calculate the result live self.ui.inputA.textEdited.connect(self.calc) self.ui.inputB.textEdited.connect(self.calc) # also when the operator box is changed self.ui.operatorBox.currentIndexChanged.connect(self.calc) def clear(self): """ Slot to clear the form fields """ self.ui.inputA.clear() self.ui.inputB.clear() self.ui.result.clear() def calc(self): """ Calculate the result from the form values """ op_str = str(self.ui.operatorBox.currentText()) op = self.OPS.get(op_str) if not op: return inputA = self.ui.inputA.text() inputB = self.ui.inputB.text() # just silently return if either field is empty if not (inputA and inputB): self.ui.result.clear() return try: i1 = float(inputA) i2 = float(inputB) result = op(i1, i2) except Exception, e: # inform the user if the operation results in # an error. Such as dividing by zero. QtGui.QMessageBox.warning(self, "Could not calculate results", "Reason:\n%s" % e) else: