teslariu

editor de texto

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