Advertisement
teslariu

editor

Nov 29th, 2021
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.86 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4.  
  5. from tkinter import *
  6. from tkinter import filedialog as fd
  7.  
  8. root = Tk()
  9. root.title("Editor de texto")
  10.  
  11. #### funciones del menu archivo ##########
  12. def nuevo():
  13.     global ruta
  14.     root.title("Mi Editor")
  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. def guardar():
  44.     global ruta
  45.     mensaje.set("Guardar archivo")
  46.     if ruta != "":
  47.         contenido = texto.get(1.0, "end-1c")
  48.         # el modo w+ abre para lectura y escritura. Si el archivo
  49.         # existe, lo sobreescribe. Si no existe, creo uno nuevo para
  50.         # escritura y lectura
  51.         f = open(ruta, "w+")
  52.         f.write(contenido)
  53.         f.close()
  54.         mensaje.set("Archivo guardado correctamente")
  55.     else:
  56.         guardar_como()
  57.    
  58.    
  59. def guardar_como():
  60.     global ruta
  61.     mensaje.set("Guardar archivo como")
  62.     f = fd.asksaveasfile(
  63.                 initialdir = ".",
  64.                 filetypes = (
  65.                     ("Archivos de texto", "*.txt"),
  66.                     ("Archivos de Python", "*.py"),
  67.                     ("Todos los archivos", "*.*"),
  68.                 ),
  69.                 title = "Guardar archivo",
  70.                 mode = "w",
  71.                 defaultextension = ".txt"
  72.     )
  73.    
  74.     if f is not None:
  75.         ruta = f.name
  76.         contenido = texto.get(1.0, "end-1c")
  77.         f = open(ruta, "w+")
  78.         f.write(contenido)
  79.         f.close()
  80.         mensaje.set("Archivo guardado correctamente")
  81.     else:
  82.         mensaje.set("Se ha cancelado el 'guardar como'")
  83.         ruta = ""
  84.  
  85.  
  86. #### funciones del menu editar ##########
  87. def undo():
  88.     texto.event_generate("<<Undo>>")
  89.    
  90. def redo():
  91.     texto.event_generate("<<Redo>>")
  92.    
  93. def cortar():
  94.     texto.event_generate("<<Cut>>")
  95.    
  96. def copiar():
  97.     texto.event_generate("<<Copy>>")
  98.    
  99. def pegar():
  100.     texto.event_generate("<<Paste>>")
  101.    
  102.    
  103.  
  104.  
  105. # creo una ruta global para 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.  
  122. # menu editar
  123. cut_image = PhotoImage(file="cut.png")
  124.  
  125. editar = Menu(menubar, tearoff=0)
  126. editar.add_command(label="Deshacer", accelerator="Ctrl+Z", command=undo)
  127. editar.add_command(label="Rehacer", accelerator="Ctrl+Y", command=redo)
  128. #editar.add_command(label="Cortar", accelerator="Ctrl+X", compound="left", image=cut_image, command=cortar)
  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.  
  133.  
  134.  
  135. menubar.add_cascade(label="Editar", menu=editar)
  136.  
  137. # añado la barra a la ventana
  138. root.config(menu=menubar)
  139.  
  140. # scrollbar
  141. scroll = Scrollbar(root)
  142. scroll.pack(side=RIGHT, fill=Y)
  143.  
  144. # zona de escritura
  145. texto = Text(root)
  146. texto.pack(fill="both", expand=1)
  147. texto.config(
  148.     padx=6, pady=6, bd=0,
  149.     font=("arial",12),
  150.     bg="beige",
  151.     undo=True, maxundo=20,
  152.     yscrollcommand = scroll.set
  153.     )
  154. scroll.config(command=texto.yview)
  155.  
  156. # barra de estado inferior
  157. mensaje = StringVar()
  158. mensaje.set("Bienvenidos a mi Editor")
  159. barra_inferior = Label(root, textvariable=mensaje, justify="right")
  160. barra_inferior.pack(side="left")
  161.  
  162. root.mainloop()
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement