473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help with Calculater (first program)

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 ---------------------------
Jul 18 '05 #1
3 1539
da**@inthegarage.net (aToaster) writes:
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


Hey, that was my first tkinter experiment too.

http://www.nightsong.com/phr/python/calc.py
Jul 18 '05 #2
Paul Rubin <http://ph****@NOSPAM.invalid> wrote in message news:<7x************@ruckus.brouhaha.com>...
da**@inthegarage.net (aToaster) writes:
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


Hey, that was my first tkinter experiment too.

http://www.nightsong.com/phr/python/calc.py


Mine too!
I think the answer to your biggest question is to mutiply 3 by ten
before 4 is added to it, that would be a simple change.

Ciao
Tony
Jul 18 '05 #3
Paul Rubin <http://ph****@NOSPAM.invalid> wrote in message news:<7x************@ruckus.brouhaha.com>...
da**@inthegarage.net (aToaster) writes:
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


Hey, that was my first tkinter experiment too.

http://www.nightsong.com/phr/python/calc.py


Mine too!
I think the answer to your biggest question is to mutiply 3 by ten
before 4 is added to it, that would be a simple change.

Ciao
Tony
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Simon | last post by:
I am a programming student and have recently missed two weeks of school due to a serious injury in a car accident. I am completing all of my assignments (via Labs) but have come across the...
4
by: Josh | last post by:
Howdy i am newb somewhat to programing and i was just for fun trying to compile a program that asks the user for an odd int less than 22 and then returns this ***************** ******* *********...
2
by: dinks | last post by:
Hi, I'm new to C++ and have been assigned a task which i dont completely understand. Any help would be greately appreciated. Here is the problem: The class "linkedListType" use the "assert"...
2
by: atreide_son | last post by:
hello all... yes i'm a newbie and i need help. i have an assignment due for class, but I don't know i'm stuck and can't get out from under myself. here's the focus of the program: Write...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
15
by: Buck Rogers | last post by:
Hi guys! Task 1: Write a program which presents a menu with 5 options. The 5th option quits the program. Each option should execute a system command using system(). Below is my humble...
2
by: leo2100 | last post by:
Hi, I need help with this program. The program is supposed to take a text file and identify the words in it, then it should print them and count how many times a word is repeated. At first main...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
4
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.