teslariu

editor

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