473,395 Members | 1,422 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,395 software developers and data experts.

Calculator Explanation

6



Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. def frame(root, side): 
  4.     w = Frame(root)
  5.     w.pack(side=side, expand=YES, fill=BOTH)
  6.     return w
  7.  
  8. def button(root, side, text, command=None): 
  9.     w = Button(root, text=text, command=command) 
  10.     w.pack(side=side, expand=YES, fill=BOTH)
  11.     return w
  12.  
  13. class Calculator(Frame):
  14.     def __init__(self):
  15.         Frame.__init__(self)
  16.         self.option_add('*Font', 'Verdana 12 bold')
  17.         self.pack(expand=YES, fill=BOTH)
  18.         self.master.title('Simple Calculator')
  19.         self.master.iconname("calc1")
  20.  
  21.         display = StringVar()
  22.         Entry(self, relief=SUNKEN, textvariable=display).pack(side=TOP, expand=YES, fill=BOTH)
  23.  
  24.         for key in ("123", "456", "789", "-0."):
  25.             keyF = frame(self, TOP)
  26.             for char in key:
  27.                 button(keyF, LEFT, char,
  28.                        lambda w=display, c=char: w.set(w.get() + c))
  29.  
  30.         opsF = frame(self, TOP)
  31.         for char in "+-*/=":
  32.             if char == '=':
  33.                 btn = button(opsF, LEFT, char)
  34.                 btn.bind('<ButtonRelease-1>',
  35.                          lambda e, s=self, w=display: s.calc(w), '+')
  36.             else:
  37.                 btn = button(opsF, LEFT, char,
  38.                    lambda w=display, s=' %s '%char: w.set(w.get()+s))
  39.  
  40.         clearF = frame(self, BOTTOM)
  41.         button(clearF, LEFT, 'Clr', lambda w=display: w.set(''))
  42.  
  43.     def calc(self, display):
  44.         try:
  45.             display.set('eval(display.get())')
  46.         except:
  47.             display.set("ERROR")
  48.  
  49. if __name__ == '__main__':
  50.     Calculator().mainloop()
Who can explain the code? How are the buttons created?
Thanks
Mar 2 '10 #1

✓ answered by bvdet

That's why I like to import the module instead of importing everything.
Expand|Select|Wrap|Line Numbers
  1. import Tkinter
  2. # or import Tkinter as Tk
You may be able to see that the buttons are created with the call
Expand|Select|Wrap|Line Numbers
  1. w = Tkinter.Button(root, text=text, command=command)
Then the pack geometry manager is called as a method of the widget to display the button.
Expand|Select|Wrap|Line Numbers
  1. w.pack(side=side, expand=YES, fill=BOTH)
display is the variable shown in the Tkinter.Entry widget. The current value of the variable can be obtained with the get() method of Tkinter variables. The "=" button is bound to the calc() function which evaluates the current value of display and sets the variable to the result using the set() method of Tlinter variables.

3 3000
bvdet
2,851 Expert Mod 2GB
That's why I like to import the module instead of importing everything.
Expand|Select|Wrap|Line Numbers
  1. import Tkinter
  2. # or import Tkinter as Tk
You may be able to see that the buttons are created with the call
Expand|Select|Wrap|Line Numbers
  1. w = Tkinter.Button(root, text=text, command=command)
Then the pack geometry manager is called as a method of the widget to display the button.
Expand|Select|Wrap|Line Numbers
  1. w.pack(side=side, expand=YES, fill=BOTH)
display is the variable shown in the Tkinter.Entry widget. The current value of the variable can be obtained with the get() method of Tkinter variables. The "=" button is bound to the calc() function which evaluates the current value of display and sets the variable to the result using the set() method of Tlinter variables.
Mar 2 '10 #2
bvdet
2,851 Expert Mod 2GB
To display a calculated result, method calc() should look like this:
Expand|Select|Wrap|Line Numbers
  1.     def calc(self, display):
  2.         try:
  3.             display.set(str(eval(display.get())))
  4.         except:
  5.             display.set("ERROR")
Mar 2 '10 #3
Fadili
6
Thanks bvdet, very clearly explained
Mar 2 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jennifer | last post by:
I'm running SQL query to caluclate projected food costs. The calculation is this: (ReportedFoodSales / PlanFoodSales) * FullPlanFoodSales Seems simple enough to me. Using the following...
6
by: Rafael | last post by:
Hi Everyone, I need some help with my calculator program. I need my program to do 2 arguments and a 3rd, but the 3rd with different operators. Any help would be great. Here is my code.... ...
3
by: Paul | last post by:
I want to make a simple calculator program but dont know where to get started. This is not GUI but a simple terminal program. It would get input like this Enter number: 5 + 10
3
by: Art | last post by:
Hi, In part of my application the user may need to do a simple arithmetic calculation in order to get the value to put in a text box. I was thinking that it would be good if I could display the...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
14
by: arnuld | last post by:
Stroustrup starts chapter 6 with a programme for desk-calculator: here is a grammer for the langugae accepted by the calcualtor: program: END // END is end-of-input...
19
by: TexasNewbie | last post by:
This was originally just a calculator without a decimal point. After I added the decimal, it now tells me invalid second number. //GUI Calculator Program import javax.swing.*; import...
3
by: karpakavallli | last post by:
i need the code and the explanation to the code calculator
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
3
by: mandy335 | last post by:
public class Calculator { private long input = 0; // current input private long result = 0; // last input/result private String lastOperator = ""; // keeps track of...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.