Advertisement
teslariu

editor

Feb 12th, 2022
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.93 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 de texto ACME")
  14.     ruta = ""
  15.    
  16. def abrir():
  17.     global ruta
  18.     mensaje.set("Abrir archivo")
  19.     ruta = fd.askopenfilename(
  20.                     initialdir = ".",
  21.                     filetypes = (
  22.                         ("Archivos de texto", "*.txt"),
  23.                         ("Archivos de Python", "*.py"),
  24.                         ("Todos los archivos", "*.*")
  25.                     ),
  26.                     title = "Abrir un archivo"
  27.     )
  28.     if ruta != "":
  29.         f = open(ruta)
  30.         contenido = f.read()
  31.         texto.delete(1.0,END)
  32.         texto.insert('insert', contenido)
  33.         f.close()
  34.         root.title(f"{ruta} - Editor ACME")
  35.         mensaje.set("Archivo abierto")
  36.        
  37.        
  38.    
  39. def guardar():
  40.     global ruta
  41.     mensaje.set("Guardar archivo")
  42.     if ruta != "":
  43.         contenido = texto.get(1.0, 'end-1c')
  44.         # el modo w+ abre para lectura y escritura. Si el archivo existe
  45.         # lo sobreescribe. Si no existe, lo crea para escritura y lectura
  46.         f = open(ruta, "w+")
  47.         f.write(contenido)
  48.         f.close()
  49.         mensaje.set("Archivo guardado")
  50.        
  51.     else:
  52.         guardar_como()
  53.        
  54.    
  55.    
  56. def guardar_como():
  57.     global ruta
  58.     mensaje.set("Guardar archivo como")
  59.     f = fd.asksaveasfile(
  60.                     filetypes = (
  61.                         ("Archivos de texto", "*.txt"),
  62.                         ("Archivos de Python", "*.py"),
  63.                         ("Todos los archivos", "*.*")
  64.                     ),
  65.                     title = "Guardar como",
  66.                     mode = "w",
  67.                     defaultextension = '.txt'
  68.     )
  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")
  77.    
  78.     else:
  79.         mensaje.set("Se ha cancelado el guardado")
  80.         ruta = ""
  81.  
  82.  
  83. ##### funciones del menu editar #################
  84. def deshacer():
  85.     texto.event_generate("<<Undo>>")
  86.    
  87. def rehacer():
  88.     texto.event_generate("<<Redo>>")
  89.    
  90. def cortar():
  91.     texto.event_generate("<<Cut>>")
  92.    
  93. def copiar():
  94.     texto.event_generate("<<Copy>>")
  95.    
  96. def pegar():
  97.     texto.event_generate("<<Paste>>")
  98.  
  99.  
  100. # creo una variable para almacenar la ruta de los archivos
  101. ruta = ""
  102.  
  103.  
  104.  
  105.  
  106. ####### parte gráfica  #################
  107. root = Tk()
  108. root.title("Editor de texto ACME")
  109.  
  110. # creo la barra de menu
  111. menubar = Menu(root)
  112.  
  113. # menu archivo
  114. archivo = Menu(menubar, tearoff=0)
  115. archivo.add_command(label="Nuevo", command=nuevo)
  116. archivo.add_command(label="Abrir", command=abrir)
  117. archivo.add_command(label="Guardar", command=guardar)
  118. archivo.add_command(label="Guardar Como", command=guardar_como)
  119. archivo.add_separator()
  120. archivo.add_command(label="Salir", command=root.quit)
  121. menubar.add_cascade(label="Archivo", menu=archivo)
  122.  
  123.  
  124. # menu editar
  125. #paste_image=PhotoImage(file="paste.png")
  126.  
  127. editar = Menu(menubar, tearoff=0)
  128. editar.add_command(label="Deshacer", accelerator="Ctrl+Z", command=deshacer)
  129. editar.add_command(label="Rehacer", accelerator="Ctrl+Y",command=rehacer)
  130. editar.add_command(label="Cortar", accelerator="Ctrl+X",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. editar.add_command(label="Pegar", accelerator="Ctrl+V", command=pegar)
  134. menubar.add_cascade(label="Editar", menu=editar)
  135.  
  136. # añado la barra de menu a la ventana
  137. root.config(menu=menubar, width=800, height=400)
  138.  
  139. # configuro un scrollbar
  140. scroll = Scrollbar(root)
  141. scroll.pack(side=RIGHT, fill=Y)
  142.  
  143. # configuro la zona de escritura
  144. texto = Text(root)
  145. texto.pack(fill='both', expand=1)
  146. texto.config(
  147.             padx=6, pady=6, bd=0,
  148.             font=("arial",12),
  149.             bg="beige",
  150.             undo=True, 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, textvar=mensaje, justify="right")
  159. barra_inferior.pack(side="left")
  160.  
  161.  
  162. root.mainloop()
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement