1
+ import wx ,json
2
+ from os import path ,mkdir
3
+
4
+ class File :
5
+ def __init__ (self ,name = 'untitled.txt' ,dirname = '' ,isSaved = False ):
6
+ try :
7
+ mkdir ('./assets' )
8
+ except :
9
+ pass
10
+ try :
11
+ mkdir ('./temp' )
12
+ self .fileName = name
13
+ self .dirName = dirname
14
+ self .path = path .join (self .dirName ,self .fileName )
15
+ self .isSaved = isSaved
16
+ # @property
17
+ # def fileName(self):
18
+ # return self.__fileName
19
+ # @fileName.setter
20
+ # def fileName(self,x):
21
+ # self.__fileName = x
22
+
23
+ # @property
24
+ # def path(self):
25
+ # return self.__path
26
+ # @path.setter
27
+ # def path(self,x):
28
+ # self.__path = x
29
+
30
+
31
+ class MyWindow (wx .Frame ,File ):
32
+ def __init__ (self ,parent ,title ):
33
+ #initialising baseclass
34
+ wx .Frame .__init__ (self ,parent ,title = title ,size = (800 ,600 ))
35
+ File .__init__ (self )
36
+ #Creating Multiline Textbox
37
+ self .control = wx .TextCtrl (self ,style = wx .TE_MULTILINE )
38
+
39
+ self .title = title
40
+
41
+ #Creates StatusBar
42
+ self .CreateStatusBar ()
43
+
44
+ #Create Menu which holds menu items such as new,save,open etc.
45
+ filemenu = wx .Menu ()
46
+
47
+ #Creates New option
48
+ menuNew = filemenu .Append (wx .ID_NEW ,'&New' ,'Creates new file' )
49
+ #filemenu.AppendSeparator()
50
+
51
+ #Creates Open option
52
+ menuOpen = filemenu .Append (wx .ID_OPEN ,'&Open' ,'Opens your Previous work' )
53
+ #filemenu.AppendSeparator()
54
+
55
+ #Creates Save option
56
+ menuSave = filemenu .Append (wx .ID_SAVE ,'&Save' ,'Saves your current work in file' )
57
+ #filemenu.AppendSeparator()
58
+
59
+ #Create Saveas option
60
+ menuSaveas = filemenu .Append (wx .ID_SAVEAS ,'&Save as' ,'Save your work with differnt name or place' )
61
+
62
+ #Creates About option
63
+ menuAbout = filemenu .Append (wx .ID_ABOUT ,'&About' ,'Information about the program' )
64
+ #filemenu.AppendSeparator()
65
+
66
+ #creates Exit option
67
+ menuExit = filemenu .Append (wx .ID_EXIT ,'&Exit' ,"Terminate the program" )
68
+
69
+ #Genrates Menubar add our file menu in it
70
+ menuBar = wx .MenuBar ()
71
+ menuBar .Append (filemenu ,'&File' )
72
+ self .SetMenuBar (menuBar )
73
+
74
+ #Binding Events to all options
75
+ self .Bind (wx .EVT_MENU ,self .OnNew ,menuNew )
76
+ self .Bind (wx .EVT_MENU ,self .OnOpen ,menuOpen )
77
+ self .Bind (wx .EVT_MENU ,self .OnSave ,menuSave )
78
+ self .Bind (wx .EVT_MENU ,self .OnSaveas ,menuSaveas ),
79
+ self .Bind (wx .EVT_MENU , self .OnAbout ,menuAbout )
80
+ self .Bind (wx .EVT_MENU , self .OnExit ,menuExit )
81
+
82
+
83
+ self .Show (True )
84
+
85
+ def saveState (self ):
86
+ with open ('./state' ,'r' ) as f :
87
+ data = f .read ()
88
+ if data != '' :
89
+ data = json .loads (data + '}' )
90
+ if self .path in data :
91
+ data [self .path ]= {'fileName' :self .fileName ,'dirName' :self .dirName ,'isSaved' :self .isSaved ,'conFileName' :'temp.' + self .fileName }
92
+ string = json .dumps (data )
93
+ with open ('./state' ,'w' ) as f :
94
+ f .write (string [:- 1 ])
95
+ with open (('./temp/temp.' + self .fileName ),'w' ) as f :
96
+ f .write (self .control .GetValue ())
97
+ else :
98
+ string = json .dumps ({self .path :{'fileName' :self .fileName ,'dirName' :self .dirName ,'isSaved' :self .isSaved ,'conFileName' :('temp.' + self .fileName )}})
99
+ with open ('./state' ,'a' ) as f :
100
+ f .write (',' + string [1 :- 1 ])
101
+ with open (('./temp/temp.' + self .fileName [:- 4 ]),'w' ) as f :
102
+ f .write (self .control .GetValue ())
103
+ else :
104
+ string = json .dumps ({self .path :{'fileName' :self .fileName ,'dirName' :self .dirName ,'isSaved' :self .isSaved ,'conFileName' :'temp.' + self .fileName }})
105
+ with open ('./state' ,'a' ) as f :
106
+ f .write ('{"0":"0"' )
107
+ f .write (',' + string [1 :- 1 ])
108
+ with open (('./temp/temp.' + self .fileName ),'w' ) as f :
109
+ f .write (self .control .GetValue ())
110
+
111
+
112
+ def OnNew (self ,e ):
113
+ self .saveState ()
114
+ File .__init__ (self )
115
+ self .SetTitle (self .title )
116
+ self .control .SetValue ('' )
117
+
118
+ def OnOpen (self ,e ):
119
+ dlg = wx .FileDialog (self ,'Choose a file' ,'' ,'' ,"TEXT files (.txt)|*.txt" ,wx .FD_OPEN )
120
+ if dlg .ShowModal () == wx .ID_OK :
121
+ self .saveState ()
122
+ File .__init__ (self ,dlg .GetFilename (),dlg .GetDirectory (),True )
123
+ with open (self .path ,'r' ) as f :
124
+ s = f .read ()
125
+ self .control .SetValue (s )
126
+ self .SetTitle (' ' + self .fileName + ' - ' + self .title )
127
+ dlg .Destroy ()
128
+
129
+ def OnSave (self ,e ):
130
+ if self .isSaved :
131
+ self .SetTitle (' ' + self .fileName + ' - ' + self .title )
132
+ print (self .path )
133
+ with open (self .path ,'w' )as f :
134
+ f .write (self .control .GetValue ())
135
+ else :
136
+ dlg = wx .FileDialog (self ,'choose a directory' ,'' ,'' ,"TEXT files (*.txt)|*.txt" ,wx .FD_SAVEwx .FD_OVERWRITE_PROMPT )
137
+ if dlg .ShowModal () == wx .ID_OK :
138
+ self .saveState ()
139
+ File .__init__ (dlg .GetFilename (),dlg .GetDirectory (),True )
140
+ self .SetTitle (' ' + self .fileName + ' - ' + self .title )
141
+ with open (self .path ,'w' ) as f :
142
+ f .write (self .control .GetValue ())
143
+ dlg .Destroy ()
144
+
145
+ def OnSaveas (self ,e ):
146
+ dlg = wx .FileDialog (self ,'choose a directory' ,self .dirName ,self .fileName ,style = wx .FD_SAVE | wx .FD_OVERWRITE_PROMPT )
147
+ if dlg .ShowModal () == wx .ID_OK :
148
+ self .saveState ()
149
+ File .__init__ (self ,dlg .GetFilename (),dlg .GetDirectory (),True )
150
+ self .SetTitle (' ' + self .fileName + ' - ' + self .title )
151
+ with open (self .path ,'w' ) as f :
152
+ f .write (self .control .GetValue ())
153
+ dlg .Destroy ()
154
+
155
+ def OnAbout (self ,e ):
156
+ dlg = wx .MessageDialog (self ,'A Small TE' ,'About' ,wx .OK )
157
+ dlg .ShowModal ()
158
+ dlg .Destroy ()
159
+ def OnExit (self ,e ):
160
+ self .saveState ()
161
+ self .Close (True )
162
+ app = wx .App (False )
163
+ frame = MyWindow (None ,"SuperEditor v0.01" )
164
+ app .MainLoop ()
0 commit comments