hi, how are you today
"C" is normally an alphabet we see on calculator to delete the last digit, so in our calculator we used the same. So whenever we receive "C", we are copying the data till one less than last digit in txt variable, then deleting all data in entry widget and now pasting the copied data in txt to entry widget.
So above 52 lines of code is enough to design a simple calculator.
New to python, this project is for you only.
Before starting this I believe you have a little bit knowledge of tkinter , a GUI for python
So as we all aware, python has a huge set of inbuilt modules that make it easier to learn and to do different projects in short time.
In this project we are going to use
- tkinter module to design GUI for calculator
- math module to calculate the expression
It means we have to import Tkinter/tkinter and math.
Tkinter and tkinter are same but depends on the Pycharm version you are using.
If it below 3 then use Tkinter else use tkinter, but here we are going for a software, so we are not aware what version client is using so we have to design it in such a way that it can work properly on any pycharm platform.
So we go with exception handling.
So we go with exception handling.
So let's start the coding section and remember my code is for the above image of calculator,
you can have your own design.
Lets discuss first the function or operations we need.
Lets discuss first the function or operations we need.
Roles Of Functions
delete the wrong digit function
Our basic requirement is to delete a wrong entered digit so for that we are going to design a function in which SLICE Operator will be helpful to us.
So our motive is to delete the digit at the last position in input area, so an easy way is to copy the data till one less than last digit and make the entry widget empty and paste the copied data. in the same input area.
below is the function code to delete wrong entry.
21: # function to delete wrong digit
22: def action(value):
23: if value == 'c':
24: txt = text.get()[:-1]
25: text.delete(0, END)
26: text.insert(0, txt)
27: else:
28: text.insert(END,value)
"C" is normally an alphabet we see on calculator to delete the last digit, so in our calculator we used the same. So whenever we receive "C", we are copying the data till one less than last digit in txt variable, then deleting all data in entry widget and now pasting the copied data in txt to entry widget.
Evaluating expression function
Now what ever data we enter to calculate, we need to evaluate it depends on the rule "BODMAS".
So instead of designing our own , we can use a predefined function eval(expression) from maths module.
the expression u enter in entry widget is a string, so this function will accept that string in parameter and will do operation on that string and return the result
29: # function to evaluate expression with pre-defined function eval() present in math module
30: def result():
31: res=eval(text.get())
32: text.delete(0, END)
33: text.insert(0, res)
So here we are collecting the evaluated result in res and again deleting the data from the entry field and pasting the evaluated data from res.
Full Working Code with GUI
1: try:
2: from Tkinter import *
3: except:
4: from tkinter import *
5: # tkinter will work with pycharm 2.x version and tkinter is for pycharm 3.x version
6: import math
7: class Calculator:
8: def __init__(self):
9: self.mw=Tk()
10: self.mw.overrideredirect(1)
11: self.mw.geometry('220x300+600+200')
12: self.mw.config( highlightcolor='black', highlightthickness=1)
13: text=Label(self.mw,text=" Calculator", font='arial 10', bg='#06213f', fg='white').pack(fill='x')
14: text1=Label(self.mw,text="SALAH Tutorials\n...inspiring self-study : only key 2 success",fg='red', font='arial 8')
15: text1.place(x=4,y=260)
16: favicon=PhotoImage(file='favicon1.png')
17: fav=Button(self.mw, image=favicon, command=self.mw.destroy).place(x=0,y=0)
18: Calculator.layout(self)
19: self.mw.mainloop()
20: def layout(self):
21: # function to delete wrong digit
22: def action(value):
23: if value == 'c':
24: txt = text.get()[:-1]
25: text.delete(0, END)
26: text.insert(0, txt)
27: else:
28: text.insert(END,value)
29: # function to evaluate expression with pre-defined function eval() present in math module
30: def result():
31: res=eval(text.get())
32: text.delete(0, END)
33: text.insert(0, res)
34: text=Entry(self.mw, width=32, bd=4)
35: text.place(x=10,y=30)
36: one = Button(self.mw, text= '1',width=5, height=2, bd=4, command=lambda :action('1')).place(x=10,y=60)
37: two = Button(self.mw, text= '2',width=5, height=2, bd=4, command=lambda :action('2')).place(x=60,y=60)
38: three = Button(self.mw, text= '3',width=5, height=2, bd=4, command=lambda :action('3')).place(x=110,y=60)
39: add = Button(self.mw, text= '+',width=5, height=2, bd=4, command=lambda :action('+')).place(x=160,y=60)
40: four = Button(self.mw, text= '4',width=5, height=2, bd=4, command=lambda :action('4')).place(x=10,y=105)
41: five = Button(self.mw, text= '5',width=5, height=2, bd=4, command=lambda :action('5')).place(x=60,y=105)
42: six = Button(self.mw, text= '6',width=5, height=2, bd=4, command=lambda :action('6')).place(x=110,y=105)
43: sub = Button(self.mw, text= '-',width=5, height=2, bd=4, command=lambda :action('-')).place(x=160,y=105)
44: seven = Button(self.mw, text= '7',width=5, height=2, bd=4, command=lambda :action('7')).place(x=10,y=150)
45: eight = Button(self.mw, text= '8',width=5, height=2, bd=4, command=lambda :action('8')).place(x=60,y=150)
46: nine = Button(self.mw, text= '9',width=5, height=2, bd=4, command=lambda :action('9')).place(x=110,y=150)
47: back = Button(self.mw, text= 'c',width=5, height=2, bd=4, command=lambda :action('c')).place(x=160,y=150)
48: mul=Button(self.mw, text= '*',width=5, height=2, bd=4, command=lambda :action('*')).place(x=10,y=195)
49: zero = Button(self.mw, text= '0',width=5, height=2, bd=4, command=lambda :action('0')).place(x=60,y=195)
50: div=Button(self.mw, text= '/',width=5, height=2, bd=4, command=lambda :action('/')).place(x=110,y=195)
51: res = Button(self.mw, text= '=',width=5, height=2, bd=4, command=lambda :result()).place(x=160,y=195)
52: c=Calculator()
So above 52 lines of code is enough to design a simple calculator.
Video
Hope you all like this post and I wish this proves useful to you.
Keep sharing & give your views about post in comments.
Comments are always welcomed for better improvement.
Keep sharing & give your views about post in comments.
Comments are always welcomed for better improvement.
Thanks :)
Comments
Post a Comment