Example #1
0
    def OnInputChanged(self, *args): #args just catches tkinter arguments. I don't need them
        self.operand1 = (self.input_operand1.get(), Base.stringToBase(self.input_base1.get()))
        self.operand2 = (self.input_operand2.get(), Base.stringToBase(self.input_base2.get()))
        operator = Operator.stringToOperator(self.operator.get())

        if self.canCalculate(operator):
            operand1 = convert(self.operand1[0], self.operand1[1], Base.decimal)

            # edge case not operation, operand2 could be invalid
            if operator != Operator.NOT:
                operand2 = convert(self.operand2[0], self.operand2[1], Base.decimal)
            else:
                operand2 = None

            # edge case div by zero
            if operator == Operator.DIV and float(operand2) == 0:
                self.outputError('2', "Can't divide by 0")
                self.outputResults('result', clear=True)
                return

            try:
                self.result = (result(operand1, operand2, operator), Base.decimal)
                self.outputResults('result')
            except Exception as e:
                print("Calculation: ", e)
        else:
            self.outputResults('result', clear=True)
Example #2
0
 def outputResults(self, box, clear=False):
     if box == '1':
         if not clear:
             dec = convert(self.operand1[0], self.operand1[1], Base.decimal)
             hexa = convert(self.operand1[0], self.operand1[1], Base.hexadecimal)
             binary = convert(self.operand1[0], self.operand1[1], Base.binary)
             sem = convert(self.operand1[0], self.operand1[1], Base.sem)
         else:
             dec = ''
             hexa = ''
             binary = ''
             sem = ''
         self.output_decimalOperand1.set(dec)
         self.output_hexOperand1.set(hexa)
         self.output_binaryOperand1.set(binary)
         self.output_semOperand1.set(sem)
     elif box == '2':
         if not clear:
             dec = convert(self.operand2[0], self.operand2[1], Base.decimal)
             hexa = convert(self.operand2[0], self.operand2[1], Base.hexadecimal)
             binary = convert(self.operand2[0], self.operand2[1], Base.binary)
             sem = convert(self.operand2[0], self.operand2[1], Base.sem)
         else:
             dec = ''
             hexa = ''
             binary = ''
             sem = ''
         self.output_decimalOperand2.set(dec)
         self.output_hexOperand2.set(hexa)
         self.output_binaryOperand2.set(binary)
         self.output_semOperand2.set(sem)
     elif box == 'result':
         if not clear:
             dec = convert(self.result[0], self.result[1], Base.decimal)
             hexa = convert(self.result[0], self.result[1], Base.hexadecimal)
             binary = convert(self.result[0], self.result[1], Base.binary)
             sem = convert(self.result[0], self.result[1], Base.sem)
         else:
             dec = ''
             hexa = ''
             binary = ''
             sem = ''
         self.output_decimalResult.set(dec)
         self.output_hexResult.set(hexa)
         self.output_binaryResult.set(binary)
         self.output_semResult.set(sem)
     else:
         raise ValueError("box is invalid")