ClearCode

Converter

Apr 1st, 2022
2,902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. import PySimpleGUI as sg
  2.  
  3. layout = [
  4.     [
  5.         sg.Input(key = '-INPUT-'),
  6.         sg.Spin(['km to mile','kg to pound','sec to min'], key = '-UNITS-'),
  7.         sg.Button('Convert', key = '-CONVERT-')
  8.     ],
  9.     [sg.Text('Output', key = '-OUTPUT-')]
  10. ]
  11.  
  12. window = sg.Window('Converter',layout)
  13.  
  14. while True:
  15.     event, values = window.read()
  16.  
  17.     if event == sg.WIN_CLOSED:
  18.         break
  19.  
  20.     if event == '-CONVERT-':
  21.         input_value = values['-INPUT-']
  22.         if input_value.isnumeric():
  23.             match values['-UNITS-']:
  24.                 case 'km to mile':
  25.                     output = round(float(input_value) * 0.6214,2)
  26.                     output_string = f'{input_value} km are {output} miles.'
  27.                 case 'kg to pound':
  28.                     output = round(float(input_value) * 2.20462,2)
  29.                     output_string = f'{input_value} kg are {output} pounds.'
  30.                 case 'sec to min':
  31.                     output = round(float(input_value) / 60,2)
  32.                     output_string = f'{input_value} seconds are {output} minutes.'
  33.  
  34.             window['-OUTPUT-'].update(output_string)
  35.         else:
  36.             window['-OUTPUT-'].update('Please enter a number')
  37.  
  38. window.close()
Advertisement
Add Comment
Please, Sign In to add comment