class CalculatorGUI(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.calculator=CalculatorModel()
        self.calculator.addObserver(self)
        self.pack()
        self._create_widgets()

    def _create_widgets(self):

        self.Display = Entry(self)
        self.Display.pack({"side": "left"})

        Operators = Frame(self)
        self.PlusBtn = Button(Operators)
        self.PlusBtn["text"] = "+"
        self.PlusBtn["command"] = self.press_plus
        self.PlusBtn.pack({"side": "left"})
        self.MinusBtn = Button(Operators)
        self.MinusBtn["text"] = "-"
        self.MinusBtn["command"] = None
        self.MinusBtn.pack({"side": "left"})
        self.EqualsBtn = Button(Operators)
        self.EqualsBtn["text"] = "="
        self.EqualsBtn["command"] = self.press_equal
        self.EqualsBtn.pack({"side": "left"})
        Operators.pack()

        Digits = Frame(self)
        self.DigitBtn = []
        for i in range(0,10):
            TheButton = Button(Digits, text=str(i))
            TheButton.pack(side=LEFT)
            self.DigitBtn = self.DigitBtn + [TheButton]
        Digits.pack()
        self.DigitBtn[0]["command"] = lambda: self.press_digit(0)
        self.DigitBtn[1]["command"] = lambda: self.press_digit(1)
        self.DigitBtn[2]["command"] = lambda: self.press_digit(2)
        self.DigitBtn[3]["command"] = lambda: self.press_digit(3)
        self.DigitBtn[4]["command"] = lambda: self.press_digit(4)
        self.DigitBtn[5]["command"] = lambda: self.press_digit(5)
        self.DigitBtn[6]["command"] = lambda: self.press_digit(6)
        self.DigitBtn[7]["command"] = lambda: self.press_digit(7)
        self.DigitBtn[8]["command"] = lambda: self.press_digit(8)
        self.DigitBtn[9]["command"] = lambda: self.press_digit(9)

    def update(self):
        self.Display.delete(0, END)
        self.Display.insert(0, self.calculator.display())
        print(self.calculator.display())

    def press_digit(self, digit):
        self.calculator.press_digit(digit)

    def press_plus(self):
        self.calculator.press_plus()

    def press_equal(self):
        self.calculator.press_equals()
 def setUp(self):
     self.c = CalculatorModel()
class TestCalc( unittest.TestCase ):

    def setUp(self):
        self.c = CalculatorModel()

    def tearDown(self):
        self.c = None

    def test_work(self):
        print(self)

    def test_fail(self):
        self.assertTrue(not False)

    def testShouldShowZeroWhenInitialized(self):
        self.assertEqual("0", self.c.display())

    def testShouldReturnOneWhenOneIsPressed(self):
        self.c.press_digit(1);
        self.assertEqual("1", self.c.display())

    def testShouldReturnTwoWhenOnePlusOne(self):
        self.c.press_digit(1);
        self.assertEqual("1", self.c.display())

        self.c.press_plus();
        self.assertEqual("1", self.c.display())

        self.c.press_digit(1);
        self.assertEqual("1", self.c.display())

        self.c.press_equals();
        self.assertEqual("2", self.c.display())

    def testMinus(self):
        self.c.press_minus()
        self.c.press_digit(1)
        self.c.press_equals()
        self.assertEqual("-1", self.c.display())

    def testMult(self):
        self.c.press_digit(2)
        self.c.press_mult()
        self.c.press_digit(2)
        self.c.press_equals()
        self.assertEqual("4", self.c.display(), "2*2=4")

    def testShouldRefuseStrings(self):
        self.assertRaises(CalcError, self.c.press_digit, "aas")

    def testShouldRefuseMoreThanOneDigit(self):
        self.assertRaises(CalcError, self.c.press_digit, 99)
 def __init__(self, master=None):
     Frame.__init__(self, master)
     self.calculator=CalculatorModel()
     self.calculator.addObserver(self)
     self.pack()
     self._create_widgets()
Beispiel #5
0
from view import View
from controller import Controller
from calculator_model import CalculatorModel
if __name__ == '__main__':
    ''' Main function, instantiate instances of Model, View and a Controller'''

    model = CalculatorModel()
    view = View()

    controller = Controller(model=model, view=view)
    controller.run()
Beispiel #6
0
from view import View
from controller import Controller
from calculator_model import CalculatorModel

if __name__ == '__main__':
    ''' Main function, instantiate instances of Model, View and a Controller'''

    model = CalculatorModel()
    view = View()
    model.attach(view)
    controller = Controller(model=model, view=view)
    controller.run()
class CalculatorViewModel(BaseViewModel):
    """
    The view model is the data-context for the view: It formats the data in order to be presented
    and communicated with the model
    """

    name = 'calculator'  # this will be the variable name of this data-context in context-aware data-contexts

    @property
    def display_content(self):
        """
        Gets the content to be displayed in the display of the calculator
        """
        if self._has_error:  # check error flag
            self._has_error = False  # reset error flag
            return 'ZERO DIVISION ERROR'  # get error message

        # build display string from model data
        content = (''.join(['{}' + op for op in self._calculator.operations]) +
                   '{}').format(*self._calculator.operands)
        return content

    def __init__(self):
        super().__init__()  # initialize data-context
        self._calculator = CalculatorModel()  # create instance of model
        self._has_error = False  # initialize error flag

    def enter_number(self, number):
        """
        Add digit to current number
        :param number: digit
        """
        value = self._calculator.operands[
            -1]  # get last number of expression in display
        self._calculator.operands[
            -1] = 10 * value + number  # shift last number and add new digit
        self.notify_change('display_content')  # notify view about change

    def enter_operator(self, operator):
        """
        Add operator to expression
        :param operator: +, -, *, /
        """
        self._calculator.operations.append(operator)  # append operator
        self._calculator.operands.append(
            0)  # append zero in order for the expression to stay valid
        self.notify_change('display_content')  # notify view about change

    def clear(self, *args):
        """
        Set expression to zero
        :param args: Argument needed to be valid Qt slot
        """
        self._calculator.clear()
        self.notify_change('display_content')  # notify view about change

    def evaluate(self, *args):
        """
        Evaluate current expression
        :param args: Argument needed to be valid Qt slot
        """
        try:
            self._calculator.evaluate()  # evaluate expression in model
        except ZeroDivisionError:  # catch zero-division errors
            self._calculator.clear()  # clear calculator
            self._has_error = True  # set error flag to true

        self.notify_change('display_content')  # notify view about change
 def __init__(self):
     super().__init__()  # initialize data-context
     self._calculator = CalculatorModel()  # create instance of model
     self._has_error = False  # initialize error flag