Advertisement
teslariu

EDITOR

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