Advertisement
teslariu

editor ACME

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