Advertisement
teslariu

editor con clases

Sep 27th, 2023
970
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.57 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. from tkinter import messagebox as mb
  7.  
  8. class Editor:
  9.    
  10.     def __init__(self, root):
  11.         self.root = root
  12.         self.ruta = ""
  13.         self.mensaje = StringVar()
  14.         self.texto = Text(self.root)
  15.        
  16.         self.root.title("Editor ACME")
  17.         self.icono = PhotoImage(file="icono.png")
  18.         self.root.iconphoto(True,self.icono)
  19.  
  20.         # dimensiones y posicionamiento
  21.         root.geometry("600x500+400+300")
  22.         root.resizable(True, True)
  23.         root.minsize(300,300)
  24.        
  25.         # creo la barra de menu
  26.         menubar = Menu(root)
  27.        
  28.         # archivo
  29.         archivo = Menu(menubar, tearoff=0)
  30.         archivo.add_command(label="Nuevo", command=self.nuevo)
  31.         archivo.add_command(label="Abrir", command=self.abrir)
  32.         archivo.add_command(label="Guardar", command=self.guardar)
  33.         archivo.add_command(label="Guardar como", command=self.guardar_como)
  34.         archivo.add_separator()
  35.         archivo.add_command(label="Salir", command=root.quit)
  36.         menubar.add_cascade(label="Archivo", menu=archivo)
  37.  
  38.  
  39.         # imagenes del menu editar
  40.         undo = PhotoImage(file="undo.png")
  41.         redo = PhotoImage(file="redo.png")
  42.  
  43.         # editar
  44.         editar = Menu(menubar, tearoff=0)
  45.         editar.add_command(label="Deshacer", accelerator="Ctrl+Z", compound="left", image=undo, command=self.deshacer)
  46.         editar.add_command(label="Rehacer", accelerator="Ctrl+Y", compound="left", image=redo, command=self.rehacer)
  47.         editar.add_command(label="Cortar",accelerator="Ctrl+X", command=self.cortar)
  48.         editar.add_command(label="Copiar",accelerator="Ctrl+C", command=self.copiar)
  49.         editar.add_command(label="Pegar",accelerator="Ctrl+V", command=self.pegar)
  50.         menubar.add_cascade(label="Editar", menu=editar)
  51.  
  52.  
  53.         # añado la barra a la ventana
  54.         self.root.config(menu=menubar)
  55.  
  56.         # scrollbar
  57.         scroll = Scrollbar(root)
  58.         scroll.pack(side=RIGHT, fill=Y)
  59.  
  60.         # zona de texto del editor
  61.         self.texto = Text(root)
  62.         self.texto.config(
  63.             padx=6, pady=6, bd=0,
  64.             font=('arial',13),
  65.             bg='beige',
  66.             undo=True,
  67.             maxundo=20,
  68.             yscrollcommand=scroll.set
  69.         )
  70.         self.texto.pack(fill='both', expand=True)
  71.         scroll.config(command=self.texto.yview)
  72.  
  73.         # barra de estado inferior
  74.         self.mensaje.set("Bienvenido al Editor ACME")
  75.         barra = Label(root, textvar=self.mensaje, justify='right')
  76.         barra.pack(side='left')
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.     def nuevo(self):
  84.         """Crea un archivo nuevo"""
  85.         if self.texto:
  86.             opcion = mb.askokcancel(title="Advertencia", message="Perderá los cambios no guardados")
  87.         if not opcion:
  88.             return
  89.         self.mensaje.set("Nuevo archivo")
  90.         self.texto.delete(1.0, END)
  91.         self.root.title("Editor ACME")
  92.         self.ruta = ""
  93.        
  94.  
  95.     def abrir(self):
  96.         self.mensaje.set("Abrir archivo")
  97.         self.ruta = fd.askopenfilename(
  98.                 initialdir = ".",
  99.                 filetypes = (
  100.                     ("Archivos de texto", "*.txt"),
  101.                     ("Archivos de Python", "*.py"),
  102.                     ("Todos los archivos", "*.*"),
  103.                 ),
  104.                 title='Abrir un archivo'
  105.         )
  106.         # si la ruta es válida, abro el archivo EN MODO LECTURA
  107.         if self.ruta:
  108.             f = open(self.ruta)
  109.             contenido = f.read()
  110.             f.close()
  111.             self.texto.delete(1.0, END)
  112.             self.texto.insert('insert', contenido)
  113.             self.root.title(f"{self.ruta} - Editor ACME")
  114.             self.mensaje.set("Archivo abierto")
  115.    
  116.    
  117.     def guardar(self):
  118.         self.mensaje.set("Guardar archivo")
  119.         if self.ruta:
  120.             contenido = self.texto.get(1.0, 'end-1c')
  121.             f = open(self.ruta, "w+")
  122.             f.write(contenido)
  123.             f.close()
  124.             self.mensaje.set("Archivo guardado correctamente")
  125.        
  126.         else:
  127.             self.guardar_como()
  128.    
  129.    
  130.     def guardar_como(self):
  131.         self.mensaje.set("Guardar archivo como")
  132.         f = fd.asksaveasfile(
  133.                 filetypes = (
  134.                     ("Archivos de texto", "*.txt"),
  135.                     ("Archivos de Python", "*.py"),
  136.                     ("Todos los archivos", "*.*"),
  137.                 ),
  138.                 title = "Guardar archivo como",
  139.                 mode = "w",
  140.                 defaultextension = '.txt'
  141.         )
  142.         if f:
  143.             self.ruta = f.name
  144.             contenido = self.texto.get(1.0, 'end-1c')
  145.             f = open(self.ruta, "w+")
  146.             f.write(contenido)
  147.             f.close()
  148.        
  149.             self.mensaje.set("Archivo guardado correctamente")
  150.         else:
  151.             self.mensaje.set("Se ha cancelado el guardado del archivo")
  152.             self.ruta = ""     
  153.    
  154.    
  155.     def deshacer(self):
  156.         self.texto.event_generate("<<Undo>>")
  157.  
  158.     def rehacer(self):
  159.         self.texto.event_generate("<<Redo>>")
  160.    
  161.     def cortar(self):
  162.         self.texto.event_generate("<<Cut>>")
  163.    
  164.     def copiar(self):
  165.         self.texto.event_generate("<<Copy>>")
  166.    
  167.     def pegar(self):
  168.         self.texto.event_generate("<<Paste>>")
  169.  
  170.  
  171. if __name__ == "__main__":
  172.     root = Tk()
  173.     editor = Editor(root)
  174.     root.mainloop
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207. root.mainloop()
  208.  
Advertisement
Comments
  • TheTimeGuardian
    224 days
    # text 1.68 KB | 0 0
    1. Atlantius — Today at 8:21 PM
    2. I just realized Alicia Straka is pre-empting attacks and forcing attacks on her from me because she believes I'm going to attack her in the future, likely. So as to my note 171: the reason why this occured is because she's attempting to exhaust me as much as she possibly can by caching me into programmatic hate for her so that there's very little left when she goes live with my functionality at some point because it crosses her ethical boundaries or the games rules. Essentially she is forcing me (like she and her coworkers did in the past) to attack her. She is affiliated with Anthony Howe, and Derek Coppola, and several other people. I am certain without a shadow of a doubt that 171 was correct, and that this note is the rationale behind 171 (the previous note). 9.27.23 8:20PM EST JGR
    3. [8:22 PM]
    4. These folks are continuing trying to get me to step across the isle and shake their hand (last night) by caching me into nicety, then the following day as described by 171 and 172, look to start war with me. As I thought, they arent looking for me to succeed and they're just playing me at this point through deceptive means. 9.27.23 8:22pm EST JGR
    5. [8:26 PM]
    6. Elon musk has been mining for dirt for quite some time, I think he's collecting hate fueled speech's by me and rhetoric after caching me into my worst angry versions, then he either is leaking the content to his fans or my fans or the collective or waiting to leak it all at once without context (In the SAME exact way I keep stating how Elon and co are censoring the information and i can notice when i censor the information which contextualizes my anger and the process that they are doing to me). 9.27.23 8:26pm EST JGR
Add Comment
Please, Sign In to add comment
Advertisement