Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import Tk, Entry, Button, Label, messagebox
- def calculate():
- try:
- a = float(entry_a.get())
- b = float(entry_b.get())
- operation = entry_operation.get()
- if operation == '+':
- result = a + b
- elif operation == '-':
- result = a - b
- elif operation == '*':
- result = a * b
- elif operation == '/':
- result = a / b
- entry_result.delete(0, 'end')
- entry_result.insert('end', str(result))
- except ValueError:
- messagebox.showerror('Error', 'Invalid input')
- def exit_program():
- if messagebox.askyesno('Exit', 'Are you sure you want to exit?'):
- root.destroy()
- root = Tk()
- label_a = Label(root, text='a:')
- label_a.grid(row=0, column=0)
- entry_a = Entry(root)
- entry_a.grid(row=0, column=1)
- label_operation = Label(root, text='Operation:')
- label_operation.grid(row=1, column=0)
- entry_operation = Entry(root)
- entry_operation.grid(row=1, column=1)
- label_b = Label(root, text='b:')
- label_b.grid(row=2, column=0)
- entry_b = Entry(root)
- entry_b.grid(row=2, column=1)
- button_calculate = Button(root, text='Calculate', command=calculate)
- button_calculate.grid(row=3, column=0)
- label_result = Label(root, text='Result:')
- label_result.grid(row=4, column=0)
- entry_result = Entry(root)
- entry_result.grid(row=4, column=1)
- button_exit = Button(root, text='Exit', command=exit_program)
- button_exit.grid(row=5, column=0)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement