Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- import os
- from tkinter import *
- from tkinter import filedialog as fd
- # creo la ventana
- root = Tk()
- root.geometry("600x500")
- root.title("Editor ACME")
- root.resizable(True,True)
- root.minsize(300,300)
- if os.path.exists("imagenes"):
- root.iconbitmap("imagenes/acme.ico")
- ####### funciones de la opcion 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 un archivo"
- )
- # si la ruta es válida, abro EN SOLO LECTURA
- if ruta:
- f = open(ruta)
- contenido = f.read()
- f.close()
- texto.delete(1.0, END)
- texto.insert("insert",contenido)
- 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')
- # abro en modo lecto-escritura
- # si el archivo existe lo abre y lo sobreescribe
- # Si no existe, crea uno nuevo para escritura y lectura
- f = open(ruta, "w+")
- f.write(contenido)
- f.close()
- mensaje.set("Archivo guardado")
- else:
- guardar_como()
- def guardar_como():
- global ruta
- mensaje.set("Guardar archivo como")
- f = fd.asksaveasfile(
- filetypes = (
- ("Archivos de texto", "*.txt"),
- ("Archivos de Python", "*.py"),
- ("Todos los archivos", "*.*"),
- ),
- title = "Guardar archivo",
- mode = "w",
- defaultextension = ".txt"
- )
- if f:
- ruta = f.name
- contenido = texto.get(1.0, 'end-1c')
- f = open(ruta, "w")
- f.write(contenido)
- f.close()
- mensaje.set("Archivo guardado...")
- else:
- mensaje.set("No se ha guardado el archivo")
- ruta = ""
- ####### funciones de la opcion editar
- def rehacer():
- texto.event_generate("<<Redo>>")
- def deshacer():
- texto.event_generate("<<Undo>>")
- def cortar():
- texto.event_generate("<<Cut>>")
- def copiar():
- texto.event_generate("<<Copy>>")
- def pegar():
- texto.event_generate("<<Paste>>")
- ###### EDITOR #########################
- # creo la ruta global de los archivos
- ruta = ""
- # creo la barra de menu
- menubar = Menu(root)
- # creo el submenu 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)
- # creo el submenu editar
- undo = PhotoImage(file="imagenes/undo.png")
- redo = PhotoImage(file="imagenes/redo.png")
- cut = PhotoImage(file="imagenes/cut.png")
- paste = PhotoImage(file="imagenes/paste.png")
- editar = Menu(menubar, tearoff=0)
- if not os.path.exists("imagenes"):
- editar.add_command(label="Deshacer", accelerator="Ctrl+Z", command=deshacer)
- else:
- editar.add_command(label="Deshacer", accelerator="Ctrl+Z", compound="left", image=undo, command=deshacer)
- if not os.path.exists("imagenes"):
- editar.add_command(label="Rehacer", accelerator="Ctrl+Y", command=rehacer)
- else:
- editar.add_command(label="Reshacer", accelerator="Ctrl+Y", compound="left", image=redo, command=deshacer)
- if not os.path.exists("imagenes"):
- editar.add_command(label="Cortar", accelerator="Ctrl+X", command=cortar)
- else:
- editar.add_command(label="Cortar", accelerator="Ctrl+X", compound="left", image=cut, command=deshacer)
- if not os.path.exists("imagenes"):
- editar.add_command(label="Pegar", accelerator="Ctrl+V", command=pegar)
- else:
- editar.add_command(label="Pegar", accelerator="Ctrl+V", compound="left", image=paste, command=deshacer)
- editar.add_command(label="Copiar", accelerator="Ctrl+C", command=copiar)
- menubar.add_cascade(label="Editar", menu=editar)
- # añado la barra a la ventana
- root.config(menu=menubar)
- # creo el scrollbar
- scroll = Scrollbar(root)
- scroll.pack(side=RIGHT, fill=Y)
- # zona de texto del editor
- texto = Text(root)
- texto.pack(fill='both', expand=1)
- texto.config(
- padx=6, pady=6, bd=0,
- font=("arial",12),
- bg="navajo white",
- 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