Advertisement
teslariu

editor

Jan 4th, 2022
1,873
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.92 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 ACME")
  10.  
  11. ###### funciones archivo ##############
  12. def nuevo():
  13.     global ruta
  14.     mensaje.set("Nuevo archivo")
  15.     texto.delete(1.0, END)
  16.     root.title("Editor ACME")
  17.     ruta = ""
  18.    
  19. def abrir():
  20.     global ruta
  21.     mensaje.set("Abriendo archivo...")
  22.     ruta = fd.askopenfilename(
  23.                 initialdir = ".",
  24.                 filetypes = (
  25.                     ("Archivos de texto", "*.txt"),
  26.                     ("Archivos de Python", "*.py"),
  27.                     ("Todos los archivos", "*.*")
  28.                 ),
  29.                 title = "Abrir un archivo"
  30.     )
  31.     # si la ruta es válida abro el archivo como SOLO LECTURA
  32.     if ruta != "":
  33.         f = open(ruta)
  34.         contenido = f.read()
  35.         texto.delete(1.0, END)
  36.         texto.insert('insert',contenido)
  37.         f.close()
  38.         root.title(f" {ruta} - Editor ACME")
  39.         mensaje.set("Archivo abierto")
  40.    
  41.    
  42.    
  43. def guardar():
  44.     global ruta
  45.     mensaje.set("Guardando archivo...")
  46.     if ruta != "":
  47.         contenido = texto.get(1.0, 'end-1c')
  48.         # el modo w+ abre para lectura y escritura. Si ela rchivo existe
  49.         # lo sobreescribe. Si no existe, lo crea para escritura y lectura
  50.         f = open(ruta,"w+")
  51.         f.write(contenido)
  52.         f.close()
  53.         mensaje.set("Archivo guardado correctamente")
  54.    
  55.     else:
  56.         guardar_como()
  57.    
  58.    
  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 como",
  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 como...")
  84.         ruta = ""
  85.    
  86.    
  87. ###### funciones editar ################
  88. def redo():
  89.     texto.event_generate("<<Redo>>")
  90.    
  91. def undo():
  92.     texto.event_generate("<<Undo>>")
  93.  
  94. def cortar():
  95.     texto.event_generate("<<Cut>>")
  96.    
  97. def copiar():
  98.     texto.event_generate("<<Copy>>")
  99.    
  100. def pegar():
  101.     texto.event_generate("<<Paste>>")
  102.  
  103.  
  104. # creo una ruta global de los archivos
  105. ruta = ""
  106.  
  107. # creo una barra de menu
  108. menubar = Menu(root)
  109.  
  110. # creo el menu archivo
  111. archivo = Menu(menubar, tearoff=0)
  112. archivo.add_command(label="Nuevo", command=nuevo)
  113. archivo.add_command(label="Abrir", command=abrir)
  114. archivo.add_command(label="Guardar", command=guardar)
  115. archivo.add_command(label="Guardar como", command=guardar_como)
  116. archivo.add_separator()
  117. archivo.add_command(label="Salir", command=root.quit)
  118. menubar.add_cascade(label="Archivo", menu=archivo)
  119.  
  120.  
  121. # creo el menu editar
  122. undo_image = PhotoImage(file="undo.png")
  123. editar = Menu(menubar, tearoff=0)
  124. #editar.add_command(label="Deshacer", accelerator="Ctrl+Z", compound="left", image=undo_image, command=undo)
  125. editar.add_command(label="Deshacer", accelerator="Ctrl+Z", command=undo)
  126. editar.add_command(label="Rehacer", accelerator="Ctrl+Y",command=redo)
  127. editar.add_command(label="Cortar", accelerator="Ctrl+X" ,command=cortar)
  128. editar.add_command(label="Copiar", accelerator="Ctrl+C",command=copiar)
  129. editar.add_command(label="Pegar", accelerator="Ctrl+V",command=pegar)
  130. menubar.add_cascade(label="Editar", menu=editar)
  131.  
  132. # añado la barra a la ventana
  133. root.config(menu=menubar)
  134.  
  135. # scrollbar
  136. scroll = Scrollbar(root)
  137. scroll.pack(side=RIGHT, fill=Y)
  138.  
  139. # zona de texto del editor
  140. texto = Text(root)
  141. texto.pack(fill='both',expand=1)
  142. texto.config(
  143.     padx=6, pady=6, bd=0,
  144.     font = ("Helvetica",15),
  145.     bg = "black", #color de fondo
  146.     fg = "white", #color de fuente
  147.     undo = True, maxundo=20,
  148.     yscrollcommand = scroll.set
  149.     )
  150. scroll.config(command=texto.yview)
  151.  
  152. # barra de estado inferior
  153. mensaje = StringVar()
  154. mensaje.set("Bienvenidos al Editor ACME")
  155. barra_inferior = Label(root, textvar=mensaje, justify="right")
  156. barra_inferior.pack(side="left")
  157.  
  158. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement