How to achieve interaction between GUI class with logic class
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 suff开发者_如何学Pythoniciently 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()
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.
精彩评论