Advertisement
Sweet1123

Untitled

Sep 26th, 2023
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. from tkinter import Tk, Entry, Button, Label, messagebox
  2.  
  3. def calculate():
  4.     try:
  5.         a = float(entry_a.get())
  6.         b = float(entry_b.get())
  7.         operation = entry_operation.get()
  8.        
  9.         if operation == '+':
  10.             result = a + b
  11.         elif operation == '-':
  12.             result = a - b
  13.         elif operation == '*':
  14.             result = a * b
  15.         elif operation == '/':
  16.             result = a / b
  17.        
  18.         entry_result.delete(0, 'end')
  19.         entry_result.insert('end', str(result))
  20.     except ValueError:
  21.         messagebox.showerror('Error', 'Invalid input')
  22.  
  23. def exit_program():
  24.     if messagebox.askyesno('Exit', 'Are you sure you want to exit?'):
  25.         root.destroy()
  26.  
  27. root = Tk()
  28.  
  29. label_a = Label(root, text='a:')
  30. label_a.grid(row=0, column=0)
  31. entry_a = Entry(root)
  32. entry_a.grid(row=0, column=1)
  33.  
  34. label_operation = Label(root, text='Operation:')
  35. label_operation.grid(row=1, column=0)
  36. entry_operation = Entry(root)
  37. entry_operation.grid(row=1, column=1)
  38.  
  39. label_b = Label(root, text='b:')
  40. label_b.grid(row=2, column=0)
  41. entry_b = Entry(root)
  42. entry_b.grid(row=2, column=1)
  43.  
  44. button_calculate = Button(root, text='Calculate', command=calculate)
  45. button_calculate.grid(row=3, column=0)
  46.  
  47. label_result = Label(root, text='Result:')
  48. label_result.grid(row=4, column=0)
  49. entry_result = Entry(root)
  50. entry_result.grid(row=4, column=1)
  51.  
  52. button_exit = Button(root, text='Exit', command=exit_program)
  53. button_exit.grid(row=5, column=0)
  54.  
  55. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement