Advertisement
teslariu

acme editor

Oct 13th, 2022
1,293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.48 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.geometry("600x500+50+50")
  9. root.resizable(True,True)
  10. root.minsize(300,300)
  11. root.title("Editor ACME")
  12. root.iconbitmap("icono.ico")
  13.  
  14.  
  15.  
  16. ############## funciones del menu archivo
  17. def nuevo():
  18.     global ruta
  19.     mensaje.set("Nuevo archivo")
  20.     texto.delete(1.0,END)
  21.     root.title("Editor ACME")
  22.     ruta = ""
  23.    
  24. def abrir():
  25.     global ruta
  26.     mensaje.set("Abrir archivo")
  27.     ruta = fd.askopenfilename(
  28.                 initialdir = ".",
  29.                 filetypes = (
  30.                     ("Archivos de texto", "*.txt"),
  31.                     ("Archivos de Python", "*.py"),
  32.                     ("Todos los archivos", "*.*")
  33.                 ),
  34.                 title = "Abrir un archivo"
  35.     )
  36.     # si la ruta es válida abro el archivo en modo SOLO LECTURA
  37.     if ruta:
  38.         f = open(ruta)
  39.         contenido = f.read()
  40.         texto.delete(1.0, END)
  41.         texto.insert('insert',contenido)
  42.         f.close()
  43.         root.title(f"{ruta} - Editor ACME")
  44.         mensaje.set("Archivo abierto")
  45.        
  46.        
  47.    
  48. def guardar():
  49.     global ruta
  50.     mensaje.set("Guardar archivo")
  51.     if ruta:
  52.         contenido = texto.get(1.0, 'end-1c')
  53.         # el modo w+ abre para lectura y escritura. Si el archivo existe
  54.         # lo sobreescribe. Si no existe crea uno nuevo para lectura y
  55.         # escritura
  56.         f = open(ruta, "w+")
  57.         f.write(contenido)
  58.         f.close()
  59.         mensaje.set("Archivo guardado correctamente")
  60.        
  61.     else:
  62.         guardar_como()
  63.    
  64.    
  65. def guardar_como():
  66.     global ruta
  67.     mensaje.set("Guardar 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 como",
  75.             mode = "w",
  76.             defaultextension = '.txt'
  77.     )
  78.     if f is not None:
  79.         ruta = f.name
  80.         contenido = texto.get(1.0, 'end-1c')
  81.         f = open(ruta, "w+")
  82.         f.write(contenido)
  83.         f.close()
  84.         mensaje.set("Archivo guardado correctamente")
  85.    
  86.     else:
  87.         mensaje.set("No se ha guardado el archivo")
  88.         ruta = ""
  89.  
  90.  
  91. ########## funciones del menu editar
  92. def undo():
  93.     texto.event_generate("<<Undo>>")
  94.    
  95. def redo():
  96.     texto.event_generate("<<Redo>>")
  97.    
  98. def cut():
  99.     texto.event_generate("<<Cut>>")
  100.    
  101. def copy():
  102.     texto.event_generate("<<Copy>>")
  103.    
  104. def paste():
  105.     texto.event_generate("<<Paste>>")
  106.  
  107.  
  108. ############## programa principal ##############
  109. # creo la ruta global de los archivos
  110. ruta = ""
  111.  
  112. # creo la barra de menu
  113. menubar = Menu(root)
  114.  
  115.  
  116. # creo el 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. # creo el menu editar
  127. undo_image = PhotoImage(file="undo.png")
  128. redo_image = PhotoImage(file="redo.png")
  129. editar = Menu(menubar, tearoff=0)
  130. editar.add_command(label="Deshacer", accelerator="Ctrl+Z", compound="left", image=undo_image, command=undo)
  131. editar.add_command(label="Rehacer", accelerator="Ctrl+Y", compound="left", image=redo_image, command=redo)
  132. editar.add_command(label="Cortar", accelerator="Ctrl+X", command=cut)
  133. editar.add_command(label="Copiar", accelerator="Ctrl+C", command=copy)
  134. editar.add_command(label="Pegar", accelerator="Ctrl+V", command=paste)
  135. menubar.add_cascade(label="Editar",menu=editar)
  136.  
  137. # configuro la ventana
  138. root.config(menu=menubar)
  139.  
  140. # creo la barra lateral
  141. scroll = Scrollbar(root)
  142. scroll.pack(side=RIGHT, fill=Y)
  143.  
  144. # creo la zona de edicion de texto
  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("Bienvenidos al Editor ACME")
  162. barra_inferior = Label(root, textvar = mensaje, justify="right")
  163. barra_inferior.pack(side='left')
  164.  
  165. root.mainloop()
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement