ClearCode

Text Editor

Apr 1st, 2022
2,269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. import PySimpleGUI as sg
  2. from pathlib import Path
  3.  
  4. smileys = [
  5.     'happy',[':)','xD',':D','<3'],
  6.     'sad',[':(','T_T'],
  7.     'other',[':3']
  8. ]
  9. smiley_events = smileys[1] + smileys[3] + smileys[5]
  10.  
  11. menu_layout = [
  12.     ['File',['Open','Save','---','Exit']],
  13.     ['Tools',['Word Count']],
  14.     ['Add',smileys]
  15. ]
  16.  
  17. sg.theme('GrayGrayGray')
  18. layout = [
  19.     [sg.Menu(menu_layout)],
  20.     [sg.Text('Untitled', key = '-DOCNAME-')],
  21.     [sg.Multiline(no_scrollbar = True, size = (40,30), key = '-TEXTBOX-')]
  22. ]
  23.  
  24. window = sg.Window('Text Editor', layout)
  25.  
  26. while True:
  27.     event, values = window.read()
  28.     if event == sg.WIN_CLOSED:
  29.         break
  30.  
  31.     if event == 'Open':
  32.         file_path = sg.popup_get_file('open',no_window = True)
  33.         if file_path:
  34.             file = Path(file_path)
  35.             window['-TEXTBOX-'].update(file.read_text())
  36.             window['-DOCNAME-'].update(file_path.split('/')[-1])
  37.  
  38.     if event == 'Save':
  39.         file_path = sg.popup_get_file('Save as',no_window = True, save_as = True) + '.txt'
  40.         file = Path(file_path)
  41.         file.write_text(values['-TEXTBOX-'])
  42.         window['-DOCNAME-'].update(file_path.split('/')[-1])
  43.  
  44.     if event == 'Word Count':
  45.         full_text = values['-TEXTBOX-']
  46.         clean_text = full_text.replace('\n',' ').split(' ')
  47.         word_count = len(clean_text)
  48.         char_count = len(''.join(clean_text))
  49.         sg.popup(f'words {word_count}\ncharacters: {char_count}')
  50.    
  51.     if event in smiley_events:
  52.         current_text = values['-TEXTBOX-']
  53.         new_text = current_text + ' ' + event
  54.         window['-TEXTBOX-'].update(new_text)
  55.  
  56. window.close()
Advertisement
Add Comment
Please, Sign In to add comment