views:

84

answers:

1

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.get())
        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 

# define undefined functions for sufficiently liberal meanings of "define"
Gui.on_press_enter = Gui.on_equal_btn_click
Gui.on_number1_button_click = Gui.on_equal_btn_click

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

Corrected:

My first answer was totally mistaken, ignore it. The problem was that you were accidentally overwriting your entry variable text with an a string representation of the entryVariable object. Note the addition of a get() in the call to Calc():

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

welcome to weakly typed languages, that sort of bug can drive you up a wall. I find liberal use of print and repr() (or print('foo %r' % object)) to be invaluable in such times.

msw
Im not sure I understand what your trying to get at. The variable Im trying to pass the value of is a StringVar and I am using the get() method... could please elaborate a little.. what should I change? Thanks
volting
wow, was I mistaken, please see **Corrected**
msw
Thanks, I guessed it something stupid but I didn't expect it to be that obvious... I think the problem is Im still in structured paradigm mode! One thing though this method of interaction obviously works, but it is standard and accepted way to implement interaction or is there a better way?
volting
I'm of the increasing belief that Tk and Python are a bad mix, as you've got code expecting a Tcl interpreter beneath it with a subtly different interpreter below. Tcl/Tk was very expressive, Tkinter seems a good way to make bad code (yes, Tcl is application scalability undefined which is why I no longer use it). I'm currently looking at PyGTK...
msw
Interesting I hadn't thought about it much to be honest. I choose tkinter as seemed to be the most popular. I will only be using it for small apps so I will stick with it for the moment for its ease of use(and to learn GUI design), I plan to learn QT for c++ in a few months so maybe Ill try pyQT then also. Thanks again
volting