teslariu

text editor

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