1from tkinter import *
2
3
4def adding():
5 try:
6 text1 = int(Textbox1.get())
7 text2 = int(Textbox2.get())
8 except Exception:
9 Output.delete(0, END)
10 Output.insert(0, 'Error! Enter a number please!')
11 return
12 text_output = str(text1 + text2)
13 Output.delete(0, END)
14 Output.insert(0, text_output)
15
16root = Tk()
17root.title('Adding')
18root.geometry('500x500')
19Textbox1 = Entry(root)
20Textbox1.pack(ipadx=50, ipady=10)
21spacing = Label(root, text='+')
22spacing.pack()
23Textbox2 = Entry(root)
24Textbox2.pack(ipadx=50, ipady=10)
25spacing2 = Label(root)
26spacing2.pack()
27Button1 = Button(root, text='Add The numbers!', command=adding)
28Button1.pack()
29spacing3 = Label(root)
30spacing3.pack()
31Output = Entry(root)
32Output.pack(ipadx=50)
33root.mainloop()
34
1from tkinter import *
2
3class Calculator:
4 def __init__(self,master):
5 self.master = master
6 master.title("My Calculator @ www.pickupbrain.com")
7 master.configure(bg='#C0C0C0')
8
9 #creating screen widget
10 self.screen = Text(master, state='disabled', width=50,height= 3, background="#EAFAF1", foreground="#000000",font=("Arial",15,"bold"))
11
12 #Screen position in window
13 self.screen.grid(row=0,column=0,columnspan=4,padx=2,pady=2)
14 self.screen.configure(state='normal')
15
16 #initialize screen value as empty
17 self.equation=''
18
19 #create buttons
20 b1 = self.createButton(7)
21 b2 = self.createButton(8)
22 b3 = self.createButton(9)
23 b4 = self.createButton(u"\u232B",None)
24 b5 = self.createButton(4)
25 b6 = self.createButton(5)
26 b7 = self.createButton(6)
27 b8 = self.createButton(u"\u00F7")
28 b9 = self.createButton(1)
29 b10 = self.createButton(2)
30 b11 = self.createButton(3)
31 b12 = self.createButton('*')
32 b13 = self.createButton('.')
33 b14 = self.createButton(0)
34 b15 = self.createButton('+')
35 b16 = self.createButton('-')
36 b17 = self.createButton('=', None,35)
37
38 #stored all buttons in list
39 buttons = [b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17]
40
41 #initalize counter
42 count=0
43
44 #arrange buttons with grid manager
45 for row in range(1,5):
46 for column in range(4):
47 buttons[count].grid(row = row, column= column,padx=0,pady=0)
48 count +=1
49
50 #arrange last button '=' at the bottom
51 buttons[16].grid(row=5,column=0,columnspan=4)
52
53 def createButton(self,val,write=True,width=8):
54
55 #this function creates a button and takes one compulsary argument, the value that should be on the button
56 return Button(self.master,text=val,command= lambda:self.click(val,write),width=width,background="#ffffff",foreground ="#1f4bff",font=("times",20,"bold"))
57
58 def click(self,text,write):
59 #this function handles the actions when you
60 #click a button 'write' arguement, if true
61 #than value val should be written on screen,
62 #if none then should not be written on screen
63 if write == None:
64
65 #Evaluates when there is an equation to be evaluated
66 if text == '=' and self.equation:
67 #replace the unicode values of division ./. with python division
68 #symbol / using regex
69 self.equation = re.sub(u"\u00F7", '/', self.equation)
70 print(self.equation)
71 answer = str(eval(self.equation))
72 self.clear_screen()
73 self.insert_screen(answer,newline = True)
74 elif text == u"\u232B":
75 self.clear_screen()
76
77 else:
78 #add text to screen
79 self.insert_screen(text)
80
81 def clear_screen(self):
82 #to clear screen
83 #set equation to empty before deleteing screen
84 self.equation = ''
85 self.screen.configure(state = 'normal')
86 self.screen.delete('1.0', END)
87
88 def insert_screen(self, value, newline = False):
89 self.screen.configure(state ='normal')
90 self.screen.insert(END,value)
91
92 #record every value inserted in screen
93 self.equation += str(value)
94 self.screen.configure(state = 'disabled')
95
96def calci():
97
98 #Function that creates calculator GUI
99 root = Tk()
100 my_gui = Calculator(root)
101 root.mainloop()
102
103# Running Calculator
104calci()
105
1# importing everyting from tkinter
2from tkinter import *
3# expression to access among all the functions
4expression = ""
5# functions
6def input_number(number, equation):
7 # accessing the global expression variable
8 global expression
9 # concatenation of string
10 expression = expression + str(number)
11 equation.set(expression)
12def clear_input_field(equation):
13 global expression
14 expression = ""
15 # setting empty string in the input field
16 equation.set("Enter the expression")
17def evaluate(equation):
18global expression
19# trying to evaluate the expression
20try:
21result = str(eval(expression))
22# showing the result in the input field
23equation.set(result)
24# setting expression to empty string
25expression = ""
26except:
27# some error occured
28# showing it to the user equation.set("Enter a valid expression")
29expression = ""
30# creating the GUI
31def main():
32 # main window window = Tk()
33 # setting the title of GUI window
34 window.title("Calculator")
35 # set the configuration of GUI window
36 window.geometry("325x175")
37 # varible class instantiation
38 equation = StringVar()
39 # input field for the expression
40 input_field = Entry(window, textvariable=equation)
41 input_field.place(height=100)
42 # we are using grid position
43 # for the arrangement of the widgets
44 input_field.grid(columnspan=4, ipadx=100, ipady=5)
45 # settin the placeholder message for users
46 equation.set("Enter the expression")
47 # creating buttons and placing them at respective positions
48 _1 = Button(window, text='1', fg='white', bg='black', bd=0, command=lambda: input_number(1, equation), height=2, width=7)
49 _1.grid(row=2, column=0)
50 _2 = Button(window, text='2', fg='white', bg='black', bd=0, command=lambda: input_number(2, equation), height=2, width=7)
51 _2.grid(row=2, column=1)
52 _3 = Button(window, text='3', fg='white', bg='black', bd=0, command=lambda: input_number(3, equation), height=2, width=7)
53 _3.grid(row=2, column=2)
54 _4 = Button(window, text='4', fg='white', bg='black', bd=0, command=lambda: input_number(4, equation), height=2, width=7)
55 _4.grid(row=3, column=0)
56 _5 = Button(window, text='5', fg='white', bg='black', bd=0, command=lambda: input_number(5, equation), height=2, width=7)
57 _5.grid(row=3, column=1)
58 _6 = Button(window, text='6', fg='white', bg='black', bd=0, command=lambda: input_number(6, equation), height=2, width=7)
59 _6.grid(row=3, column=2)
60 _7 = Button(window, text='7', fg='white', bg='black', bd=0, command=lambda: input_number(7, equation), height=2, width=7)
61 _7.grid(row=4, column=0)
62 _8 = Button(window, text='8', fg='white', bg='black', bd=0, command=lambda: input_number(8, equation), height=2, width=7)
63 _8.grid(row=4, column=1)
64 _9 = Button(window, text='9', fg='white', bg='black', bd=0, command=lambda: input_number(9, equation), height=2, width=7)
65 _9.grid(row=4, column=2)
66 _0 = Button(window, text='0', fg='white', bg='black', bd=0, command=lambda: input_number(0, equation), height=2, width=7)
67 _0.grid(row=5, column=0)
68 plus = Button(window, text='+', fg='white', bg='black', bd=0, command=lambda: input_number('+', equation), height=2, width=7)
69 plus.grid(row=2, column=3)
70 minus = Button(window, text='-', fg='white', bg='black', bd=0, command=lambda: input_number('-', equation), height=2, width=7)
71 minus.grid(row=3, column=3)
72 multiply = Button(window, text='*', fg='white', bg='black', bd=0, command=lambda: input_number('*', equation), height=2, width=7)
73 multiply.grid(row=4, column=3)
74 divide = Button(window, text='/', fg='white', bg='black', bd=0, command=lambda: input_number('/', equation), height=2, width=7)
75 divide.grid(row=5, column=3)
76 equal = Button(window, text='=', fg='white', bg='black', bd=0, command=lambda: evaluate(equation), height=2, width=7)
77 equal.grid(row=5, column=2)
78 clear = Button(window, text='Clear', fg='white', bg='black', bd=0, command=lambda: clear_input_field(equation), height=2, width=7)
79 clear.grid(row=5, column=1)
80 # showing the GUI
81 window.mainloop()
82# start of the program
83if __name__ == '__main__':
84 main()
85