├── LICENSE ├── README.md └── main.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Sushant Patrikar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-tkinter-calculator 2 | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) 3 | ![stars](https://img.shields.io/github/stars/sushantPatrikar/Python-tkinter-calculator.svg) 4 | ![Python 3.6](https://img.shields.io/badge/python-3.6-blue.svg) 5 | 6 | This is my first python project. From the start I had an interest in GUIs, so as soon as I learnt the basics of tkinter I started working on this project. 7 | 8 | ### Making the layout of the calculator 9 | 10 | In this calculator, a frame is used for making the screen, and two labels, one for putting digit buttons and the other for function keys are used. I used sub-labels inside these labels to put buttons on it 11 | 12 | ### Making the screen 13 | 14 | Entry box is used for making the screen of the calculator 15 | 16 | ### Giving life to the buttons 17 | 18 | Till this stage my claculator was completed, virtually, it was not functionable. I tried binding buttons to the functions but the results were printed on the console and not on the calculator's screen, so I tried using lambda in the buttons and it worked! 19 | 20 | ### Giving Midas touch to the calculator 21 | 22 | I put this code in the __init__ function of the class calculator and gave it nice color combinatons to make it look more beautiful,and here it is! 23 | 24 | ![screenshot 2](https://user-images.githubusercontent.com/40419750/41705277-29414842-7556-11e8-8220-4b063cdadf11.png) 25 | 26 | P.S:- When you see my code, you'll find that I've commented out some part of my code, that's because I was trying to add one more button in the calculator 'delete' which would delete just the first digit from the right but when I'm trying it, it is throwing out some errors. 27 | So I will update it as soon as I find the solution. 28 | 29 | Feedback is welcomed! 30 | 31 | Happy Coding :) 32 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | root = Tk() 4 | #button_9 = Button(label_key,text='9',height=3,width=5,font=('Helvetica','12')) 5 | #button_9.grid(row=0,column=0) 6 | class Calculator: 7 | def click_button(self,numbers): 8 | global operator 9 | global var 10 | self.operator = self.operator + str(numbers) 11 | self.var.set(self.operator) 12 | 13 | def clear(self): 14 | self.entry.delete(0,END) 15 | self.operator ="" 16 | 17 | ''' def delete(self): 18 | self.operator = str(self.entry.delete(len(self.entry.get())-1)) 19 | ''' 20 | 21 | 22 | def evaluate(self): 23 | self.answer =eval(self.entry.get()) 24 | self.var.set(self.answer) 25 | self.operator = str(self.answer) 26 | 27 | def __init__(self,master): 28 | 29 | self.operator = "" 30 | self.var = StringVar() 31 | frame_s = Frame(master, height=400, width=45 ) 32 | frame_s.pack(side=TOP, fill=BOTH, expand=True) 33 | self.entry = Entry(frame_s,textvariable=self.var,bg='grey',width=45,bd=20,insertwidth=4,justify='right',font=('arial',10,'bold')) 34 | self.entry.pack() 35 | self.t = Text(self.entry,height=40) 36 | 37 | 38 | 39 | label_key = Label(root, height=15, width=30,bd=10,bg='gray50') 40 | label_key.pack(side=LEFT, fill=BOTH, expand=True) 41 | 42 | label_fkey = Label(root, height=15, width=15, bg='gray25') 43 | label_fkey.pack(fill=BOTH, expand=True) 44 | 45 | label_7 = Label(label_key, bg='black') 46 | label_7.grid(row=0, column=0) 47 | button_7 = Button(label_7, text='7', font=('Helvetica', '16'),command= lambda : self.click_button(7),bg='black',fg='cyan') 48 | button_7.pack() 49 | 50 | label_8 = Label(label_key, bg='black') 51 | label_8.grid(row=0, column=1, padx=20) 52 | button_8 = Button(label_8, text='8', font=('Helvetica', '16'),command= lambda: self.click_button(8),bg='black',fg='cyan') 53 | button_8.pack() 54 | 55 | label_9 = Label(label_key, bg='black') 56 | label_9.grid(row=0, column=2, padx=10) 57 | button_9 = Button(label_9, text='9', font=('Helvetica', '16'),command= lambda: self.click_button(9),bg='black',fg='cyan') 58 | button_9.pack() 59 | 60 | label_4 = Label(label_key, bg='black') 61 | label_4.grid(row=1, column=0, padx=10, pady=10) 62 | button_4 = Button(label_4, text='4', font=('Helvetica', '16'),command= lambda: self.click_button(4),bg='black',fg='cyan') 63 | button_4.pack() 64 | 65 | label_5 = Label(label_key, bg='black') 66 | label_5.grid(row=1, column=1, padx=10, pady=10) 67 | button_5 = Button(label_5, text='5', font=('Helvetica', '16'),command= lambda: self.click_button(5),bg='black',fg='cyan') 68 | button_5.pack() 69 | 70 | label_6 = Label(label_key, bg='black') 71 | label_6.grid(row=1, column=2, padx=10, pady=10) 72 | button_6 = Button(label_6, text='6', font=('Helvetica', '16'),command= lambda: self.click_button(6),bg='black',fg='cyan') 73 | button_6.pack() 74 | 75 | label_1 = Label(label_key, bg='black') 76 | label_1.grid(row=2, column=0, padx=10) 77 | button_1 = Button(label_1, text='1', font=('Helvetica', '16'),command= lambda: self.click_button(1),bg='black',fg='cyan') 78 | button_1.pack() 79 | 80 | label_2 = Label(label_key, bg='black') 81 | label_2.grid(row=2, column=1, padx=10) 82 | button_2 = Button(label_2, text='2', font=('Helvetica', '16'),command= lambda: self.click_button(2),bg='black',fg='cyan') 83 | button_2.pack() 84 | 85 | label_3 = Label(label_key, bg='black') 86 | label_3.grid(row=2, column=2, padx=10) 87 | button_3 = Button(label_3, text='3', font=('Helvetica', '16'),command= lambda: self.click_button(3),bg='black',fg='cyan') 88 | button_3.pack() 89 | 90 | label_0 = Label(label_key, bg='black') 91 | label_0.grid(row=3, column=0, padx=10, pady=10) 92 | button_0 = Button(label_0, text='0', font=('Helvetica', '16'),command= lambda: self.click_button(0),bg='black',fg='cyan') 93 | button_0.pack() 94 | 95 | label_deci = Label(label_key, bg='black') 96 | label_deci.grid(row=3, column=1, padx=10, pady=10) 97 | button_deci = Button(label_deci, text='.', font=('Helvetica', '16'),command= lambda: self.click_button('.'),bg='black',fg='cyan') 98 | button_deci.pack() 99 | 100 | label_equal = Label(label_key, bg='black') 101 | label_equal.grid(row=3, column=2, padx=10, pady=10) 102 | button_equal = Button(label_equal, text='=', font=('Helvetica', '16'),command= self.evaluate,bg='black',fg='cyan') 103 | button_equal.pack() 104 | 105 | label_C = Label(label_fkey, bg='black') 106 | label_C.grid(row=0, column=0,columnspan=2) 107 | button_C = Button(label_C, text='C', font=('Helvetica', '16'), height=1, width=10,command= self.clear,bg='black',fg='cyan') 108 | button_C.pack(side=LEFT) 109 | 110 | '''label_del = Label(label_fkey, bg ='black') 111 | label_del.grid(row=0,column=1,sticky=E) 112 | button_del = Button(label_del, text='del', font=('Helvetica', '16'),bd=3, height=1, width=3,command= self.delete) 113 | button_del.pack()''' 114 | 115 | label_sub = Label(label_fkey, bg='black') 116 | label_sub.grid(row=1, column=0, sticky=W, pady=10) 117 | button_sub = Button(label_sub, text='-', font=('Helvetica', '16'), height=1, width=3,command= lambda: self.click_button('-'),bg='black',fg='cyan') 118 | button_sub.pack(side=LEFT) 119 | 120 | label_mul = Label(label_fkey, bg='black') 121 | label_mul.grid(row=1, column=1, sticky=E) 122 | button_mul = Button(label_mul, text='x', font=('Helvetica', '16'), height=1, width=3,command= lambda: self.click_button('*'),bg='black',fg='cyan') 123 | button_mul.pack() 124 | 125 | label_div = Label(label_fkey, bg='black') 126 | label_div.grid(row=2, column=0, sticky=W) 127 | button_div = Button(label_div, text='/', font=('Helvetica', '16'), height=1, width=3,command= lambda: self.click_button('/'),bg='black',fg='cyan') 128 | button_div.pack() 129 | 130 | label_add = Label(label_fkey, bg='black') 131 | label_add.grid(row=2, column=1, sticky=E) 132 | button_add = Button(label_add, text='+', font=('Helvetica', '16'), height=1, width=3,command= lambda: self.click_button('+'),bg='black',fg='cyan') 133 | button_add.pack() 134 | 135 | label_lbrace = Label(label_fkey, bg='black') 136 | label_lbrace.grid(row=3,column=0,sticky=W,pady=10) 137 | button_lbrace = Button(label_lbrace,text='(', font=('Helvetica', '16'), height=1, width=3,command= lambda: self.click_button('('),bg='black',fg='cyan') 138 | button_lbrace.pack() 139 | 140 | label_rbrace = Label(label_fkey, bg='black') 141 | label_rbrace.grid(row=3, column=1, sticky=E, pady=10) 142 | button_rbrace = Button(label_rbrace, text=')', font=('Helvetica', '16'), height=1, width=3, 143 | command=lambda: self.click_button(')'),bg='black',fg='cyan') 144 | button_rbrace.pack() 145 | 146 | c = Calculator(root) 147 | root.title("Sushant\'s Calculator") 148 | root.mainloop() --------------------------------------------------------------------------------