-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
python_code_editor.py
122 lines (92 loc) · 2.99 KB
/
python_code_editor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from tkinter import *
import ctypes
import re
import os
# Increas Dots Per inch so it looks sharper
ctypes.windll.shcore.SetProcessDpiAwareness(True)
# Setup Tkinter
root = Tk()
root.geometry('500x500')
# Execute the Programm
def execute(event=None):
# Write the Content to the Temporary File
with open('run.py', 'w', encoding='utf-8') as f:
f.write(editArea.get('1.0', END))
# Start the File in a new CMD Window
os.system('start cmd /K "python run.py"')
# Register Changes made to the Editor Content
def changes(event=None):
global previousText
# If actually no changes have been made stop / return the function
if editArea.get('1.0', END) == previousText:
return
# Remove all tags so they can be redrawn
for tag in editArea.tag_names():
editArea.tag_remove(tag, "1.0", "end")
# Add tags where the search_re function found the pattern
i = 0
for pattern, color in repl:
for start, end in search_re(pattern, editArea.get('1.0', END)):
editArea.tag_add(f'{i}', start, end)
editArea.tag_config(f'{i}', foreground=color)
i+=1
previousText = editArea.get('1.0', END)
def search_re(pattern, text, groupid=0):
matches = []
text = text.splitlines()
for i, line in enumerate(text):
for match in re.finditer(pattern, line):
matches.append(
(f"{i + 1}.{match.start()}", f"{i + 1}.{match.end()}")
)
return matches
def rgb(rgb):
return "#%02x%02x%02x" % rgb
previousText = ''
# Define colors for the variouse types of tokens
normal = rgb((234, 234, 234))
keywords = rgb((234, 95, 95))
comments = rgb((95, 234, 165))
string = rgb((234, 162, 95))
function = rgb((95, 211, 234))
background = rgb((42, 42, 42))
font = 'Consolas 15'
# Define a list of Regex Pattern that should be colored in a certain way
repl = [
['(^| )(False|None|True|and|as|assert|async|await|break|class|continue|def|del|elif|else|except|finally|for|from|global|if|import|in|is|lambda|nonlocal|not|or|pass|raise|return|try|while|with|yield)($| )', keywords],
['".*?"', string],
['\'.*?\'', string],
['#.*?$', comments],
]
# Make the Text Widget
# Add a hefty border width so we can achieve a little bit of padding
editArea = Text(
root,
background=background,
foreground=normal,
insertbackground=normal,
relief=FLAT,
borderwidth=30,
font=font
)
# Place the Edit Area with the pack method
editArea.pack(
fill=BOTH,
expand=1
)
# Insert some Standard Text into the Edit Area
editArea.insert('1.0', """from argparse import ArgumentParser
from random import shuffle, choice
import string
# Setting up the Argument Parser
parser = ArgumentParser(
prog='Password Generator.',
description='Generate any number of passwords with this tool.'
)
""")
# Bind the KeyRelase to the Changes Function
editArea.bind('<KeyRelease>', changes)
# Bind Control + R to the exec function
root.bind('<Control-r>', execute)
changes()
root.mainloop()