-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_editor_and_text_54.py
86 lines (59 loc) · 2.15 KB
/
code_editor_and_text_54.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
from tkinter import *
import ctypes
import re
import os
def execute(event=None):
with open("run.py", "w", encoding="utf-8") as f:
f.write(editArea.get("1.0", END))
os.system('start cmd /K "python run.py"')
def changes(event=None):
global previousText
if editArea.get("1.0", END) == previousText:
return
for tag in editArea.tag_names():
editArea.tag_remove(tag, "1.0", "end")
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):
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
ctypes.windll.shcore.SetProcessDpiAwareness(True)
root = Tk()
root.geometry("900x700")
root.title("Rise-Coding (2010)")
previousText = ""
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 = "Arial 13 normal roman"
repl = [
["(^| )(False|None|True|and|as|assert|async|await|break|class|contunie|def|del|elif|else|except|finally|for|from|global|if|import|in|is|lambda|nonlocal|not|or|pass|raise|return|try|wheel|with|yeld)($|)",keywords],
['".*?"', string],
['\".*?\"', string],
['#.*?$', comments]
]
editArea = Text(
root, background=background, foreground=normal, insertbackground=normal, relief=FLAT, borderwidth=30, font=font
)
editArea.pack(fill=BOTH, expand=1)
editArea.insert("1.0", """from random import randint
print([randint(1, 20) for i in range(10)])
""")
editArea.bind('<KeyRelease>', changes)
root.bind('<Control-r>', execute)
changes()
root.mainloop()