Advertisement
Guest User

Untitled

a guest
May 20th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. from tkinter import *
  2.  
  3. root = Tk()
  4. root.geometry("354x460")#Изменяю размер холста на 354 на 460
  5. root.title("Калькулятор")#Задаю название окна - Калькулятор
  6. rootlabel = Label(root, text="Калькултор", bg='White', font=("Times", 30, 'bold'))
  7. rootlabel.pack(side=TOP)#Поднимаю текст наверх(Калькултор теперь на верху)
  8. root.config(background='Dark gray')
  9.  
  10. textin = StringVar()
  11. operator = ""
  12.  
  13.  
  14. def clickbut(number):#создаю функцию, отвечающую за нажатие на кнопку
  15. global operator#делаю переменнуюю operator глобальной - ее можно использовать за пределами функции
  16. operator = operator + str(number)
  17. textin.set(operator)
  18.  
  19.  
  20. def equlbut():#Создаю функцию сложения
  21. global operator
  22. add = str(eval(operator))
  23. textin.set(add)
  24. operator = ''
  25.  
  26.  
  27. def equlbut():#создаю функцию для вычитания
  28. global operator
  29. sub = str(eval(operator))
  30. textin.set(sub)
  31. operator = ''
  32.  
  33.  
  34. def equlbut():#создаю функцию для умножения
  35. global operator
  36. mul = str(eval(operator))
  37. textin.set(mul)
  38. operator = ''
  39.  
  40.  
  41. def equlbut():#ну и следовательно для деления
  42. global operator
  43. div = str(eval(operator))
  44. textin.set(div)
  45. operator = ''
  46.  
  47.  
  48. def clrbut():#Делаю функцию для кнопки сброс
  49. textin.set('')
  50.  
  51.  
  52. metext = Entry(root, font=("Courier New", 12, 'bold'), textvar=textin, width=25, bd=5, bg='powder blue')
  53. metext.pack()
  54.  
  55. but1 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(1), text="1",#использую lambda функции для написания короткой одноразовой функции, анонимная функция - топ. Clickbut - индикатор нажатой кнопки, а аргумент в скобках - кнопка, ну число, на которое мы нажали.
  56. font=("Courier New", 16, 'bold'))
  57. but1.place(x=10, y=100)
  58.  
  59. but2 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(2), text="2",
  60. font=("Courier New", 16, 'bold'))
  61. but2.place(x=10, y=170)
  62.  
  63. but3 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(3), text="3",
  64. font=("Courier New", 16, 'bold'))
  65. but3.place(x=10, y=240)
  66.  
  67. but4 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(4), text="4",
  68. font=("Courier New", 16, 'bold'))
  69. but4.place(x=75, y=100)
  70.  
  71. but5 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(5), text="5",
  72. font=("Courier New", 16, 'bold'))
  73. but5.place(x=75, y=170)
  74.  
  75. but6 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(6), text="6",
  76. font=("Courier New", 16, 'bold'))
  77. but6.place(x=75, y=240)
  78.  
  79. but7 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(7), text="7",
  80. font=("Courier New", 16, 'bold'))
  81. but7.place(x=140, y=100)
  82.  
  83. but8 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(8), text="8",
  84. font=("Courier New", 16, 'bold'))
  85. but8.place(x=140, y=170)
  86.  
  87. but9 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(9), text="9",
  88. font=("Courier New", 16, 'bold'))
  89. but9.place(x=140, y=240)
  90.  
  91. but0 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(0), text="0",
  92. font=("Courier New", 16, 'bold'))
  93. but0.place(x=10, y=310)
  94.  
  95. butdot = Button(root, padx=47, pady=14, bd=4, bg='white', command=lambda: clickbut("."), text=".",
  96. font=("Courier New", 16, 'bold'))
  97. butdot.place(x=75, y=310)
  98.  
  99. butpl = Button(root, padx=14, pady=14, bd=4, bg='white', text="+", command=lambda: clickbut("+"),
  100. font=("Courier New", 16, 'bold'))
  101. butpl.place(x=205, y=100)
  102.  
  103. butsub = Button(root, padx=14, pady=14, bd=4, bg='white', text="-", command=lambda: clickbut("-"),
  104. font=("Courier New", 16, 'bold'))
  105. butsub.place(x=205, y=170)
  106.  
  107. butml = Button(root, padx=14, pady=14, bd=4, bg='white', text="*", command=lambda: clickbut("*"),
  108. font=("Courier New", 16, 'bold'))
  109. butml.place(x=205, y=240)
  110.  
  111. butdiv = Button(root, padx=14, pady=14, bd=4, bg='white', text="/", command=lambda: clickbut("/"),
  112. font=("Courier New", 16, 'bold'))
  113. butdiv.place(x=205, y=310)
  114.  
  115. butclear = Button(root, padx=14, pady=119, bd=4, bg='white', text="CE", command=clrbut, font=("Courier New", 16, 'bold'))
  116. butclear.place(x=270, y=100)
  117.  
  118. butequal = Button(root, padx=151, pady=14, bd=4, bg='white', command=equlbut, text="=", font=("Courier New", 16, 'bold'))
  119. butequal.place(x=10, y=380)
  120. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement