[Tutor] Code with traceback...Re: Help me.. problem in building calculator application
Kent Johnson
kent37 at tds.net
Thu Oct 26 16:01:42 CEST 2006
Immediately following the line
self.str1 = StringVar()
you have
self.str1 = "0"
at which point self.str1 is a string, not a StringVar. Maybe you mean
self.str1.set("0")
?
Kent
Asrarahmed Kadri wrote:
>
> Traceback is as under:
>
> Traceback (most recent call last):
> File "C:\python\lib\lib-tk\Tkinter.py", line 1345, in __call__
> return self.func(*args)
> File "calculator_version2.py", line 105, in <lambda>
> self.buttonPlu = Button(self.container3,text='+',command = lambda:
> self.oprC
> lick('+',self.str1.get()))
> AttributeError: 'str' object has no attribute 'get'
>
>
>
> from Tkinter import *
> import string
>
>
> class Mycalc:
>
> def buttonClick(self,str2,str3):
> if str2 == ".":
> if self.dotFlag:
> pass
> else:
> self.dotFlag = True
> self.operand = str3 + str2
> else:
> self.operand = str3 + str2
>
>
>
> def oprClick(self,str11,oprt):
>
> self.dotFlag = False
>
> if oprt == "+":
> if self.result == "0":
> self.result = str11
> else:
> self.result = string.atof(self.result) + string.atof(str11)
>
>
>
> elif oprt == "-":
> if self.result == "0":
> self.result = str11
> else:
> self.result = string.atof(self.result) - string.atof(str11)
>
>
> elif str2 == "*":
> if self.result == "0":
> self.result = str11
> else:
> self.result = string.atof(self.result) * string.atof(str11)
>
>
> elif str2 == "/":
> if self.result == "0":
> self.result = str11
> else:
> self.result = string.atof (self.result) / string.atof(str11)
>
>
> self.entryWidget.icursor(0)
> self.entryWidget.delete(0,END)
> self.entryWidget.insert(INSERT,self.result)
>
>
>
>
>
> def __init__(self,master):
>
> self.container1 =
> Frame(master,width=200,height=200,background='brown')
> self.container1.pack()
> self.str1 = StringVar()
> self.str1 = "0"
> self.entryWidget = Entry(
> self.container1,textvariable=self.str1)
> self.entryWidget.pack(side=TOP)
>
> self.container2 = Frame(master,background='cyan')
> self.container2.pack()
>
> self.container3 = Frame(master,background='grey')
> self.container3.pack()
>
> self.operand = "0"
> self.result = "0"
> self.dotFlag = False
>
> def make_buttons(self):
> self.buttonOne = Button(self.container2,text='1')
> self.buttonOne.grid(row=0,column=0)
> self.buttonTwo = Button(self.container2,text='2')
> self.buttonTwo.grid (row=0,column=1)
> self.buttonThree = Button(self.container2,text='3')
> self.buttonThree.grid(row=0,column=2)
> self.buttonFour = Button(self.container2,text='4')
> self.buttonFour.grid (row=1,column=0)
> self.buttonFive = Button(self.container2,text='5')
> self.buttonFive.grid(row=1,column=1)
> self.buttonSix = Button(self.container2,text='6')
> self.buttonSix.grid(row=1,column=2)
> self.buttonSeven = Button(self.container2,text='7')
> self.buttonSeven.grid(row=2,column=0)
> self.buttonEight = Button(self.container2,text='8')
> self.buttonEight.grid(row=2,column=1)
> self.buttonNine = Button(self.container2,text='9')
> self.buttonNine.grid(row=2,column=2)
> self.buttonZero = Button(self.container2,text='0')
> self.buttonZero.grid(row=3,column=0)
> self.buttonPoint = Button(self.container2,text='.')
> self.buttonPoint.grid(row=3,column=1)
>
> def make_oprbuttons(self):
>
> self.buttonPlu = Button(self.container3,text='+',command =
> lambda: self.oprClick('+',self.str1.get()))
> self.buttonPlu.pack(side=LEFT)
>
> self.buttonMin = Button(self.container3,text='-',command =
> lambda: self.oprClick('-',self.str1.get()))
> self.buttonMin.pack(side=LEFT)
>
> self.buttonMul = Button(self.container3,text='*',command =
> lambda: self.oprClick('*',self.str1.get()))
> self.buttonMul.pack(side=LEFT)
>
> self.buttonDiv = Button(self.container3,text='/',command =
> lambda: self.oprClick('/',self.str1.get()))
> self.buttonDiv.pack(side=LEFT)
>
> self.buttonEqu = Button(self.container3,text='=',command =
> lambda: self.oprClick('=',self.str1.get()))
> self.buttonEqu.pack(side=LEFT)
>
>
>
> root = Tk()
> mycalc = Mycalc(root)
> mycalc.make_buttons()
> mycalc.make_oprbuttons()
>
> root.mainloop()
>
>
> On 10/26/06, *Kent Johnson* <kent37 at tds.net <mailto:kent37 at tds.net>> wrote:
>
> Asrarahmed Kadri wrote:
> >
> >
> > Hi folks,
> >
> > I am trying to build a rudimentary calculator application using
> Tkinter...
> >
> > I have used one Entry widget for displaying the input and output
> data.
> > All this is put in a class.
> > The porblem is when I click teh '+' button, teh callback function is
> > called but it gives me an error message: AttributeError: 'str'
> object
> > has no attribute 'get'
> >
> > I have defined a variable, self.str1 = StringVar() to bind it
> with the
> > entry widget and I am calling the function when + is clciked. The
> > function is as follows:
> >
> > self.buttonPlu = Button(self.container3,text='+',command = lambda:
> > self.oprClick('+',*self.str1.get()))*
> >
> > Can't we use the get() method to access the contents of the entry
> > widget. Is there any other way to access the contents of the entry
> > widget....???
>
> The error message is saying that the object you are calling get() on is
> a string, not a StringVar. Please post complete code and the complete
> error message including traceback.
>
> Kent
>
> _______________________________________________
> Tutor maillist - Tutor at python.org <mailto:Tutor at python.org>
> http://mail.python.org/mailman/listinfo/tutor
> <http://mail.python.org/mailman/listinfo/tutor>
>
>
>
>
> --
> To HIM you shall return.
More information about the Tutor
mailing list