This repository was archived by the owner on Jan 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoriginal.py
More file actions
117 lines (95 loc) · 3.25 KB
/
original.py
File metadata and controls
117 lines (95 loc) · 3.25 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
Um bloco de notas minimalista criado com a estrutura PySimpleGUI
"""
import PySimpleGUI as sg
sg.ChangeLookAndFeel("BrownBlue") # Mudança do Tema
WIN_W: int = 90
WIN_H: int = 25
filename: str = None
# Variáveis de string para reduzir o loop e o código do menu!
file_new: str = "New............(CTRL+N)"
file_open: str = "Open..........(CTRL+O)"
file_save: str = "Save............(CTRL+S)"
menu_layout: list = [
["File", [file_new, file_open, file_save, "Save As", "---", "Exit"]],
["Tools", ["Word Count"]],
["Help", ["About"]],
]
layout: list = [
[sg.Menu(menu_layout)],
[sg.Text("> New file <", font=("Consolas", 10), size=(WIN_W, 1), key="_INFO_")],
[sg.Multiline(font=("Consolas", 12), size=(WIN_W, WIN_H), key="_BODY_")],
]
window: object = sg.Window(
"Notepad",
layout=layout,
margins=(0, 0),
resizable=True,
return_keyboard_events=True,
)
window.read(timeout=1)
window.maximize()
window["_BODY_"].expand(expand_x=True, expand_y=True)
def new_file() -> str:
""" Redefinir corpo e barra de informações e limpar variável de nome de arquivo! """
window["_BODY_"].update(value="")
window["_INFO_"].update(value="> New File <")
filename = None
return filename
def open_file() -> str:
""" Abra o arquivo e atualize o infobar """
try:
filename: str = sg.popup_get_file("Open File", no_window=True)
except:
return
if filename not in (None, "") and not isinstance(filename, tuple):
with open(filename, "r") as f:
window["_BODY_"].update(value=f.read())
window["_INFO_"].update(value=filename)
return filename
def save_file(filename: str):
""" Salve o arquivo instantaneamente se já estiver aberto; caso contrário, use o pop-up `save-as` """
if filename not in (None, ""):
with open(filename, "w") as f:
f.write(values.get("_BODY_"))
window["_INFO_"].update(value=filename)
else:
save_file_as()
def save_file_as() -> str:
""" Salvar novo arquivo ou salvar o arquivo existente com outro nome """
try:
filename: str = sg.popup_get_file("Save File", save_as=True, no_window=True)
except:
return
if filename not in (None, "") and not isinstance(filename, tuple):
with open(filename, "w") as f:
f.write(values.get("_BODY_"))
window["_INFO_"].update(value=filename)
return filename
def word_count():
""" Exibir contagem estimada de palavras """
words: list = [w for w in values["_BODY_"].split(" ") if w != "\n"]
word_count: int = len(words)
sg.PopupQuick("Word Count: {:,d}".format(word_count), auto_close=False)
def about_me():
sg.PopupQuick(
'"Todas as grandes coisas têm pequenos começos "- Peter Senge', auto_close=False
)
while True:
event, values = window.read()
if event in (None, "Exit"):
break
if event in (file_new, "n:78"):
filename = new_file()
if event in (file_open, "o:79"):
filename = open_file()
if event in (file_save, "s:83"):
save_file(filename)
if event in ("Save As",):
filename = save_file_as()
if event in ("Word Count",):
word_count()
if event in ("About",):
about_me()
Notepad = Notepad()
Notepad.Iniciar()