-
Notifications
You must be signed in to change notification settings - Fork 41
/
notepad.py
110 lines (90 loc) · 3.19 KB
/
notepad.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
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
from tkinter.filedialog import *
from tkinter.messagebox import *
import pyperclip
import time
import threading
import tkinter as tk
import os
#FUNCIÓN PARA GUARDAR TEXTO EN ARCHIVO .TXT
def saveFile():
if len(entry.get('1.0',END)) > 1:
doc = asksaveasfilename(title="save text",
initialfile="my_text",
defaultextension=".txt")
if doc != "":
new_file = open(doc,"w")
text = str(entry.get(1.0, END))
new_file.write(text)
new_file.close()
#FUNCIÓN PARA IMPORTAR TEXTO
def import_text():
clearFile()
ultima_copia = pyperclip.paste().strip()
showinfo("COPIAR TEXTO","Seleccione texto a copiar.")
while True:
time.sleep(0.1)
copia = pyperclip.paste().strip()
if copia != ultima_copia:
entry.insert(END,copia)
ultima_copia = copia
break
#FUNCIÓN PARA INICIAR "import_text()" DE FORMA CONCURRENTE
def init_copy():
threading.Thread(target=import_text).start()
#FUNCIÓN PARA ABRIR Y LEER ARCHIVOS DE TEXTO
def openFile():
file = askopenfile(mode = 'r', filetype =[('text_files', '*.txt')])
if file is not None:
if len(entry.get('1.0',END))>1:
clearFile() #LLAMADA A FUNCIÓN 'clearFile()'
content = file.read()
entry.insert(INSERT, content)
#FUNCIÓN PARA LIMPIAR ÁREA DE TEXTO
def clearFile():
entry.delete('1.0',END)
#FUNCIÓN PARA CERRAR VENTANA.
def destroy_window():
if len(entry.get('1.0',END)) > 1:
question = askquestion("GUARDAR?",
"¿Desea guardar los cambios hechos en el documento?")
if question == "yes":
saveFile() #LLAMADA A FUNCIÓN "saveFile()"
else:
pass
canvas.destroy()
#CREACIÓN VENTANA
canvas = tk.Tk()
canvas.geometry("550x660")
canvas.title("Notepad")
canvas.config(bg = "white")
#SALIDA PARA DIRECTORIO ACTUAL
current_dir = StringVar()
current_dir.set(os.getcwd())
Entry(canvas,textvariable=current_dir).pack(padx=0,pady=0,fill="both")
#CONTENEDOR PARA LOS BOTONES
top = Frame(canvas)
top.pack(padx = 10, pady = 3, anchor = "nw")
#BOTONES
b1 = Button(canvas,text="Open",bg="white",command=openFile)
b1.pack(in_ = top, side=LEFT)
b2 = Button(canvas,text="Save",bg="white",command=saveFile)
b2.pack(in_ = top, side=LEFT)
b3 = Button(canvas,text="Clear",bg="white",command=clearFile)
b3.pack(in_ = top, side=LEFT)
b5 = Button(canvas,text="Paste",bg="white",command=init_copy)
b5.pack(in_ = top, side=LEFT)
b4 = Button(canvas,text="Exit",bg="white",command=destroy_window)
b4.pack(in_ = top, side=LEFT)
#ELEMENTO CANVAS PARA ENTRADA DE TEXTO Y BARRA DE SCROLL
canvas2 = Canvas(canvas)
canvas2.pack(padx = 10, pady = 5, expand = TRUE, fill = BOTH)
#BARRA DE SCROLL LATERAL
scrollbar = Scrollbar(canvas2,orient=VERTICAL)
scrollbar.pack(side = RIGHT, fill = Y)
#ENTRADA PARA TEXTO
entry = Text(canvas2, wrap = WORD, bg = "azure", font = ("poppins", 15))#wrap = WORD
entry.pack(side = LEFT, fill = BOTH, expand = TRUE)
#CONECTAR BARRA DE SCROLL Y ENTRADA
entry.config(yscrollcommand = scrollbar.set)
scrollbar.config(command = entry.yview)
canvas.mainloop()