ClearCode

Calculator

Apr 1st, 2022
2,990
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. import PySimpleGUI as sg
  2.  
  3. def create_window(theme):
  4.     sg.theme(theme)
  5.     sg.set_options(font = 'Franklin 14', button_element_size = (6,3))
  6.     button_size = (6,3)
  7.     layout = [
  8.         [sg.Text(
  9.             '',
  10.             font = 'Franklin 26',
  11.             justification = 'right',
  12.             expand_x = True,
  13.             pad = (10,20),
  14.             right_click_menu = theme_menu,
  15.             key = '-TEXT-')
  16.         ],
  17.         [sg.Button('Clear', expand_x = True), sg.Button('Enter', expand_x = True)],
  18.         [sg.Button(7, size = button_size),sg.Button(8, size = button_size),sg.Button(9, size = button_size),sg.Button('*', size = button_size)],
  19.         [sg.Button(4, size = button_size),sg.Button(5, size = button_size),sg.Button(6, size = button_size),sg.Button('/', size = button_size)],
  20.         [sg.Button(1, size = button_size),sg.Button(2, size = button_size),sg.Button(3, size = button_size),sg.Button('-', size = button_size)],
  21.         [sg.Button(0, expand_x = True),sg.Button('.', size = button_size),sg.Button('+', size = button_size)],
  22.     ]
  23.  
  24.     return sg.Window('Calculator', layout)
  25.  
  26. theme_menu = ['menu',['LightGrey1','dark','DarkGray8','random']]
  27. window = create_window('dark')
  28.  
  29. current_num = []
  30. full_operation = []
  31.  
  32. while True:
  33.     event, values = window.read()
  34.     if event == sg.WIN_CLOSED:
  35.         break
  36.  
  37.     if event in theme_menu[1]:
  38.         window.close()
  39.         window = create_window(event)
  40.        
  41.     if event in ['0','1','2','3','4','5','6','7','8','9','.']:
  42.         current_num.append(event)
  43.         num_string = ''.join(current_num)
  44.         window['-TEXT-'].update(num_string)
  45.  
  46.     if event in ['+','-','/','*']:
  47.         full_operation.append(''.join(current_num))
  48.         current_num = []
  49.         full_operation.append(event)
  50.         window['-TEXT-'].update('')
  51.  
  52.     if event == 'Enter':
  53.         full_operation.append(''.join(current_num))
  54.         result = eval(' '.join(full_operation))
  55.         window['-TEXT-'].update(result)
  56.         full_operation = []
  57.  
  58.     if event == 'Clear':
  59.         current_num = []
  60.         full_operation = []
  61.         window['-TEXT-'].update('')
  62.  
  63. window.close()
Advertisement
Add Comment
Please, Sign In to add comment