-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextEdit.py
56 lines (46 loc) · 1.48 KB
/
textEdit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import PySimpleGUI as sg
from pathlib import Path
smileys = [
'happy',[':)','xD',':D','<3'],
'sad',[':(','T_T'],
'other',[':3','8======D']
]
smiley_events = smileys[1] + smileys[3] + smileys[5]
menu_layout = [
['File',['Open','Save','---','Exit']],
['Tools',['Word Count']],
['Add',smileys]
]
sg.theme('GrayGrayGray')
layout = [
[sg.Menu(menu_layout)],
[sg.Text('Untitled', key = '-DOCNAME-')],
[sg.Multiline(no_scrollbar = True, size = (40,30), key = '-TEXTBOX-')]
]
window = sg.Window('Text Editor', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == 'Open':
file_path = sg.popup_get_file('open',no_window = True)
if file_path:
file = Path(file_path)
window['-TEXTBOX-'].update(file.read_text())
window['-DOCNAME-'].update(file_path.split('/')[-1])
if event == 'Save':
file_path = sg.popup_get_file('Save as',no_window = True, save_as = True) + '.txt'
file = Path(file_path)
file.write_text(values['-TEXTBOX-'])
window['-DOCNAME-'].update(file_path.split('/')[-1])
if event == 'Word Count':
full_text = values['-TEXTBOX-']
clean_text = full_text.replace('\n',' ').split(' ')
word_count = len(clean_text)
char_count = len(''.join(clean_text))
sg.popup(f'words {word_count}\ncharacters: {char_count}')
if event in smiley_events:
current_text = values['-TEXTBOX-']
new_text = current_text + ' ' + event
window['-TEXTBOX-'].update(new_text)
window.close()