Indie_Coder

Calculator GUI App

Oct 10th, 2022
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | Source Code | 0 0
  1. import PySimpleGUI as sg
  2.  
  3. btn_size = (3, 1)
  4. sg.theme("LightBlue5")
  5. sg.set_options(font=("Rockwell", 25))
  6.  
  7. layout = [
  8.     [sg.Push(), sg.Text("0", font="Consolas 20", key="-FORMULA-")],
  9.     [sg.Push(), sg.Text("0", font="Consolas 40", key="-DISPLAY-")],
  10.     [
  11.         sg.Button("*", size=btn_size),
  12.         sg.Button("del", size=btn_size, button_color="black on red"),
  13.         sg.Button("clear", expand_x=True, button_color="black on orange"),
  14.     ],
  15.     [
  16.         sg.Button("7", size=btn_size),
  17.         sg.Button("8", size=btn_size),
  18.         sg.Button("9", size=btn_size),
  19.         sg.Button("+", size=btn_size),
  20.     ],
  21.     [
  22.         sg.Button("4", size=btn_size),
  23.         sg.Button("5", size=btn_size),
  24.         sg.Button("6", size=btn_size),
  25.         sg.Button("-", size=btn_size),
  26.     ],
  27.     [
  28.         sg.Button("1", size=btn_size),
  29.         sg.Button("2", size=btn_size),
  30.         sg.Button("3", size=btn_size),
  31.         sg.Button("/", size=btn_size),
  32.     ],
  33.     [
  34.         sg.Button("0", size=btn_size),
  35.         sg.Button(".", size=btn_size),
  36.         sg.Button("=", expand_x=True, button_color="green3"),
  37.     ],
  38. ]
  39.  
  40. window = sg.Window("My Calculator", layout)
  41. eval("print('hello')")
  42.  
  43. current_num = []
  44. formula = []
  45. # Main Event Loop
  46. while True:
  47.     event, values = window.read()
  48.  
  49.     if event == sg.WIN_CLOSED:
  50.         break
  51.  
  52.     if event.isdigit() or (event == "." and "." not in current_num):
  53.         # Skip first 0
  54.         if len(current_num) == 0 and event == "0":
  55.             continue
  56.         # Extend current number
  57.         current_num.append(event)
  58.         num_str = "".join(current_num)
  59.         window.Element("-DISPLAY-").update(num_str)
  60.  
  61.     if event in ["+", "-", "*", "/"] and len(current_num):
  62.         # Append Operation
  63.         formula.append(num_str)
  64.         if current_num[-1] == ".":
  65.             formula.append("0")
  66.         formula.append(event)
  67.         window["-FORMULA-"].update("".join(formula))
  68.         current_num = []
  69.  
  70.     if event == "del" and len(current_num):
  71.         current_num.pop()
  72.         num_str = "".join(current_num) if len(current_num) else "0"
  73.         window["-DISPLAY-"].update(num_str)
  74.  
  75.     if event == "clear":
  76.         current_num = []
  77.         formula = []
  78.         window["-DISPLAY-"].update(0)
  79.         window["-FORMULA-"].update(0)
  80.  
  81.     # Calculating the result
  82.     if event == "=" and len(current_num) and len(formula):
  83.         formula.append(num_str)
  84.         formula_string = "".join(formula)
  85.         result = eval(formula_string)
  86.         window["-DISPLAY-"].update(result)
  87.         window["-FORMULA-"].update(formula_string)
  88.         current_num = []
  89.         formula = []
  90.  
  91.  
  92. window.close()
  93.  
Advertisement
Add Comment
Please, Sign In to add comment