Advertisement
GeorgiLukanov87

tkinter calculator

Oct 17th, 2022
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.04 KB | None | 0 0
  1. from tkinter import *
  2.  
  3. root = Tk()
  4. root.title('Calculator')
  5.  
  6. erase = Entry(root, width=30, borderwidth=8, bg='white')
  7. erase.grid(row=0, column=0, columnspan=3, padx=15, pady=15)
  8. root['bg'] = 'gray'
  9. root.resizable(False, False)
  10.  
  11.  
  12. def button_clear():
  13.     erase.delete(0, END)
  14.  
  15.  
  16. def button_click(number):
  17.     current = erase.get()
  18.     erase.delete(0, END)
  19.     erase.insert(0, str(current) + str(number))
  20.  
  21.  
  22. def button_add():
  23.     first_number = erase.get()
  24.     global number
  25.     global operation
  26.     operation = 'addition'
  27.     number = int(first_number)
  28.     erase.delete(0, END)
  29.  
  30.  
  31. def button_equal():
  32.     second_number = erase.get()
  33.     erase.delete(0, END)
  34.  
  35.     if operation == 'addition':
  36.         erase.insert(0, number + int(second_number))
  37.  
  38.     if operation == 'subtraction':
  39.         erase.insert(0, number - int(second_number))
  40.  
  41.     if operation == 'multiplication':
  42.         erase.insert(0, number * int(second_number))
  43.  
  44.     if operation == 'division':
  45.         if second_number == '0':
  46.             erase.insert(0, 'Cannot divide by ZERO!')
  47.         else:
  48.             erase.insert(0, number / int(second_number))
  49.  
  50.  
  51. def button_subtract():
  52.     first_number = erase.get()
  53.     global number
  54.     global operation
  55.     operation = 'subtraction'
  56.     number = int(first_number)
  57.     erase.delete(0, END)
  58.  
  59.  
  60. def button_multiply():
  61.     first_number = erase.get()
  62.     global number
  63.     global operation
  64.     operation = 'multiplication'
  65.     number = int(first_number)
  66.     erase.delete(0, END)
  67.  
  68.  
  69. def button_divide():
  70.     first_number = erase.get()
  71.     global number
  72.     global operation
  73.     operation = 'division'
  74.     number = int(first_number)
  75.     erase.delete(0, END)
  76.  
  77.  
  78. button_1 = Button(root, text='7', padx=45, borderwidth=3, pady=25, command=lambda: button_click(7), bg='turquoise')
  79. button_2 = Button(root, text='8', padx=45, borderwidth=3, pady=25, command=lambda: button_click(8), bg='turquoise')
  80. button_3 = Button(root, text='9', padx=45, borderwidth=3, pady=25, command=lambda: button_click(9), bg='turquoise')
  81. button_4 = Button(root, text='4', padx=45, borderwidth=3, pady=25, command=lambda: button_click(4), bg='turquoise')
  82. button_5 = Button(root, text='5', padx=45, borderwidth=3, pady=25, command=lambda: button_click(5), bg='pink')
  83. button_6 = Button(root, text='6', padx=45, borderwidth=3, pady=25, command=lambda: button_click(6), bg='turquoise')
  84. button_7 = Button(root, text='1', padx=45, borderwidth=3, pady=25, command=lambda: button_click(1), bg='turquoise')
  85. button_8 = Button(root, text='2', padx=45, borderwidth=3, pady=25, command=lambda: button_click(2), bg='turquoise')
  86. button_9 = Button(root, text='3', padx=45, borderwidth=3, pady=25, command=lambda: button_click(3), bg='turquoise')
  87. button_0 = Button(root, text='0', padx=45, borderwidth=3, pady=30, command=lambda: button_click(0), bg='red')
  88.  
  89. button_add = Button(root, text='+', padx=45, borderwidth=3, pady=30, command=button_add, bg='green')
  90. button_equal = Button(root, text='=', padx=88, borderwidth=5, pady=25, command=button_equal, bg='yellow')
  91. button_clear = Button(root, text='CLEAR', padx=79, borderwidth=5, pady=25, command=button_clear, bg='white')
  92. button_subtract = Button(root, text='-', borderwidth=4, padx=45, pady=30, command=button_subtract, bg='green')
  93. button_multiply = Button(root, text='*', borderwidth=4, padx=45, pady=30, command=button_multiply, bg='orange')
  94. button_divide = Button(root, text='/', borderwidth=4, padx=45, pady=30, command=button_divide, bg='orange')
  95.  
  96. button_1.grid(row=3, column=0)
  97. button_2.grid(row=3, column=1)
  98. button_3.grid(row=3, column=2)
  99. button_4.grid(row=2, column=0)
  100. button_5.grid(row=2, column=1)
  101. button_6.grid(row=2, column=2)
  102. button_7.grid(row=1, column=0)
  103. button_8.grid(row=1, column=1)
  104. button_9.grid(row=1, column=2)
  105. button_0.grid(row=4, column=0)
  106.  
  107. button_clear.grid(row=4, column=1, columnspan=2)
  108. button_add.grid(row=5, column=0)
  109. button_equal.grid(row=5, column=1, columnspan=2)
  110. button_subtract.grid(row=6, column=0)
  111. button_multiply.grid(row=6, column=1)
  112. button_divide.grid(row=6, column=2)
  113.  
  114. root.mainloop()
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement