How to achieve interaction between GUI class with logic class

Posted by volting on Stack Overflow See other posts from Stack Overflow or by volting
Published on 2010-05-12T15:44:17Z Indexed on 2010/05/12 16:04 UTC
Read the original article Hit count: 194

Filed under:
|
|
|

Im new to GUI programming, and haven't done much OOP. Im working on a basic calculator app to help me learn GUI design and to brush up on OOP. I understand that anything GUI related should be kept seperate from the logic, but Im unsure how to implement interaction between logic an GUI classes when needed i.e. basically passing variables back and forth...

Im using TKinter and when I pass a tkinter variable to my logic it only seems to hold the string PY_VAR0.

def on_equal_btn_click(self):
            self.entryVariable.set(self.entryVariable.get() + "=")
            calculator = Calc(self.entryVariable)
            self.entryVariable.set(calculator.calculate())

Im sure that im probably doing something fundamentally wrong and probabaly really stupid, I spent a considerable amount of time experimenting (and searching for answers online) but Im getting no where. Any help would be appreciated.

Thanks, V

The Full Program (well just enough to show the structure..)

import Tkinter

class Gui(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()
        self.create_widgets()
        """ grid config """
        #self.grid_columnconfigure(0,weight=1,pad=0)
        self.resizable(False, False)

    def create_widgets(self):

        """row 0 of grid"""

        """Create Text Entry Box"""
        self.entryVariable = Tkinter.StringVar()
        self.entry = Tkinter.Entry(self,width=30,textvariable=self.entryVariable)
        self.entry.grid(column=0,row=0, columnspan = 3 )
        self.entry.bind("<Return>", self.on_press_enter)

        """create equal button"""
        equal_btn = Tkinter.Button(self,text="=",width=4,command=self.on_equal_btn_click)
        equal_btn.grid(column=3, row=0)

        """row 1 of grid"""

        """create number 1 button"""
        number1_btn = Tkinter.Button(self,text="1",width=8,command=self.on_number1_btn_click)
        number1_btn.grid(column=0, row=1)

        .
        .
        .

    def on_equal_btn_click(self):
        self.entryVariable.set(self.entryVariable.get() + "=")
        calculator = Calc(self.entryVariable)
        self.entryVariable.set(calculator.calculate())

class Calc():

    def __init__(self, equation):
        self.equation = equation

    def calculate(self):
        #TODO: parse string and calculate...
        return self.equation 



if __name__ == "__main__":
    app = Gui(None)
    app.title('Calculator')
    app.mainloop()   

© Stack Overflow or respective owner

Related posts about python

Related posts about gui