Advertisement
GalinaKG

Calculator

Jul 20th, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. from tkinter import *
  2.  
  3. root = Tk()
  4. root.title('Calculator')
  5.  
  6. e = Entry(root, width=35, borderwidth=5, bg='alice blue')
  7. e.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
  8. root['bg'] = 'dodger blue'
  9.  
  10.  
  11. def button_click(number):
  12.     current = e.get()
  13.     e.delete(0, END)
  14.     e.insert(0, str(current) + str(number))
  15.  
  16.  
  17. def button_clear_func():
  18.     e.delete(0, END)
  19.  
  20.  
  21. def button_add_func():
  22.     first_number = e.get()
  23.     global f_num
  24.     global math
  25.     math = 'addition'
  26.     f_num = int(first_number)
  27.     e.delete(0, END)
  28.  
  29.  
  30. def button_equal_func():
  31.     second_number = e.get()
  32.     e.delete(0, END)
  33.  
  34.     if math == 'addition':
  35.         e.insert(0, f_num + int(second_number))
  36.  
  37.     if math == 'subtraction':
  38.         e.insert(0, f_num - int(second_number))
  39.  
  40.     if math == 'multiplication':
  41.         e.insert(0, f_num * int(second_number))
  42.  
  43.     if math == 'division':
  44.         e.insert(0, f_num / int(second_number))
  45.  
  46.  
  47. def button_subtract_func():
  48.     first_number = e.get()
  49.     global f_num
  50.     global math
  51.     math = 'subtraction'
  52.     f_num = int(first_number)
  53.     e.delete(0, END)
  54.  
  55.  
  56. def button_multiply_func():
  57.     first_number = e.get()
  58.     global f_num
  59.     global math
  60.     math = 'multiplication'
  61.     f_num = int(first_number)
  62.     e.delete(0, END)
  63.  
  64.  
  65. def button_divide_func():
  66.     first_number = e.get()
  67.     global f_num
  68.     global math
  69.     math = 'division'
  70.     f_num = int(first_number)
  71.     e.delete(0, END)
  72.  
  73.  
  74. button_1 = Button(root, text='1', padx=40, pady=20, command=lambda: button_click(1), bg='HotPink1')
  75. button_2 = Button(root, text='2', padx=40, pady=20, command=lambda: button_click(2), bg='HotPink1')
  76. button_3 = Button(root, text='3', padx=40, pady=20, command=lambda: button_click(3), bg='HotPink1')
  77. button_4 = Button(root, text='4', padx=40, pady=20, command=lambda: button_click(4), bg='HotPink1')
  78. button_5 = Button(root, text='5', padx=40, pady=20, command=lambda: button_click(5), bg='HotPink1')
  79. button_6 = Button(root, text='6', padx=40, pady=20, command=lambda: button_click(6), bg='HotPink1')
  80. button_7 = Button(root, text='7', padx=40, pady=20, command=lambda: button_click(7), bg='HotPink1')
  81. button_8 = Button(root, text='8', padx=40, pady=20, command=lambda: button_click(8), bg='HotPink1')
  82. button_9 = Button(root, text='9', padx=39, pady=20, command=lambda: button_click(9), bg='HotPink1')
  83. button_0 = Button(root, text='0', padx=40, pady=20, command=lambda: button_click(0), bg='HotPink1')
  84. button_add = Button(root, text='+', padx=40, pady=20, command=button_add_func, bg='HotPink1')
  85. button_equal = Button(root, text='=', padx=88, pady=20, command=button_equal_func, bg='HotPink1')
  86. button_clear = Button(root, text='Clear', padx=79, pady=20, command=button_clear_func, bg='HotPink1')
  87.  
  88. button_subtract = Button(root, text='-', padx=41, pady=20, command=button_subtract_func, bg='HotPink1')
  89. button_multiply = Button(root, text='*', padx=40, pady=20, command=button_multiply_func, bg='HotPink1')
  90. button_divide = Button(root, text='/', padx=41, pady=20, command=button_divide_func, bg='HotPink1')
  91.  
  92.  
  93. button_1.grid(row=3, column=0)
  94. button_2.grid(row=3, column=1)
  95. button_3.grid(row=3, column=2)
  96.  
  97. button_4.grid(row=2, column=0)
  98. button_5.grid(row=2, column=1)
  99. button_6.grid(row=2, column=2)
  100.  
  101. button_7.grid(row=1, column=0)
  102. button_8.grid(row=1, column=1)
  103. button_9.grid(row=1, column=2)
  104.  
  105. button_0.grid(row=4, column=0)
  106. button_clear.grid(row=4, column=1, columnspan=2)
  107. button_add.grid(row=5, column=0)
  108. button_equal.grid(row=5, column=1, columnspan=2)
  109.  
  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