Advertisement
teslariu

editor

Feb 25th, 2022
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.12 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. from tkinter import *
  5. from tkinter import filedialog as fd
  6. from tkinter import messagebox
  7.  
  8. root = Tk()
  9. root.title("Editor ACME")
  10.  
  11.  
  12. ################ funciones  archivo ##################
  13. def nuevo():
  14.     global ruta
  15.     mensaje.set("Nuevo archivo")
  16.     opcion = messagebox.askokcancel(title="Pregunta", message="¿Desea continuar (se borrará el texto escrito)")
  17.     if opcion:
  18.         texto.delete(1.0, END)
  19.         root.title("Editor ACME")
  20.         ruta = ''
  21.    
  22.  
  23. def abrir():
  24.     global ruta
  25.     mensaje.set("Abrir archivo")
  26.     ruta = fd.askopenfilename(
  27.                 initialdir = ".",
  28.                 filetypes = (
  29.                     ("Archivos de texto", "*.txt"),
  30.                     ("Archivos de Python", "*.py"),
  31.                     ("Todos los archivos", "*.*")
  32.                 ),
  33.                 title = "Abrir un archivo"
  34.     )
  35.     # si la ruta es válida, abrir en SOLO LECTURA
  36.     if ruta != '':
  37.         f = open(ruta)
  38.         contenido = f.read()
  39.         texto.delete(1.0, END)
  40.         texto.insert('insert', contenido)
  41.         f.close()
  42.         root.title(f"{ruta} - Editor ACME")
  43.         mensaje.set("Archivo abierto")
  44.    
  45.    
  46. def guardar():
  47.     global ruta
  48.     mensaje.set("Guardar archivo")
  49.     if ruta != '':
  50.         contenido = texto.get(1.0, 'end-1c')
  51.         # el modo w+ de open() abre un archivo para lectura y
  52.         # escritura. Si el archivo existe, lo sobreescribe. Si no existe
  53.         # crea uno nuevo para lectura y escritura
  54.         f = open(ruta, "w+")
  55.         f.write(contenido)
  56.         f.close()
  57.         mensaje.set("Archivo guardado correctamente")
  58.     else:
  59.         guardar_como() 
  60.    
  61.    
  62.    
  63. def guardar_como():
  64.     global ruta
  65.     mensaje.set("Guardar archivo como ...")
  66.     f = fd.asksaveasfile(
  67.                 filetypes = (
  68.                     ("Archivos de texto", "*.txt"),
  69.                     ("Archivos de Python", "*.py"),
  70.                     ("Todos los archivos", "*.*")
  71.                 ),
  72.                 title = "Guardar archivo",
  73.                 mode = "w",
  74.                 defaultextension = '.txt'
  75.     )
  76.     if f is not None:
  77.         ruta = f.name
  78.         contenido = texto.get(1.0, 'end-1c')
  79.         f = open(ruta, 'w+')
  80.         f.write(contenido)
  81.         f.close()
  82.         mensaje.set("Archivo guardado correctamente")
  83.     else:
  84.         mensaje.set("Se ha cancelado el 'Guardar como'")
  85.         ruta = ""
  86.    
  87.    
  88.  
  89. ############### funciones editar ######################
  90. def undo():
  91.     texto.event_generate("<<Undo>>")
  92.    
  93. def redo():
  94.     texto.event_generate("<<Redo>>")
  95.    
  96. def cortar():
  97.     texto.event_generate("<<Cut>>")
  98.    
  99. def copiar():
  100.     texto.event_generate("<<Copy>>")
  101.    
  102. def pegar():
  103.     texto.event_generate("<<Paste>>")
  104.    
  105. #######################################################
  106.  
  107. # creo la ruta global de los archivos
  108. ruta = ""
  109.  
  110. # creo la barra de menu
  111. menubar = Menu(root)
  112.  
  113. # creo el menu archivo
  114. archivo = Menu(menubar, tearoff=0)
  115. archivo.add_command(label="Nuevo", command=nuevo)
  116. archivo.add_command(label="Abrir", command=abrir)
  117. archivo.add_command(label="Guardar", command=guardar)
  118. archivo.add_command(label="Guardar como", command=guardar_como)
  119. archivo.add_separator()
  120. archivo.add_command(label="Salir", command=root.quit)
  121. menubar.add_cascade(label="Archivo", menu=archivo)
  122.  
  123. # creo el menu editar
  124.  
  125. paste_image = PhotoImage(file="paste.png")
  126. editar = Menu(menubar, tearoff=0)
  127. editar.add_command(label="Deshacer", accelerator="Ctrl+Z", command=undo)
  128. editar.add_command(label="Rehacer", accelerator="Ctrl+Y", command=redo)
  129. editar.add_command(label="Cortar", accelerator="Ctrl+X", command=cortar)
  130. editar.add_command(label="Copiar", accelerator="Ctrl+C", command=copiar)
  131. editar.add_command(label="Pegar", accelerator="Ctrl+V", command=pegar)
  132. #editar.add_command(label="Pegar", accelerator="Ctrl+V", compound="left", image=paste_image, command=pegar)
  133. menubar.add_cascade(label="Editar", menu=editar)
  134.  
  135. # añado la barra a la ventana
  136. root.config(menu=menubar)
  137.  
  138. # scrollbar
  139. scroll = Scrollbar(root)
  140. scroll.pack(side=RIGHT, fill=Y)
  141.  
  142. # zona de escritura del editor
  143. texto = Text(root)
  144. texto.pack(fill='both', expand=1)
  145. texto.config(
  146.         padx=6, pady=6, bd=0,
  147.         font = ('arial', 20),
  148.         bg="beige",
  149.         undo=True, maxundo=20,
  150.         yscrollcommand = scroll.set
  151.     )
  152. scroll.config(command=texto.yview)
  153.  
  154. # barra de estado inferior
  155. mensaje = StringVar()
  156. mensaje.set("Bienvenido al Editor ACME")
  157. barra_inferior = Label(root, textvar=mensaje, justify='right')
  158. barra_inferior.pack(side='left')
  159.  
  160.  
  161. root.mainloop()
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement