-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbloco_notas.py
63 lines (53 loc) · 2.31 KB
/
bloco_notas.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
57
58
59
60
61
62
63
import tkinter as tk
from tkinter import scrolledtext, filedialog, messagebox
class NotepadApp:
def __init__(self, master):
self.master = master
self.master.title("Bloco de Notas")
self.textarea = scrolledtext.ScrolledText(self.master, wrap=tk.WORD)
self.textarea.pack(expand=True, fill='both')
self.menu_bar = tk.Menu(self.master)
self.master.config(menu=self.menu_bar)
self.file_menu = tk.Menu(self.menu_bar, tearoff=0)
self.menu_bar.add_cascade(label="Arquivo", menu=self.file_menu)
self.file_menu.add_command(label="Novo", command=self.new_file)
self.file_menu.add_command(label="Abrir", command=self.open_file)
self.file_menu.add_command(label="Salvar", command=self.save_file)
self.file_menu.add_command(label="Salvar como...", command=self.save_as_file)
self.file_menu.add_separator()
self.file_menu.add_command(label="Sair", command=self.exit_app)
def new_file(self):
self.textarea.delete(1.0, tk.END)
def open_file(self):
file_path = filedialog.askopenfilename(filetypes=[("Arquivos de texto", "*.txt")])
if file_path:
with open(file_path, "r") as file:
content = file.read()
self.textarea.delete(1.0, tk.END)
self.textarea.insert(1.0, content)
def save_file(self):
content = self.textarea.get(1.0, tk.END)
if not content.strip():
messagebox.showerror("Erro", "Não há conteúdo para salvar.")
return
file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Arquivos de texto", "*.txt")])
if file_path:
with open(file_path, "w") as file:
file.write(content)
def save_as_file(self):
content = self.textarea.get(1.0, tk.END)
if not content.strip():
messagebox.showerror("Erro", "Não há conteúdo para salvar.")
return
file_path = filedialog.asksaveasfilename(filetypes=[("Arquivos de texto", "*.txt")])
if file_path:
with open(file_path, "w") as file:
file.write(content)
def exit_app(self):
self.master.destroy()
def main():
root = tk.Tk()
app = NotepadApp(root)
root.mainloop()
if __name__ == "__main__":
main()