1+ '''
2+ This code is based on the code from
3+ https://docs.hektorprofe.net/python/interfaces-graficas-con-tkinter/editor-de-texto/
4+ '''
5+
6+ from tkinter import *
7+ from tkinter import filedialog as FileDialog
8+ from io import open
9+
10+ ruta = "" # La utilizaremos para almacenar la ruta del fichero
11+
12+ def nuevo ():
13+ global ruta
14+ mensaje .set ("New file" )
15+ ruta = ""
16+ texto .delete (1.0 , "end" )
17+ root .title ("Pyfoch Editor" )
18+
19+ def abrir ():
20+ global ruta
21+ mensaje .set ("Open file" )
22+ ruta = FileDialog .askopenfilename (
23+ initialdir = '.' ,
24+ filetypes = (("Pyfoch File" , "*.pfcf" ),("Text file" , "*.txt" ),),
25+ title = "Open a new file" )
26+
27+ if ruta != "" :
28+ fichero = open (ruta , 'r' )
29+ contenido = fichero .read ()
30+ texto .delete (1.0 ,'end' )
31+ texto .insert ('insert' , contenido )
32+ fichero .close ()
33+ root .title (ruta + " - Pyfoch Editor" )
34+
35+ def guardar ():
36+ mensaje .set ("Save file" )
37+ if ruta != "" :
38+ contenido = texto .get (1.0 ,'end-1c' )
39+ fichero = open (ruta , 'w+' )
40+ fichero .write (contenido )
41+ fichero .close ()
42+ mensaje .set ("File saved succesfully" )
43+ else :
44+ guardar_como ()
45+
46+ def guardar_como ():
47+ global ruta
48+ mensaje .set ("Save file as" )
49+
50+ fichero = FileDialog .asksaveasfile (title = "Save file" ,
51+ mode = "w" , defaultextension = ".pfcf" )
52+
53+ if fichero is not None :
54+ ruta = fichero .name
55+ contenido = texto .get (1.0 ,'end-1c' )
56+ fichero = open (ruta , 'w+' )
57+ fichero .write (contenido )
58+ fichero .close ()
59+ mensaje .set ("File saved succesfully" )
60+ else :
61+ mensaje .set ("Save canceled" )
62+ ruta = ""
63+
64+
65+ # Configuracion de la raiz
66+ root = Tk ()
67+ root .title ("Pyfoch Editor" )
68+
69+ # Menu superior
70+ menubar = Menu (root )
71+ filemenu = Menu (menubar , tearoff = 0 )
72+ filemenu .add_command (label = "New" , command = nuevo )
73+ filemenu .add_command (label = "Open" , command = abrir )
74+ filemenu .add_command (label = "save" , command = guardar )
75+ filemenu .add_command (label = "Save as" , command = guardar_como )
76+ filemenu .add_separator ()
77+ filemenu .add_command (label = "Exit" , command = root .quit )
78+ menubar .add_cascade (menu = filemenu , label = "File" )
79+
80+ # Caja de texto central
81+ texto = Text (root )
82+ texto .pack (fill = "both" , expand = 1 )
83+ texto .config (bd = 0 , padx = 6 , pady = 4 , font = ("Consolas" ,12 ))
84+
85+ # Monitor inferior
86+ mensaje = StringVar ()
87+ mensaje .set ("Welcome to Pyfoch Editor" )
88+ monitor = Label (root , textvar = mensaje , justify = 'left' )
89+ monitor .pack (side = "left" )
90+
91+ root .config (menu = menubar )
92+ # Finalmente bucle de la aplicacion
93+ root .mainloop ()
94+
95+ '''
96+ This code is mine:
97+ '''
0 commit comments