Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import PySimpleGUI as sg
- btn_size = (3, 1)
- sg.theme("LightBlue5")
- sg.set_options(font=("Rockwell", 25))
- layout = [
- [sg.Push(), sg.Text("0", font="Consolas 20", key="-FORMULA-")],
- [sg.Push(), sg.Text("0", font="Consolas 40", key="-DISPLAY-")],
- [
- sg.Button("*", size=btn_size),
- sg.Button("del", size=btn_size, button_color="black on red"),
- sg.Button("clear", expand_x=True, button_color="black on orange"),
- ],
- [
- sg.Button("7", size=btn_size),
- sg.Button("8", size=btn_size),
- sg.Button("9", size=btn_size),
- sg.Button("+", size=btn_size),
- ],
- [
- sg.Button("4", size=btn_size),
- sg.Button("5", size=btn_size),
- sg.Button("6", size=btn_size),
- sg.Button("-", size=btn_size),
- ],
- [
- sg.Button("1", size=btn_size),
- sg.Button("2", size=btn_size),
- sg.Button("3", size=btn_size),
- sg.Button("/", size=btn_size),
- ],
- [
- sg.Button("0", size=btn_size),
- sg.Button(".", size=btn_size),
- sg.Button("=", expand_x=True, button_color="green3"),
- ],
- ]
- window = sg.Window("My Calculator", layout)
- eval("print('hello')")
- current_num = []
- formula = []
- # Main Event Loop
- while True:
- event, values = window.read()
- if event == sg.WIN_CLOSED:
- break
- if event.isdigit() or (event == "." and "." not in current_num):
- # Skip first 0
- if len(current_num) == 0 and event == "0":
- continue
- # Extend current number
- current_num.append(event)
- num_str = "".join(current_num)
- window.Element("-DISPLAY-").update(num_str)
- if event in ["+", "-", "*", "/"] and len(current_num):
- # Append Operation
- formula.append(num_str)
- if current_num[-1] == ".":
- formula.append("0")
- formula.append(event)
- window["-FORMULA-"].update("".join(formula))
- current_num = []
- if event == "del" and len(current_num):
- current_num.pop()
- num_str = "".join(current_num) if len(current_num) else "0"
- window["-DISPLAY-"].update(num_str)
- if event == "clear":
- current_num = []
- formula = []
- window["-DISPLAY-"].update(0)
- window["-FORMULA-"].update(0)
- # Calculating the result
- if event == "=" and len(current_num) and len(formula):
- formula.append(num_str)
- formula_string = "".join(formula)
- result = eval(formula_string)
- window["-DISPLAY-"].update(result)
- window["-FORMULA-"].update(formula_string)
- current_num = []
- formula = []
- window.close()
Advertisement
Add Comment
Please, Sign In to add comment