Advertisement
teslariu

editor ACME

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