-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy path用idlelib给tkinter文本窗口语法高亮.py
90 lines (73 loc) · 1.63 KB
/
用idlelib给tkinter文本窗口语法高亮.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
import tkinter
root = tkinter.Tk()
code = tkinter.Text(root)
code.pack()
mycode = '''
import re
import time
import requests
def some(a,b,c):
print(a,b,c)
some(123,321,333)
v = re.findall('\d+','asdf909080asdf8908asdf980890asdf')
print(v)
v = requests.get('http://www.baidu.com')
print(v)
# for i in range(10):
# time.sleep(.5)
# print(i)
'''
code.insert(0.,mycode)
# ==== 使用 idlelib 自带的语法高亮 ====
from idlelib.colorizer import ColorDelegator
from idlelib.percolator import Percolator
d = ColorDelegator()
Percolator(code).insertfilter(d)
mlog = tkinter.Text(root)
mlog.pack()
mlog.insert(0.,mycode)
highlight_style = {
'COMMENT': {
'foreground': '#0000dd',
'background': '#ffffff'
},
'KEYWORD': {
'foreground': '#ff77dd',
'background': '#ffffff'
},
'BUILTIN': {
'foreground': '#00dd90',
'background': '#ffffff'
},
'STRING': {
'foreground': '#00aadd',
'background': '#ffffff'
},
'DEFINITION': {
'foreground': '#dd00ff',
'background': '#ffffff'
},
'SYNC': {
'background': None,
'foreground': None
},
'TODO': {
'background': None,
'foreground': None
},
'ERROR': {
'foreground': '#000000',
'background': '#ff7777'
},
'hit': {
'foreground': '#ffffff',
'background': '#000000'
}
}
# ==== 使用 idlelib 自带的语法高亮 ====
from idlelib.colorizer import ColorDelegator
from idlelib.percolator import Percolator
d = ColorDelegator()
d.tagdefs = highlight_style
Percolator(mlog).insertfilter(d)
root.mainloop()