So I've been trying to make a VERY simple calculator program, but I tried so many things, and all of my methods failed. I've been at this for weeks, and I'm pretty frustrated up to this point. Nothing calls back on the entry box correctly, and although at certain points I was able to make numbers appear on the entry widget, I couldn't interact with them (e.g. add, subtract, etc.)

For now, I'm trying to make my program add 1+1 so it'll equal 2. However, Python IDLE is telling me that I have indentation problems. As far as I know, everything looks fine. I also copied bits of another calculator program elsewhere. If anyone knows a detailed source on entry boxes, please let me know. Thanks in advance!

from Tkinter import *

root=Tk()

class Calculator(Frame):
    
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        display = StringVar()
        ent=Entry(self, textvariable=display).pack(side=TOP)

        self.onebutton=Button(self, text="1", command=self.number).pack()
        self.twobutton=Button(self, text="2", command=self.number).pack()
        self.eqlbutton=Button(self, text="=", command=self.equal).pack()
        
    def number(self, event):
        lambda w=display, s=' %s '%text:w.set(w.get() + s))

    def equal(self, event):
        lambda e, s=self, w=display: s.calc(w), '+')

    def calc(self, display):
        try:
            display.set(eval(display.get()))
        except:
            display.set("ERROR")
            
Calculator().mainloop()

Well I re-wrote the code to be a bit easier, and all that's left is the equal sign to work. This is my WIP.

from Tkinter import *

root=Tk()

class Calculator(Frame):
    
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.master.title("CalculatorGUI")
        display = StringVar()
        self.ent=Entry(self)
        self.ent.pack()

        onebutton=Button(self, text="1", command=self.one).pack()
        plsbutton=Button(self, text="+", command=self.pls).pack()
        eqlbutton=Button(self, text="=", command=self.eql).pack()
    def one(self):
        self.ent.insert(END, "1")
    def pls(self):
        self.ent.insert(END, "+")
    def eql(self):
        self.ent.get()=solution
        
Calculator().mainloop()

OK, so I got this message:

Error line 23: can't assign to function call.

What this means is that you have called self.ent.get(), and then tried to take the contents of solution and assign it to the result of the function call.

As you can see, that's definitely not what you wanted. By commenting out that line and replacing it with pass, you will see that the rest of your code works, except for eql(self).

What you need to do is take that single statement "self.ent.get()=solution" and unpack it a little bit. Callbacks don't have to be a single line of code, so think carefully about all that you want to accomplish with eql(self), and then break that into as simple steps as possible.

Jeff

Well I finally got my program working. It's just that I need to mess around with the .pack() function a little bit. I tried doing .pack(row=0, column=0) and stuff, but it tells me this:

Traceback (most recent call last):
File "/Users/radeux/Documents/Programming Files/CalculatorGUI.py", line 80, in <module>
Calculator().mainloop()
File "/Users/radeux/Documents/Programming Files/CalculatorGUI.py", line 16, in __init__
onebutton.pack(row=0, column=0)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk/Tkinter.py", line 1762, in pack_configure
+ self._options(cnf, kw))
TclError: bad option "-column": must be -after, -anchor, -before, -expand, -fill, -in, -ipadx, -ipady, -padx, -pady, or -side

I thought I did everything correctly. Could it possibly be because of the settings on my main window?

from Tkinter import *

root=Tk()

class Calculator(Frame):
    
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.master.title("CalculatorGUI")
        self.display = StringVar()
        self.ent=Entry(self, textvariable=self.display)
        self.ent.pack(side=TOP, expand=YES)

        onebutton=Button(self, text="1", command=self.one)
        onebutton.pack(row=0, column=0)
        twobutton=Button(self, text="2", command=self.two)
        twobutton.pack()
        thrbutton=Button(self, text="3", command=self.thr)
        thrbutton.pack()
        furbutton=Button(self, text="4", command=self.fur)
        furbutton.pack()
        fivbutton=Button(self, text="5", command=self.fiv)
        fivbutton.pack()
        sixbutton=Button(self, text="6", command=self.six)
        sixbutton.pack()
        svnbutton=Button(self, text="7", command=self.svn)
        svnbutton.pack()
        eigbutton=Button(self, text="8", command=self.eig)
        eigbutton.pack()
        ninbutton=Button(self, text="9", command=self.nin)
        ninbutton.pack()
        zerbutton=Button(self, text="0", command=self.zer)
        zerbutton.pack()
        plsbutton=Button(self, text="+", command=self.pls)
        plsbutton.pack()
        mnsbutton=Button(self, text="-", command=self.mns)
        mnsbutton.pack()
        mulbutton=Button(self, text="*", command=self.mul)
        mulbutton.pack()
        divbutton=Button(self, text="/", command=self.div)
        divbutton.pack()
        dotbutton=Button(self, text=".", command=self.dot)
        dotbutton.pack()
        eqlbutton=Button(self, text="=", command=self.eql)
        eqlbutton.pack()
    def one(self):
        self.ent.insert(END, "1")
    def two(self):
        self.ent.insert(END, "2")
    def thr(self):
        self.ent.insert(END, "3")
    def fur(self):
        self.ent.insert(END, "4")
    def fiv(self):
        self.ent.insert(END, "5")
    def six(self):
        self.ent.insert(END, "6")
    def svn(self):
        self.ent.insert(END, "7")
    def eig(self):
        self.ent.insert(END, "8")
    def nin(self):
        self.ent.insert(END, "9")
    def zer(self):
        self.ent.insert(END, "0")
    def pls(self):
        self.ent.insert(END, "+")
    def mns(self):
        self.ent.insert(END, "-")
    def mul(self):
        self.ent.insert(END, "*")
    def div(self):
        self.ent.insert(END, "/")
    def dot(self):
        self.ent.insert(END, ".")
    def eql(self):
        self.display.set(eval(self.display.get()))
        
Calculator().mainloop()

Here's some code that worked for me.

from Tkinter import *

root=Tk()

class Calculator(Frame):
    
    def __init__(self, master=root):
        Frame.__init__(self, master)
        self.grid()
        self.master.title("CalculatorGUI")
        self.f = Frame(master=self)
        self.f.pack()
        self.g = Frame(master=self)
        self.g.pack()

        self.display = StringVar()
        self.ent=Entry(master=self.f, textvariable=self.display)
        self.ent.grid(row=1, column=1)

	btn_list = [
		'7', '8', '9', '*', 'C',
		'4', '5', '6', '/', 'M->',
		'1', '2', '3', '-', '->M',
		'0', '.', '=', '+', 'neg'
		   ]

	r = 2
	c = 0
	for b in btn_list:
	    rel = 'ridge'
	    cmd = lambda x=b: self.click(x)
	    Button(self.g,text=b,width=5,relief=rel,command=cmd).grid(row=r,column=c)
	    print "Added button '",b,"' w/ grid",r,c
	    c += 1
	    if c > 4:
	        c = 0
	        r += 1
	print "Done adding buttons"

    def click(self, x):
      if x == "=":
        self.display.set(eval(self.display.get()))
      elif x == "C":
        self.display.set("")
      else:
       self.ent.insert(END, x)
        
Calculator().mainloop()

Two comments:
1) pack() does not have row and column, that is for grid()

2) do not mix pack() and grid() in the same frame

This question has already been answered. Start a new discussion instead.