Advertisement
Masterpeace

Editor de texto

Aug 14th, 2020 (edited)
2,151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from tkinter import*
  2. from tkinter import filedialog as FileDialog
  3. from io import open
  4.  
  5. ruta = " " #La utilizaremos para almacenar al ruta de un fichero
  6.  
  7. def nuevo():
  8.     global ruta
  9.     mensaje.set("Nuevo fichero")
  10.     ruta = ""
  11.     texto.delete(1.0, "end")
  12.  
  13. def abrir():
  14.     global ruta
  15.     mensaje.set("Abrir fichero")
  16.     ruta = FileDialog.askopenfilename(
  17.         initialdir= '.',
  18.         filetype=(("Ficheros de texto", "*.txt"),),
  19.         title="Abrir un fichero")
  20.  
  21.     if ruta != "":
  22.         fichero = open(ruta, 'r')
  23.         contenido = fichero. read()
  24.         texto.delete(1.0, 'end')
  25.         texto.insert('insert', contenido)
  26.         fichero.close()
  27.         root.title(ruta + " - Mi editor")
  28.  
  29.  
  30. def guardar():
  31.     global ruta
  32.     mensaje.set("Guardar fichero")
  33.     if ruta != " ":
  34.         contenido = texto.get(1.0, 'end-1c')
  35.         fichero = open(ruta, 'w+')
  36.         fichero.write(contenido)
  37.         fichero.close()
  38.         mensaje.set("Fcihero guardado correctamente")
  39.     else:
  40.         guardar_como()
  41.  
  42. def guardar_como():
  43.     global ruta
  44.     mensaje.set("Guardar fichero como")
  45.     fichero = FileDialog.asksaveasfile(title = "Guardar fichero", mode = "w", defaultextension = ".txt")
  46.     if fichero is not None:
  47.         ruta = fichero.name
  48.         contenido = texto.get(1.0, 'end-1c')
  49.         fichero = open(ruta, 'w+')
  50.         fichero.write(contenido)
  51.         fichero.close()
  52.         mensaje.set("Fcihero guardado correctamente")
  53.     else:
  54.         mensaje.set("Guardado Cancelado")
  55.         ruta = " "
  56.  
  57.  
  58. #Configuracion de la raiz
  59. root = Tk()
  60. root.title("Mi editor")
  61.  
  62. #Menu superior
  63. menubar = Menu(root)
  64. filemenu = Menu(menubar, tearoff=0)
  65. filemenu.add_command(label="Nuevo", command=nuevo)
  66. filemenu.add_command(label="Abrir", command=abrir)
  67. filemenu.add_command(label="Guardar", command=guardar)
  68. filemenu.add_command(label="Guardar como", command=guardar_como)
  69. filemenu.add_separator()
  70. filemenu.add_command(label="Salir", command=root.quit)
  71. menubar.add_cascade(menu=filemenu, label="Archivo")
  72.  
  73. # Caja de texto central
  74.  
  75. texto= Text(root)
  76. texto.pack(fill="both", expand=1)
  77. texto.config(bd=0, padx=6, pady=4, font=("Consolas", 12))
  78.  
  79.  
  80. #Monitor inferior
  81. mensaje = StringVar()
  82. mensaje.set("Bienvenido a tu editor")
  83. monitor = Label(root, textvar=mensaje, justify="left")
  84. monitor.pack(side="left")
  85.  
  86. root.config(menu=menubar)
  87. #Finalmente bucle de la aplicacion
  88. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement