Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- root = Tk()
- root.geometry("354x460")#Изменяю размер холста на 354 на 460
- root.title("Калькулятор")#Задаю название окна - Калькулятор
- rootlabel = Label(root, text="Калькултор", bg='White', font=("Times", 30, 'bold'))
- rootlabel.pack(side=TOP)#Поднимаю текст наверх(Калькултор теперь на верху)
- root.config(background='Dark gray')
- textin = StringVar()
- operator = ""
- def clickbut(number):#создаю функцию, отвечающую за нажатие на кнопку
- global operator#делаю переменнуюю operator глобальной - ее можно использовать за пределами функции
- operator = operator + str(number)
- textin.set(operator)
- def equlbut():#Создаю функцию сложения
- global operator
- add = str(eval(operator))
- textin.set(add)
- operator = ''
- def equlbut():#создаю функцию для вычитания
- global operator
- sub = str(eval(operator))
- textin.set(sub)
- operator = ''
- def equlbut():#создаю функцию для умножения
- global operator
- mul = str(eval(operator))
- textin.set(mul)
- operator = ''
- def equlbut():#ну и следовательно для деления
- global operator
- div = str(eval(operator))
- textin.set(div)
- operator = ''
- def clrbut():#Делаю функцию для кнопки сброс
- textin.set('')
- metext = Entry(root, font=("Courier New", 12, 'bold'), textvar=textin, width=25, bd=5, bg='powder blue')
- metext.pack()
- but1 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(1), text="1",#использую lambda функции для написания короткой одноразовой функции, анонимная функция - топ. Clickbut - индикатор нажатой кнопки, а аргумент в скобках - кнопка, ну число, на которое мы нажали.
- font=("Courier New", 16, 'bold'))
- but1.place(x=10, y=100)
- but2 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(2), text="2",
- font=("Courier New", 16, 'bold'))
- but2.place(x=10, y=170)
- but3 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(3), text="3",
- font=("Courier New", 16, 'bold'))
- but3.place(x=10, y=240)
- but4 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(4), text="4",
- font=("Courier New", 16, 'bold'))
- but4.place(x=75, y=100)
- but5 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(5), text="5",
- font=("Courier New", 16, 'bold'))
- but5.place(x=75, y=170)
- but6 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(6), text="6",
- font=("Courier New", 16, 'bold'))
- but6.place(x=75, y=240)
- but7 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(7), text="7",
- font=("Courier New", 16, 'bold'))
- but7.place(x=140, y=100)
- but8 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(8), text="8",
- font=("Courier New", 16, 'bold'))
- but8.place(x=140, y=170)
- but9 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(9), text="9",
- font=("Courier New", 16, 'bold'))
- but9.place(x=140, y=240)
- but0 = Button(root, padx=14, pady=14, bd=4, bg='white', command=lambda: clickbut(0), text="0",
- font=("Courier New", 16, 'bold'))
- but0.place(x=10, y=310)
- butdot = Button(root, padx=47, pady=14, bd=4, bg='white', command=lambda: clickbut("."), text=".",
- font=("Courier New", 16, 'bold'))
- butdot.place(x=75, y=310)
- butpl = Button(root, padx=14, pady=14, bd=4, bg='white', text="+", command=lambda: clickbut("+"),
- font=("Courier New", 16, 'bold'))
- butpl.place(x=205, y=100)
- butsub = Button(root, padx=14, pady=14, bd=4, bg='white', text="-", command=lambda: clickbut("-"),
- font=("Courier New", 16, 'bold'))
- butsub.place(x=205, y=170)
- butml = Button(root, padx=14, pady=14, bd=4, bg='white', text="*", command=lambda: clickbut("*"),
- font=("Courier New", 16, 'bold'))
- butml.place(x=205, y=240)
- butdiv = Button(root, padx=14, pady=14, bd=4, bg='white', text="/", command=lambda: clickbut("/"),
- font=("Courier New", 16, 'bold'))
- butdiv.place(x=205, y=310)
- butclear = Button(root, padx=14, pady=119, bd=4, bg='white', text="CE", command=clrbut, font=("Courier New", 16, 'bold'))
- butclear.place(x=270, y=100)
- butequal = Button(root, padx=151, pady=14, bd=4, bg='white', command=equlbut, text="=", font=("Courier New", 16, 'bold'))
- butequal.place(x=10, y=380)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement