1
- import json
2
1
import os
3
2
import sys
4
3
import time
4
+ import json
5
5
import shutil
6
6
import ctypes
7
7
import pygments
8
8
import subprocess
9
+ import webbrowser
9
10
from tkinter import *
11
+ from tkinter .font import Font
12
+ from tklinenums import TkLineNumbers
10
13
from pygments .lexers import PythonLexer
11
14
from tkinter .scrolledtext import ScrolledText
15
+ from tkinter .filedialog import askopenfile , asksaveasfile
12
16
13
17
if getattr (sys , 'frozen' , False ):
14
18
application_path = sys ._MEIPASS
26
30
except :
27
31
pass
28
32
33
+ VERSION = "1.1.0"
34
+ VERSION_TEXT = "v1.1.0"
29
35
TEMP_FOLDER_PATH = os .path .join (application_path , ".codedumps" )
30
36
31
37
def br (master = root ):
32
38
Label (master , text = "" ).pack ()
33
39
40
+ def openFile (* _ ):
41
+ filetypes = [
42
+ ('Python Files' , '*.py' ),
43
+ ('Text Files' , '*.txt' )
44
+ ]
45
+ file = askopenfile ("r" , filetypes = filetypes )
46
+ if file :
47
+ CODE .delete (1.0 , END )
48
+ CODE .insert (INSERT , file .read ())
49
+ highlightCode ()
50
+ update_highlighter ()
51
+
52
+ def saveFile (* _ ):
53
+ filetypes = [
54
+ ('Python Files' , '.py' )
55
+ ]
56
+ file = asksaveasfile ("w" , filetypes = filetypes , defaultextension = ".py" )
57
+ if file :
58
+ file .write (CODE .get (1.0 , END ))
59
+ file .close ()
60
+
61
+ def newFile (* _ ):
62
+ subprocess .Popen ([sys .executable , f"{ application_path } /main.py" ], shell = True , stdin = None , stdout = None , stderr = None , close_fds = True )
63
+
34
64
def highlightCode (* _ ):
65
+ linenums .redraw ()
35
66
for tag in CODE .tag_names (index = None ):
36
67
if tag != "sel" :
37
68
CODE .tag_remove (tag , "1.0" , "end" )
@@ -51,28 +82,29 @@ def highlightCode(*_):
51
82
CODE .tag_add (str (token ), start , end )
52
83
start = end
53
84
54
- def generate_font_list (self , input_dict : dict ) -> list :
55
- font_dict = {"-family" : self .font_family , "-size" : self .font_size }
56
-
57
- for style_key , style_value in input_dict .items ():
58
- if style_key == "family" :
59
- font_dict ["-family" ] = style_value
60
- elif style_key == "size" :
61
- font_dict ["-size" ] = style_value
62
- elif style_key == "bold" :
63
- font_dict ["-weight" ] = "bold" if style_value else "normal"
64
- elif style_key == "italic" :
65
- font_dict ["-slant" ] = "italic" if style_value else "roman"
66
- elif style_key == "underline" :
67
- font_dict ["-underline" ] = style_value
68
- elif style_key == "strikethrough" :
69
- font_dict ["-overstrike" ] = style_value
70
-
71
- font_list = []
72
- for x , y in zip (font_dict .keys (), font_dict .values ()):
73
- font_list .extend ([x , y ])
74
-
75
- return font_list
85
+ def generate_font_list (input_dict : dict ) -> list :
86
+ _font = Font (font = ("Cooper" , 18 ))
87
+ font_dict = {"-family" : _font .actual ("family" ), "-size" : _font .actual ("size" )}
88
+
89
+ for style_key , style_value in input_dict .items ():
90
+ if style_key == "family" :
91
+ font_dict ["-family" ] = style_value
92
+ elif style_key == "size" :
93
+ font_dict ["-size" ] = style_value
94
+ elif style_key == "bold" :
95
+ font_dict ["-weight" ] = "bold" if style_value else "normal"
96
+ elif style_key == "italic" :
97
+ font_dict ["-slant" ] = "italic" if style_value else "roman"
98
+ elif style_key == "underline" :
99
+ font_dict ["-underline" ] = style_value
100
+ elif style_key == "strikethrough" :
101
+ font_dict ["-overstrike" ] = style_value
102
+
103
+ font_list = []
104
+ for x , y in zip (font_dict .keys (), font_dict .values ()):
105
+ font_list .extend ([x , y ])
106
+
107
+ return font_list
76
108
77
109
def update_highlighter (highlighter : str = "mariana" ) -> None :
78
110
highlight_file = highlighter
@@ -117,7 +149,62 @@ def paste(*_):
117
149
except :
118
150
pass
119
151
120
- CODE .event_generate ("<<TextPasted>>" )
152
+ return "break"
153
+
154
+ def copy (* _ ):
155
+ if CODE .tag_ranges ("sel" ):
156
+ code = CODE .get (CODE .index ("sel.first" ), CODE .index ("sel.last" ))
157
+ else :
158
+ code = CODE .get (1.0 , END )
159
+ root .clipboard_clear ()
160
+ root .clipboard_append (code )
161
+ root .update ()
162
+ return "break"
163
+
164
+ def cut (* _ ):
165
+ if CODE .tag_ranges ("sel" ):
166
+ sel_start = CODE .index ("sel.first" )
167
+ sel_end = CODE .index ("sel.last" )
168
+ code = CODE .get (sel_start , sel_end )
169
+ del_start , del_end = sel_start , sel_end
170
+ else :
171
+ code = CODE .get (1.0 , END )
172
+ del_start , del_end = 1.0 , END
173
+ root .clipboard_clear ()
174
+ root .clipboard_append (code )
175
+ root .update ()
176
+ CODE .delete (del_start , del_end )
177
+
178
+ highlightCode ()
179
+ update_highlighter ()
180
+
181
+ return "break"
182
+
183
+ def selectAll (* _ ):
184
+ CODE .tag_add (SEL , "1.0" , END )
185
+ CODE .mark_set (INSERT , "1.0" )
186
+ CODE .see (INSERT )
187
+
188
+ return "break"
189
+
190
+ def undo (* _ ):
191
+ try :
192
+ CODE .edit_undo ()
193
+ highlightCode ()
194
+ update_highlighter ()
195
+ except :
196
+ pass
197
+
198
+ return "break"
199
+
200
+ def redo (* _ ):
201
+ try :
202
+ CODE .edit_redo ()
203
+ highlightCode ()
204
+ update_highlighter ()
205
+ except :
206
+ pass
207
+
121
208
return "break"
122
209
123
210
def compile_nd_run ():
@@ -161,13 +248,39 @@ def compile_nd_run():
161
248
162
249
CODE_FRAME = Frame (APP_FRAME )
163
250
Label (CODE_FRAME , text = "Enter your python code below." , fg = "Black" , font = ("Cooper Black" , 20 ), justify = CENTER ).pack ()
164
- CODE = ScrolledText (CODE_FRAME , height = 9 , width = 45 , font = ("Cooper" , 18 ))
251
+
252
+ CODE_BLOCK_FRAME = Frame (CODE_FRAME )
253
+ CODE = ScrolledText (CODE_BLOCK_FRAME , undo = True , height = 9 , width = 45 , font = ("Cooper" , 18 ))
254
+
255
+ linenums = TkLineNumbers (CODE_BLOCK_FRAME , CODE , height = 9 , width = 2 , justify = CENTER )
256
+ linenums .pack (side = LEFT , fill = Y , expand = True )
257
+
165
258
CODE .pack ()
166
259
CODE .bind ("<KeyRelease>" , highlightCode , add = True )
260
+ CODE .bind ("<<Cut>>" , cut , add = True )
261
+ CODE .bind ("<<Copy>>" , copy , add = True )
167
262
CODE .bind ("<<Paste>>" , paste , add = True )
263
+ CODE .bind ("<Control-Key-a>" , selectAll , add = True )
264
+ CODE .bind ("<Control-Key-A>" , selectAll , add = True )
265
+ CODE .bind ("<Control-Key-s>" , saveFile , add = True )
266
+ CODE .bind ("<Control-Key-S>" , saveFile , add = True )
267
+ CODE .bind ("<Control-Key-n>" , newFile , add = True )
268
+ CODE .bind ("<Control-Key-N>" , newFile , add = True )
269
+ CODE .bind ("<Control-Key-o>" , openFile , add = True )
270
+ CODE .bind ("<Control-Key-O>" , openFile , add = True )
271
+ CODE .bind ("<Control-Key-z>" , undo , add = True )
272
+ CODE .bind ("<Control-Key-Z>" , undo , add = True )
273
+ CODE .bind ("<Control-Shift-Key-z>" , redo , add = True )
274
+ CODE .bind ("<Control-Shift-Key-Z>" , redo , add = True )
275
+ root .bind ("<Alt-Key-F4>" , lambda * _ : root .destroy ())
276
+ root .bind ("<Control-Key-w>" , lambda * _ : root .destroy ())
277
+ root .bind ("<Control-Key-W>" , lambda * _ : root .destroy ())
278
+
168
279
highlightCode ()
169
280
update_highlighter ()
281
+
170
282
CODE .focus_set ()
283
+ CODE_BLOCK_FRAME .pack ()
171
284
CODE_FRAME .pack ()
172
285
173
286
br (APP_FRAME )
@@ -176,7 +289,7 @@ def compile_nd_run():
176
289
177
290
INPUT_FRAME = Frame (INPUT_ND_OUTPUT_FRAME )
178
291
Label (INPUT_FRAME , text = "Input" , font = ("Cooper Black" , 20 )).pack ()
179
- INPUT = ScrolledText (INPUT_FRAME , height = 9 , width = 40 , font = ("Cooper" , 18 ))
292
+ INPUT = ScrolledText (INPUT_FRAME , undo = True , height = 9 , width = 40 , font = ("Cooper" , 18 ))
180
293
INPUT .pack ()
181
294
INPUT_FRAME .pack (side = LEFT , padx = 10 )
182
295
@@ -201,5 +314,37 @@ def compile_nd_run():
201
314
202
315
APP_FRAME .pack ()
203
316
317
+ menubar = Menu (root )
318
+
319
+ file = Menu (menubar , tearoff = 0 , font = ("Cooper" , 11 ))
320
+ menubar .add_cascade (label = "File" , menu = file )
321
+ file .add_command (label = f"New File{ ' ' * 4 } (Ctrl+N)" , command = newFile )
322
+ file .add_command (label = f"Open...{ ' ' * 6 } (Ctrl+O)" , command = openFile )
323
+ file .add_command (label = f"Save{ ' ' * 10 } (Ctrl+S)" , command = saveFile )
324
+ file .add_separator ()
325
+ file .add_command (label = f"Exit{ ' ' * 12 } (Ctrl+W)" , command = root .destroy )
326
+
327
+ edit = Menu (menubar , tearoff = 0 , font = ("Cooper" , 11 ))
328
+ menubar .add_cascade (label = "Edit" , menu = edit )
329
+ edit .add_command (label = f"Undo{ ' ' * 11 } (Ctrl+Z)" , command = undo )
330
+ edit .add_command (label = f"Redo{ ' ' * 11 } (Ctrl+Shift+Z)" , command = redo )
331
+ edit .add_separator ()
332
+ edit .add_command (label = f"Cut{ ' ' * 14 } (Ctrl+X)" , command = cut )
333
+ edit .add_command (label = f"Copy{ ' ' * 11 } (Ctrl+C)" , command = copy )
334
+ edit .add_command (label = f"Paste{ ' ' * 10 } (Ctrl+V)" , command = paste )
335
+ edit .add_command (label = f"Select All{ ' ' * 5 } (Ctrl+A)" , command = selectAll )
336
+
337
+ window_ = Menu (menubar , tearoff = 0 , font = ("Cooper" , 11 ))
338
+ menubar .add_cascade (label = "Window" , menu = window_ )
339
+ window_ .add_command (label = f"*Compiler { VERSION_TEXT } *" , command = None )
340
+
341
+ help_ = Menu (menubar , tearoff = 0 , font = ("Cooper" , 11 ))
342
+ menubar .add_cascade (label = 'Help' , menu = help_ )
343
+ help_ .add_command (label = 'Source Code' , command = lambda * _ : webbrowser .open ("https://github.com/MrHydroCoder/Python-Compiler" , new = 1 ))
344
+ help_ .add_separator ()
345
+ help_ .add_command (label = 'Developer' , command = lambda * _ : webbrowser .open ("https://github.com/MrHydroCoder" , new = 1 ))
346
+
347
+ root .config (menu = menubar )
348
+
204
349
Label (root , text = "Made with 💝 by @MrHydroCoder" , font = ("Cooper Black" , 25 )).pack (side = BOTTOM )
205
350
root .mainloop ()
0 commit comments