Advertisement
Guest User

hello xxpilifplayzytxx

a guest
Apr 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. from tkinter import *
  2.  
  3. calc = ""
  4.  
  5. def press(num):
  6. global calc
  7. calc = calc + str(num)
  8. eq.set(calc)
  9.  
  10. def total():
  11. try:
  12. global calc
  13. equal = str(eval(calc))
  14. eq.set(equal)
  15. calc = ""
  16.  
  17. except:
  18. eq.set("Error!")
  19. calc = ""
  20.  
  21. def clear():
  22. global calc
  23.  
  24. calc = ""
  25. eq.set("")
  26.  
  27. ##GUI Code
  28. gui = Tk()
  29.  
  30. #Properties
  31. ##gui.configure(background="white")
  32.  
  33. gui.title("Maciejs Calculator")
  34.  
  35. gui.geometry("375x150")
  36.  
  37. eq = StringVar()
  38.  
  39. calc_field = Entry(gui, textvariable=eq)
  40.  
  41. calc_field.grid(columnspan=4, ipadx=70)
  42.  
  43. eq.set("This Calculator is Amazing")
  44.  
  45. #Buttons
  46. btn1 = Button(gui, text="1", fg="black", bg="light blue", command=lambda: press(1), height=1, width=7)
  47. btn1.grid(row=2, column=0)
  48.  
  49. btn2 = Button(gui, text="2", fg="black", bg="light blue", command=lambda: press(2), height=1, width=7)
  50. btn2.grid(row=2, column=1)
  51.  
  52. btn3 = Button(gui, text="3", fg="black", bg="light blue", command=lambda: press(3), height=1, width=7)
  53. btn3.grid(row=2, column=2)
  54.  
  55. btn4 = Button(gui, text="4", fg="black", bg="light blue", command=lambda: press(4), height=1, width=7)
  56. btn4.grid(row=3, column=0)
  57.  
  58. btn5 = Button(gui, text="5", fg="black", bg="light blue", command=lambda: press(5), height=1, width=7)
  59. btn5.grid(row=3, column=1)
  60.  
  61. btn6 = Button(gui, text="6", fg="black", bg="light blue", command=lambda: press(6), height=1, width=7)
  62. btn6.grid(row=3, column=2)
  63.  
  64. btn7 = Button(gui, text="7", fg="black", bg="light blue", command=lambda: press(7), height=1, width=7)
  65. btn7.grid(row=4, column=0)
  66.  
  67. btn8 = Button(gui, text="8", fg="black", bg="light blue", command=lambda: press(8), height=1, width=7)
  68. btn8.grid(row=4, column=1)
  69.  
  70. btn9 = Button(gui, text="9", fg="black", bg="light blue", command=lambda: press(9), height=1, width=7)
  71. btn9.grid(row=4, column=2)
  72.  
  73. btn0 = Button(gui, text="0", fg="black", bg="light blue", command=lambda: press(0), height=1, width=7)
  74. btn0.grid(row=5, column=1)
  75.  
  76. btnClr = Button(gui, text="Clear", fg="black", bg="light green", command=lambda: clear(), height=1, width=7)
  77. btnClr.grid(row=5, column=0)
  78.  
  79. btnEq = Button(gui, text="=", fg="black", bg="light green", command=lambda: total(), height=1, width=7)
  80. btnEq.grid(row=5, column=2)
  81.  
  82. btnPlus = Button(gui, text="+", fg="black", bg="light green", command=lambda: press("+"), height=1, width=7)
  83. btnPlus.grid(row=2, column=3)
  84.  
  85. btnMinus = Button(gui, text="-", fg="black", bg="light green", command=lambda: press("-"), height=1, width=7)
  86. btnMinus.grid(row=3, column=3)
  87.  
  88. btnMult = Button(gui, text="*", fg="black", bg="light green", command=lambda: press("*"), height=1, width=7)
  89. btnMult.grid(row=4, column=3)
  90.  
  91. btnDiv = Button(gui, text="/", fg="black", bg="light green", command=lambda: press("/"), height=1, width=7)
  92. btnDiv.grid(row=5, column=3)
  93.  
  94. gui.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement