from tkinter import ttk
from tkinter import *
window = Tk()
window.resizable(True, True)
WinFrame = ttk.Frame(window)
WinFrame.grid(sticky=NSEW)
window.grid_columnconfigure(0, weight=1)
window.grid_rowconfigure(0, weight=1)
for col in range(5):
WinFrame.grid_columnconfigure(col, weight=1)
WinFrame.grid_rowconfigure(col, weight=1)
window.title('Calc')
res = ""
display = StringVar()
calculation = ttk.Label(WinFrame, textvariable=display)
calculation.grid(columnspan=4)
def button_press(number):
global res
res += str(number)
display.set(res)
def result():
global res
try:
display.set(str(eval(res)))
except (ZeroDivisionError, SyntaxError):
display.set("ERROR")
finally:
res = ''
def clear():
global res
res = ''
display.set(res)
Button1 = ttk.Button(WinFrame, text='1', command=lambda: button_press(1))
Button1.grid(row=1, column=0, sticky=NSEW)
Button2 = ttk.Button(WinFrame, text='2', command=lambda: button_press(2))
Button2.grid(row=1, column=1, sticky=NSEW)
Button3 = ttk.Button(WinFrame, text='3', command=lambda: button_press(3))
Button3.grid(row=1, column=2, sticky=NSEW)
Button4 = ttk.Button(WinFrame, text='4', command=lambda: button_press(4))
Button4.grid(row=2, column=0, sticky=NSEW)
Button5 = ttk.Button(WinFrame, text='5', command=lambda: button_press(5))
Button5.grid(row=2, column=1, sticky=NSEW)
Button6 = ttk.Button(WinFrame, text='6', command=lambda: button_press(6))
Button6.grid(row=2, column=2, sticky=NSEW)
Button7 = ttk.Button(WinFrame, text='7', command=lambda: button_press(7))
Button7.grid(row=3, column=0, sticky=NSEW)
Button8 = ttk.Button(WinFrame, text='8', command=lambda: button_press(8))
Button8.grid(row=3, column=1 ,sticky=NSEW)
Button9 = ttk.Button(WinFrame, text='9', command=lambda: button_press(9))
Button9.grid(row=3, column=2 ,sticky=NSEW)
Button0 = ttk.Button(WinFrame, text='0', command=lambda: button_press(0))
Button0.grid(row=4, column=1 ,sticky=NSEW)
ButtonPlus = ttk.Button(WinFrame, text='+', command=lambda: button_press('+'))
ButtonPlus.grid(row=1, column=3 ,sticky=NSEW)
ButtonMinus = ttk.Button(WinFrame, text='-', command=lambda: button_press('-'))
ButtonMinus.grid(row=2, column=3 ,sticky=NSEW)
ButtonMultiply = ttk.Button(WinFrame, text='*', command=lambda: button_press('*'))
ButtonMultiply.grid(row=3, column=3 ,sticky=NSEW)
ButtonDivide = ttk.Button(WinFrame, text='/', command=lambda: button_press('/'))
ButtonDivide.grid(row=4, column=3 ,sticky=NSEW)
ButtonResult = ttk.Button(WinFrame, text='=', command=result)
ButtonResult.grid(row=1, column=4, rowspan=4, sticky=NSEW)
ButtonC = ttk.Button(WinFrame, text='C', command=clear)
ButtonC.grid(row=4, column=0, sticky=NSEW)
ButtonComma = ttk.Button(WinFrame, text='.', command=lambda: button_press('.'))
ButtonComma.grid(row=4, column=2, sticky=NSEW)
window.mainloop()
ZnJvbSB0a2ludGVyIGltcG9ydCB0dGsKZnJvbSB0a2ludGVyIGltcG9ydCAqCndpbmRvdyA9IFRrKCkKd2luZG93LnJlc2l6YWJsZShUcnVlLCBUcnVlKQpXaW5GcmFtZSA9IHR0ay5GcmFtZSh3aW5kb3cpCldpbkZyYW1lLmdyaWQoc3RpY2t5PU5TRVcpCndpbmRvdy5ncmlkX2NvbHVtbmNvbmZpZ3VyZSgwLCB3ZWlnaHQ9MSkKd2luZG93LmdyaWRfcm93Y29uZmlndXJlKDAsIHdlaWdodD0xKQpmb3IgY29sIGluIHJhbmdlKDUpOgogICAgV2luRnJhbWUuZ3JpZF9jb2x1bW5jb25maWd1cmUoY29sLCB3ZWlnaHQ9MSkKICAgIFdpbkZyYW1lLmdyaWRfcm93Y29uZmlndXJlKGNvbCwgd2VpZ2h0PTEpCgp3aW5kb3cudGl0bGUoJ0NhbGMnKQpyZXMgPSAiIgpkaXNwbGF5ID0gU3RyaW5nVmFyKCkKY2FsY3VsYXRpb24gPSB0dGsuTGFiZWwoV2luRnJhbWUsIHRleHR2YXJpYWJsZT1kaXNwbGF5KQpjYWxjdWxhdGlvbi5ncmlkKGNvbHVtbnNwYW49NCkKCgpkZWYgYnV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