class GuiView(Tk.Frame):
    VALID_OPERATIONS = ['+', '-', '*']
    view_model = PolyViewModel()

    def clck_bttn(self):
        self.view_model.set_first_poly(self.first_poly_txt.get(1.0, Tk.END))
        self.view_model.set_second_poly(self.second_poly_txt.get(1.0, Tk.END))
        self.view_model.set_operation(
            self.VALID_OPERATIONS[self.cmb_operation.current()])
        self.view_model.computing()
        self.result_label.configure(text=self.view_model.get_result())

    def __init__(self):
        Tk.Frame.__init__(self)
        self.master.title("Polynomial calculator")
        self.master.minsize(width=150, height=150)

        self.grid(sticky=Tk.W + Tk.E + Tk.N + Tk.S)
        self.first_poly_label = Tk.Label(
            self,
            text="Enter the coefficients of the " +
            "first polynomial (type int) using ','",
            fg='black',
            font="Arial 12",
            bg="light yellow")

        self.first_poly_label.pack()
        self.first_poly_txt = Tk.Text(self, height=1, width=40)
        self.first_poly_txt.pack()
        self.operation_label = Tk.Label(self,
                                        text="Select an operation",
                                        fg='black',
                                        font="Arial 12",
                                        bg="light yellow")

        self.operation_label.pack()
        self.cmb_operation = ttk.Combobox(self,
                                          height=1,
                                          width=2,
                                          values=self.VALID_OPERATIONS)
        self.cmb_operation.current(0)
        self.cmb_operation.pack()
        self.second_poly_label = Tk.Label(
            self,
            text="Enter the coefficients of the " +
            "second polynomial (type int) using ','",
            fg='black',
            font="Arial 12",
            bg="light yellow")

        self.second_poly_label.pack()
        self.second_poly_txt = Tk.Text(self, height=1, width=40)
        self.second_poly_txt.pack()
        self.calculate = Tk.Button(self,
                                   text="Calculate",
                                   width=15,
                                   height=1,
                                   font="Arial 12",
                                   bg="light blue",
                                   command=self.clck_bttn)

        self.calculate.pack()
        self.result_label = Tk.Label(self,
                                     text="",
                                     fg='black',
                                     font="Arial 12",
                                     bg="light yellow")

        self.result_label.pack()
Example #2
0
 def test_init_first_poly(self):
     t = PolyViewModel()
     t.set_first_poly('1,2,3')
     self.assertEqual('1,2,3', t.get_first_poly())
class GuiView(Tk.Frame):
    VALID_OPERATIONS = ['+', '-', '*']
    N_LOG_MESSAGES_TO_DISPLAY = 8
    view_model = PolyViewModel()

    def convert_clicked(self, event):
        self.mvvm_bind()
        self.view_model.computing()
        self.mvvm_back_bind()

    def first_poly_txt_changed(self, event):
        self.mvvm_bind()
        self.mvvm_back_bind()

    def second_poly_txt_changed(self, event):
        self.mvvm_bind()
        self.mvvm_back_bind()

    def operation_changed(self, event):
        self.mvvm_bind()
        self.mvvm_back_bind()

    def bind_events(self):
        self.calculate.bind('<Button-1>', self.convert_clicked)
        self.first_poly_txt.bind('<KeyRelease>', self.first_poly_txt_changed)
        self.second_poly_txt.bind('<KeyRelease>', self.second_poly_txt_changed)
        self.cmb_operation.bind('<<ComboboxSelected>>', self.operation_changed)

    def mvvm_bind(self):
        self.view_model.set_first_poly(self.first_poly_txt.get("1.0", Tk.END))
        self.view_model.set_second_poly(self.second_poly_txt.get(
            "1.0", Tk.END))
        self.view_model.set_operation(
            self.VALID_OPERATIONS[self.cmb_operation.current()])

    def mvvm_back_bind(self):
        self.first_poly_txt.delete(1.0, Tk.END)
        self.first_poly_txt.insert(Tk.END, self.view_model.get_first_poly())

        self.second_poly_txt.delete(1.0, Tk.END)
        self.second_poly_txt.insert(Tk.END, self.view_model.get_second_poly())

        logger_text = '\n'.join(self.view_model.logger.get_log_messages()
                                [-self.N_LOG_MESSAGES_TO_DISPLAY:])
        self.result_label.config(text='%s\n%s' %
                                 (self.view_model.get_result(), logger_text))

    def __init__(self):
        Tk.Frame.__init__(self)
        self.master.title("Polynomial calculator")
        self.master.minsize(width=150, height=150)

        self.grid(sticky=Tk.W + Tk.E + Tk.N + Tk.S)
        self.first_poly_label = Tk.Label(
            self,
            text="Enter the coefficients of the " +
            "first polynomial (type int) using ','",
            fg='black',
            font="Arial 12",
            bg="light yellow")

        self.first_poly_label.pack()
        self.first_poly_txt = Tk.Text(self, height=1, width=40)
        self.first_poly_txt.pack()
        self.operation_label = Tk.Label(self,
                                        text="Select an operation",
                                        fg='black',
                                        font="Arial 12",
                                        bg="light yellow")

        self.operation_label.pack()
        self.cmb_operation = ttk.Combobox(self,
                                          height=1,
                                          width=2,
                                          values=self.VALID_OPERATIONS)
        self.cmb_operation.current(0)
        self.cmb_operation.pack()
        self.second_poly_label = Tk.Label(
            self,
            text="Enter the coefficients of the " +
            "second polynomial (type int) using ','",
            fg='black',
            font="Arial 12",
            bg="light yellow")

        self.second_poly_label.pack()
        self.second_poly_txt = Tk.Text(self, height=1, width=40)
        self.second_poly_txt.pack()
        self.calculate = Tk.Button(self,
                                   text="Calculate",
                                   width=15,
                                   height=1,
                                   font="Arial 12",
                                   bg="light blue")

        self.calculate.pack()
        self.result_label = Tk.Label(self,
                                     fg='black',
                                     font="Arial 12",
                                     bg="light yellow")

        self.result_label.pack()

        self.bind_events()
        self.mvvm_bind()
        self.mvvm_back_bind()
Example #4
0
 def test_computing_composition(self):
     t = PolyViewModel()
     t.set_first_poly('1,2,3')
     t.set_second_poly('4,5,6,7')
     t.set_operation('*')
     t.computing()
     self.assertEqual('4x^5 + 13x^4 + 28x^3 + 34x^2 + 32x + 21', t.get_result())
Example #5
0
 def test_computing_with_er_val(self):
     t = PolyViewModel()
     t.set_first_poly('1,2,3,a')
     t.set_second_poly('4,5,6,7')
     t.set_operation('-')
     t.computing()
     self.assertEqual('Coeff has no type int', t.get_result())
Example #6
0
 def test_computing_sub(self):
     t = PolyViewModel()
     t.set_first_poly('1,2,3')
     t.set_second_poly('4,5,6,7')
     t.set_operation('-')
     t.computing()
     self.assertEqual('-4x^3 + -4x^2 + -4x + -4', t.get_result())
Example #7
0
 def test_computing_add(self):
     t = PolyViewModel()
     t.set_first_poly('1,2,3')
     t.set_second_poly('4,5,6,7')
     t.set_operation('+')
     t.computing()
     self.assertEqual('4x^3 + 6x^2 + 8x + 10', t.get_result())
Example #8
0
 def test_init_operation(self):
     t = PolyViewModel()
     t.set_operation('-')
     self.assertEqual('-', t.get_operation())