Advertisement
teslariu

editor_de_texto

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