Advertisement
horozov86

Calculator

May 29th, 2024 (edited)
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.60 KB | None | 0 0
  1. import tkinter
  2. from tkinter import *
  3.  
  4. root = Tk()
  5. root.title("Calculator")
  6. root.geometry("570x600+100+200")
  7. root.resizable(False, False)
  8. root.configure(bg="#17161b")
  9.  
  10. equation = ""
  11.  
  12. def show(value):
  13.    
  14.     global equation
  15.     equation+= value
  16.     label_result.config(text=equation)
  17.    
  18.    
  19. def clear():
  20.     global equation
  21.     equation = ""
  22.     label_result.config(text=equation)
  23.    
  24.    
  25. def calculate():
  26.     global equation
  27.    
  28.     result = ""
  29.     if equation != ""
  30.         try:
  31.             result = eval(equation)
  32.         except:
  33.             ZeroDivisionError:
  34.                 result = "Cannot divide by zero"
  35.                 equation = ""
  36.                
  37.         except:
  38.             SyntaxError:
  39.                 result = "Syntax error"
  40.                 equation = ""
  41.                
  42.         except:
  43.             result = "error"
  44.             equation = ""
  45.            
  46.     label_result.config(text=result)
  47.    
  48.  
  49.  
  50. label_result = Label(root,width=25,height=2,text="",font=("arial",30))
  51. label_result.pack()
  52.  
  53. Button(root,text"C", width=5, height=1, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#3697f5", command=lambda: clear()).place(x=10,y=100)
  54. Button(root,text"/", width=5, height=1, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#2a2d36", command=lambda: show("/")).place(x=150,y=100)
  55. Button(root,text"%", width=5, height=1, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#2a2d36", command=lambda: show("%")).place(x=290,y=100)
  56. Button(root,text"*", width=5, height=1, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#2a2d36", command=lambda: show("*")).place(x=430,y=100)
  57.  
  58.  
  59. Button(root,text"7", width=5, height=1, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#2a2d36", command=lambda: show("7")).place(x=10,y=200)
  60. Button(root,text"8", width=5, height=1, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#2a2d36", command=lambda: show("8")).place(x=150,y=200)
  61. Button(root,text"9", width=5, height=1, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#2a2d36", command=lambda: show("9")).place(x=290,y=200)
  62. Button(root,text"-", width=5, height=1, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#2a2d36", command=lambda: show("-")).place(x=430,y=200)
  63.  
  64.  
  65. Button(root,text"4", width=5, height=1, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#2a2d36", command=lambda: show("4")).place(x=10,y=300)
  66. Button(root,text"5", width=5, height=1, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#2a2d36", command=lambda: show("5")).place(x=150,y=300)
  67. Button(root,text"6", width=5, height=1, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#2a2d36", command=lambda: show("6")).place(x=290,y=300)
  68. Button(root,text"+", width=5, height=1, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#2a2d36", command=lambda: show("+")).place(x=430,y=300)
  69.  
  70.  
  71. Button(root,text"1", width=5, height=1, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#2a2d36", command=lambda: show("1")).place(x=10,y=400)
  72. Button(root,text"2", width=5, height=1, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#2a2d36", command=lambda: show("2")).place(x=150,y=400)
  73. Button(root,text"3", width=5, height=1, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#2a2d36", command=lambda: show("3")).place(x=290,y=400)
  74. Button(root,text"0", width=11, height=1, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#2a2d36", command=lambda: show("0")).place(x=10,y=500)
  75.  
  76.  
  77. Button(root,text".", width=5, height=1, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#2a2d36", command=lambda: show(".")).place(x=290,y=500)
  78. Button(root,text"=", width=5, height=3, font=("arial",30,"bold"), bd=1, fg="#fff",bg="#fe9037", command=lambda: calculate()).place(x=430,y=400)
  79.  
  80. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement