-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTkCodeView.py
More file actions
36 lines (32 loc) · 1.33 KB
/
CTkCodeView.py
File metadata and controls
36 lines (32 loc) · 1.33 KB
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
from customtkinter import CTkTextbox
from pygments.lexers import get_lexer_by_name
from pygments.styles import get_style_by_name, get_all_styles
class CTkCodeViewer(CTkTextbox):
def __init__(self, *args,
width: int = 100,
height: int = 32,
code=None,
allow_selecting=True,
language="python",
theme="monokai",
**kwargs):
super().__init__(*args, width=width, height=height, **kwargs)
self._monokai_style = get_style_by_name(theme)
self._style_parsed = self._monokai_style.list_styles()
for key in self._style_parsed:
if key[1]["color"] != "" and key[1]["color"] != None:
color = "#" + key[1]["color"]
self.tag_config(str(key[0]), foreground=color)
if code != None:
self._add_code(code, language)
if allow_selecting:
self.bind("<Key>", lambda e: "break")
else:
self.configure(state="disabled")
def _add_code(self, code, language):
lexer = get_lexer_by_name(language, stripall=True)
tokens = list(lexer.get_tokens(code))
for text in tokens:
self.insert("end", text[1], str(text[0]))
def allstyles(self):
return list(get_all_styles())