Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- # editor de texto
- from tkinter import *
- from tkinter import filedialog as fd
- root = Tk()
- root.title("Editor ACME")
- ########### funciones menu archivo ###################
- def nuevo():
- global ruta
- mensaje.set("Nuevo archivo")
- texto.delete(1.0, END)
- root.title("Editor ACME")
- ruta = ""
- def abrir():
- global ruta
- mensaje.set("Abrir archivo")
- ruta = fd.askopenfilename(
- initialdir = ".",
- filetypes = (
- ("Archivos de texto", "*.txt"),
- ("Archivos de Python", "*.py"),
- ("Todos los archivos", "*.*"),
- ),
- title = "Abrir archivo"
- )
- # si la ruta es válida, ABRIMOS EN MODO SOLO LECTURA
- if ruta != "":
- f = open(ruta)
- contenido = f.read()
- texto.delete(1.0, END)
- texto.insert('insert', contenido)
- f.close()
- root.title(f"{ruta} - Editor ACME")
- mensaje.set("Archivo abierto")
- def guardar():
- global ruta
- mensaje.set("Guardar archivo")
- if ruta != "":
- contenido = texto.get(1.0, 'end-1c')
- # el modo w+ abre para lectura y escritura. Si el archivo
- # existe lo sobreescribe. Si no existe, lo crea para escritura
- # y lectura
- f = open(ruta, "w+")
- f.write(contenido)
- f.close()
- mensaje.set("Archivo guardado correctamente")
- else:
- guardar_como()
- def guardar_como():
- global ruta
- mensaje.set("Guardar archivo como")
- f = fd.asksaveasfile(
- initialdir = ".",
- filetypes = (
- ("Archivos de texto", "*.txt"),
- ("Archivos de Python", "*.py"),
- ("Todos los archivos", "*.*"),
- ),
- title = "Guardar archivo",
- mode = "w",
- defaultextension = '.txt'
- )
- if f is not None:
- ruta = f.name
- contenido = texto.get(1.0, 'end-1c')
- f = open(ruta, "w+")
- f.write(contenido)
- f.close()
- mensaje.set("Archivo guardado correctamente")
- else:
- mensaje.set("Se canceló la operación de 'Guardar como'")
- ruta = ""
- ####### funciones del menu editar #################
- def undo():
- texto.event_generate("<<Undo>>")
- def redo():
- texto.event_generate("<<Redo>>")
- def cortar():
- texto.event_generate("<<Cut>>")
- def copiar():
- texto.event_generate("<<Copy>>")
- def pegar():
- texto.event_generate("<<Paste>>")
- # variable que almacena la ruta de los archivos
- ruta = ""
- # creo la barra de menu
- menubar = Menu(root)
- # menu archivo
- archivo = Menu(menubar, tearoff=0)
- archivo.add_command(label="Nuevo", command=nuevo)
- archivo.add_command(label="Abrir", command=abrir)
- archivo.add_command(label="Guardar", command=guardar)
- archivo.add_command(label="Guardar como", command=guardar_como)
- archivo.add_separator()
- archivo.add_command(label="Salir", command=root.quit)
- menubar.add_cascade(label="Archivo", menu=archivo)
- # menu editar
- undo_image = PhotoImage(file="undo.png")
- redo_image = PhotoImage(file="redo.png")
- cut_image = PhotoImage(file="cut.png")
- paste_image = PhotoImage(file="paste.png")
- editar = Menu(menubar, tearoff=0)
- editar.add_command(label="Deshacer", accelerator='Ctrl+Z', compound ='left', image=undo_image, command=undo)
- editar.add_command(label="Rehacer", accelerator='Shift+Ctrl+Z', compound ='left', image=redo_image, command=redo)
- editar.add_command(label="Cortar", accelerator='Ctrl+X', compound ='left', image=cut_image, command=cortar)
- editar.add_command(label="Copiar", accelerator='Ctrl+C', command=copiar)
- editar.add_command(label="Pegar", accelerator='Ctrl+V', compound ='left', image=paste_image, command=pegar)
- menubar.add_cascade(label="Editar", menu=editar)
- # agrego la barra a la ventana
- root.config(menu=menubar)
- # scrollbar
- scroll = Scrollbar(root)
- scroll.pack(side=RIGHT, fill=Y)
- # zona de escritura
- texto = Text(root)
- texto.pack(fill='both', expand=1)
- texto.config(
- padx=6, pady=6, bd=0,
- font=("arial", 12),
- bg="beige",
- undo=True, maxundo=20,
- yscrollcommand = scroll.set
- )
- scroll.config(command=texto.yview)
- # Barra de estado inferior
- mensaje = StringVar()
- mensaje.set("Bienvenido al editor ACME")
- barra_inferior = Label(root, textvar=mensaje, justify='right')
- barra_inferior.pack(side='left')
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment