Help with Calculater (first program)

A

aToaster

Hey guys, I'm just getting the hang of Python and Tkinter and I could
use some help. I wrote most of the gui for my calculator program,
well I haven't gotten around to putting in the
plus/minus/divide/mulitply yet but I'll save that for later. Right
now I want the buttons to display the number in the entry field. How
can I pass the values into the entry field as they are being clicked?
Oh and here's my biggest question. Right now I have it set so that if
you press 3 and 4 it gives you 7, but what I want it to do is if you
press 3 and then 4 it gives you 34. How would I be able to do that?
I appreciate the help! Thanks.

,David Peredo

#------------------ CalcApp.py ---------------------------
from Tkinter import *

class CalcApp:
def __init__(self, parent):
self.myParent = parent
self.myContainer1 = Frame(parent)
self.myContainer1.pack(side=TOP)

self.myContainer2 = Frame(parent)
self.myContainer2.pack()

self.myContainer3 = Frame(parent)
self.myContainer3.pack()

self.myContainer4 = Frame(parent)
self.myContainer4.pack()

self.myContainer5 = Frame(parent)
self.myContainer5.pack()

self.myContainer6 = Frame(parent)
self.myContainer6.pack(side=BOTTOM)

#calcTotal
self.calcTotal = 0

# Button 7
button_name = "7"
self.button3 = Button(self.myContainer1,
command = lambda
arg1=7, arg2=button_name :
self.buttonHandler(arg1, arg2)
)
self.button3.configure(text=button_name)
self.button3.pack(side=LEFT)

# Button 8
button_name = "8"
self.button3 = Button(self.myContainer1,
command = lambda
arg1=8, arg2=button_name :
self.buttonHandler(arg1, arg2)
)
self.button3.configure(text=button_name)
self.button3.pack(side=LEFT)

# Button 9
button_name = "9"
self.button3 = Button(self.myContainer1,
command = lambda
arg1=9, arg2=button_name :
self.buttonHandler(arg1, arg2)
)
self.button3.configure(text=button_name)
self.button3.pack(side=LEFT)

# Button 4
button_name = "4"
self.button3 = Button(self.myContainer2,
command = lambda
arg1=4, arg2=button_name :
self.buttonHandler(arg1, arg2)
)
self.button3.configure(text=button_name)
self.button3.pack(side=LEFT)

# Button 5
button_name = "5"
self.button3 = Button(self.myContainer2,
command = lambda
arg1=5, arg2=button_name :
self.buttonHandler(arg1, arg2)
)
self.button3.configure(text=button_name)
self.button3.pack(side=LEFT)

# Button 6
button_name = "6"
self.button3 = Button(self.myContainer2,
command = lambda
arg1=6, arg2=button_name :
self.buttonHandler(arg1, arg2)
)
self.button3.configure(text=button_name)
self.button3.pack(side=LEFT)

# Button 1
button_name = "1"
self.button1 = Button(self.myContainer3,
command = lambda
arg1=1, arg2=button_name :
self.buttonHandler(arg1, arg2)
)

self.button1.configure(text=button_name)
self.button1.pack(side=LEFT)

# Button 2
button_name = "2"
self.button2 = Button(self.myContainer3,
command = lambda
arg1=2, arg2=button_name :
self.buttonHandler(arg1, arg2)
)
self.button2.configure(text=button_name)
self.button2.pack(side=LEFT)

# Button 3
button_name = "3"
self.button3 = Button(self.myContainer3,
command = lambda
arg1=3, arg2=button_name :
self.buttonHandler(arg1, arg2)
)
self.button3.configure(text=button_name)
self.button3.pack(side=LEFT)

# Button 0
button_name = "0"
self.button3 = Button(self.myContainer4,
command = lambda
arg1=0, arg2=button_name :
self.buttonHandler(arg1, arg2)
)
self.button3.configure(text=button_name, width=3)
self.button3.pack(side=LEFT)

# Button .
button_name = "."
self.buttondot = Button(self.myContainer4)
self.buttondot.configure(text=button_name, width=1)
self.buttondot.pack(side=RIGHT)

# Button Clear
button_name = "Clear"
self.buttonClear = Button(self.myContainer6,
command = self.buttonClearClick)
self.buttonClear.configure(text=button_name)
self.buttonClear.pack(side=BOTTOM)

# Entry
self.entryBox = Entry(self.myContainer5)
self.entryBox.pack()


def buttonHandler(self, argument1, argument2):
print "You have clicked :", argument2
self.calcTotal = self.calcTotal + argument1
print self.calcTotal
self.entryBox.configure(text=self.calcTotal)

def buttonHandler_a(self, argument1, argument2):
print "You have clicked :", argument2
self.calcTotal = self.calcTotal +argument1
print self.calcTotal

def buttonClearClick(self):
print "You have cleared the total."
self.calcTotal = 0

print "\n"*100
print "Starting program Calc."
root = Tk()
myapp = CalcApp(root)
print "Starting even loop."
root.mainloop()
print "Finished executing event loop."

#End of Program
#------------------ CalcApp.py ---------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top