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" )
18+
19+ def abrir ():
20+ global ruta
21+ mensaje .set ("Open file" )
22+ ruta = FileDialog .askopenfilename (
23+ initialdir = '.' ,
24+ filetypes = (("Pyfoch" , "*.pfcf" ),("Text file" , "*.txt" ),),title = "Open a new file" )
25+
26+ if ruta != "" :
27+ fichero = open (ruta , 'r' )
28+ contenido = fichero .read ()
29+ texto .delete (1.0 ,'end' )
30+ texto .insert ('insert' , contenido )
31+ fichero .close ()
32+ root .title (ruta + " - Pyfoch" )
33+
34+ def guardar_como ():
35+ global ruta
36+ mensaje .set ("Save file as" )
37+
38+ fichero = FileDialog .asksaveasfile (title = "Save file" ,mode = "w" , defaultextension = ".pfcf" )
39+
40+ if fichero is not None :
41+ ruta = fichero .name
42+ contenido = texto .get (1.0 ,'end-1c' )
43+ fichero = open (ruta , 'w+' )
44+ fichero .write (contenido )
45+ fichero .close ()
46+ mensaje .set ("File saved succesfully" )
47+ else :
48+ mensaje .set ("Save canceled" )
49+ ruta = ""
50+
51+ def guardar ():
52+ mensaje .set ("Save file" )
53+ if ruta != "" :
54+ contenido = texto .get (1.0 ,'end-1c' )
55+ fichero = open (ruta , 'w+' )
56+ fichero .write (contenido )
57+ fichero .close ()
58+ mensaje .set ("File saved succesfully" )
59+ else :
60+ guardar_como ()
61+ #uwu
62+
63+ # Configuracion de la raiz
64+ root = Tk ()
65+ root .configure (bg = 'white' )
66+ root .title ("Pyfoch" )
67+
68+ # File Menu
69+ menubar = Menu (root )
70+ filemenu = Menu (menubar , tearoff = 0 )
71+ filemenu .add_command (label = "New" , command = nuevo )
72+ filemenu .add_command (label = "Open" , command = abrir )
73+ filemenu .add_command (label = "Save" , command = guardar )
74+ filemenu .add_command (label = "Save as" , command = guardar_como )
75+ filemenu .add_separator ()
76+
77+
78+ '''
79+ This code is mine:
80+ '''
81+ def getLines (adress : str ):
82+ h = open (adress ,"r" )
83+ lines = h .readlines ()
84+ h .close ()
85+ return lines
86+
87+ class Parser ():
88+ def __init__ (self ,name : str = "Parser" ):
89+ self .sep = [',' ]
90+ self .sec = ['|' ]
91+ self .ski = ["\" " ]
92+ self .vip = ["\\ " ]
93+ self .den = ["~" ]
94+ self .name = name
95+ def compare (self ,x : str ,arr ):
96+ k = False
97+ for i in arr :
98+ k = ( k or x == i )
99+ return k
100+ def separator (self ,x : str ):
101+ return self .compare (x ,self .sep )
102+ def section (self ,x : str ):
103+ return self .compare (x ,self .sec )
104+ def skip (self ,x : str ):
105+ return self .compare (x ,self .ski )
106+ def isVip (self ,x : str ):
107+ return self .compare (x ,self .vip )
108+ def isDeny (self ,x : str ):
109+ return self .compare (x ,self .den )
110+
111+ def pfcfread (name : str ,printYesOrNo : int = 1 ,returnText : int = 0 ):
112+ lines = getLines (name + ".pfcf" )
113+ T = ""
114+ t = ""
115+ code = ""
116+ codel = "" #code language
117+ m = 0
118+ codem = 0
119+ p = Parser ()
120+ lineCount = 0
121+ for k in lines :
122+ count = 0
123+ for i in range (0 ,len (k )):
124+ j = k [i ]
125+ if p .isDeny (j ):
126+ if m == 2 :
127+ m = 0
128+ else :
129+ m = 2
130+ elif p .isVip (j ):
131+ m = 1
132+ elif m == 2 : #Comment mode on
133+ pass
134+ elif m == 1 : #Vip mode on
135+ t += j
136+ m = 0
137+ elif j == "<" or j == ">" :
138+ codem += 1
139+ elif codem == 4 :
140+ try :
141+ codef (codel ,code )
142+ codem == 0
143+ codel = ""
144+ code = ""
145+ except :
146+ print ("Sintax error" )
147+ elif codem == 3 :
148+ pass
149+ elif codem == 2 :
150+ code += j
151+ elif codem == 1 : #Code mode on
152+ codel += j
153+ elif p .separator (j ):
154+ T += t + "\n "
155+ t = ""
156+ elif p .section (j ):
157+ T += "\n "
158+ elif p .skip (j ):
159+ pass
160+ elif j != "\n " :
161+ t += j
162+ count += 1
163+ lineCount += 1
164+ if printYesOrNo :
165+ print (T )
166+ if returnText :
167+ a = {T ,k }
168+ return a
169+ return T
170+
171+ def export ():
172+ global ruta
173+ mensaje .set ("Export file as" )
174+ if ".pfcf" in ruta :
175+ ruta2 = ruta [:- 5 ]
176+ #.rstrip('f')
177+ T = pfcfread (ruta2 ,0 )
178+
179+ fichero = FileDialog .asksaveasfile (title = "Save file" , mode = "w" , defaultextension = ".txt" )
180+
181+ if fichero is not None :
182+ ruta = fichero .name
183+ contenido = T
184+ fichero = open (ruta , 'w+' )
185+ fichero .write (contenido )
186+ fichero .close ()
187+ mensaje .set ("File saved succesfully" )
188+ else :
189+ mensaje .set ("Save canceled" )
190+ ruta = ""
191+
192+ #Tools Menu
193+ filemenu .add_command (label = "Export" , command = export )
194+ '''
195+ This code is based on the code from
196+ https://docs.hektorprofe.net/python/interfaces-graficas-con-tkinter/editor-de-texto
197+ '''
198+
199+ filemenu .add_command (label = "Exit" , command = root .quit )
200+ menubar .add_cascade (menu = filemenu , label = "File" )
201+
202+ # Caja de texto central
203+ texto = Text (root )
204+ texto .pack (fill = "both" , expand = 1 )
205+ texto .config (bd = 0 , padx = 6 , pady = 4 , font = ("Consolas" ,12 ))
206+
207+ # Monitor inferior
208+ mensaje = StringVar ()
209+ mensaje .set ("Welcome to Pyfoch" )
210+ monitor = Label (root , textvar = mensaje , justify = 'left' )
211+ monitor .pack (side = "left" )
212+
213+ root .config (menu = menubar )
214+ # Finalmente bucle de la aplicacion
215+ root .mainloop ()
0 commit comments