This repository has been archived by the owner on Jun 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
widgets.py
186 lines (135 loc) · 5.56 KB
/
widgets.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""
Author: rdbende
License: GNU GPLv3
Copyright: 2021 rdbende
"""
import tkinter as tk
from tkinter import ttk
from tkinter import font as tkfont
import webbrowser
import interpreter
import sys
try:
from pygments import lex
from pygments.lexers import BrainfuckLexer
pygments = True
except ImportError:
pygments = False
class Input(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.scrollbar = ttk.Scrollbar(self)
self.scrollbar.pack(side='right', fill='y')
self.text = tk.Text(self, relief="flat", highlightthickness=0, insertwidth=1, yscrollcommand=self.scrollbar.set)
self.text.pack(expand=True, fill='both')
self.scrollbar.config(command=self.text.yview)
sys.stdin = self
self.enter_pressed = False
def enter(*args):
self.enter_pressed = True
self.text.bind("<Return>", enter)
def write(self, content):
self.text.insert("end", content)
def readline(self):
self.enter_pressed = False
self.text.focus()
while not self.enter_pressed:
if interpreter.stop:
interpreter.running = False
interpreter.exit()
line = int(self.text.index("insert")[0]) - 1
return self.text.get("{}.0".format(line), "{}.end".format(line))
def clear(self):
self.text.delete("0.0", "end")
class Output(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.scrollbar = ttk.Scrollbar(self)
self.scrollbar.pack(side='right', fill='y')
self.text = tk.Text(self, state="disabled", relief="flat", highlightthickness=0, insertwidth=1, yscrollcommand=self.scrollbar.set)
self.text.pack(expand=True, fill='both')
self.scrollbar.config(command=self.text.yview)
sys.stdout = self
def disabler(func):
def wrapper(self, *args, **kwargs):
self.text.config(state="normal")
func(self, *args, **kwargs)
self.text.config(state="disabled")
return func
return wrapper
@disabler
def delete(self, *args, **kwargs):
self.text.delete(*args, **kwargs)
@disabler
def write(self, content):
self.text.insert("end", content)
class Editor(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.scrollbar = ttk.Scrollbar(self)
self.scrollbar.pack(side='right', fill='y')
self.text = tk.Text(self, relief="flat", highlightthickness=0, insertwidth=1, yscrollcommand=self.scrollbar.set)
self.text.pack(expand=True, fill='both')
self.font = tkfont.Font(family='monospace', size=10)
tab = self.font.measure(" ")
self.text.config(font=self.font, tabs=tab)
self.scrollbar.config(command=self.text.yview)
self.text.bind("<KeyRelease>", self.modified)
self.get = self.text.get
self.delete = self.text.delete
self.dirty = False
def insert(self, *args, **kwargs):
self.text.insert(*args, **kwargs)
self.highlight()
def modified(self, *args):
self.event_generate("<<Compare>>")
self.highlight()
def highlight_line(self, line=None):
start_range = 0
index = self.text.index("insert").split(".")
if line == None:
line = int(index[0])
line_text = self.text.get("{}.{}".format(line, 0), "{}.end".format(line))
for token, content in lex(line_text, BrainfuckLexer()):
end_range = start_range + len(content)
self.text.tag_add(str(token), '{}.{}'.format(line,start_range), '{}.{}'.format(line,end_range))
start_range = end_range
def highlight(self, *args):
if pygments:
code = self.text.get("1.0", "end-1c")
i = 1
for line in code.splitlines():
self.text.index("%d.0" %i)
self.highlight_line(line=i)
self.text.update()
i += 1
class LinkLabel(ttk.Label):
def __init__(self, *args, **kwargs):
self.url = kwargs.pop("url", "https://")
self.normalcolor = kwargs.pop("normalcolor", "#007fff")
self.hovercolor = kwargs.pop("hovercolor", "#005ddd")
ttk.Label.__init__(self, *args, foreground=self.normalcolor, **kwargs)
if self.tk.eval("tk windowingsystem") == "aqua":
cursor = kwargs.pop("cursor", "pointinghand")
else:
cursor = kwargs.pop("cursor", "hand2")
self.configure(cursor=cursor)
self.bind("<Button-1>", self._open)
self.bind("<Enter>", self.enter)
self.bind("<Leave>", self.leave)
def enter(self, *args):
self.configure(foreground=self.hovercolor)
def leave(self, *args):
self.configure(foreground=self.normalcolor)
def _open(self, *args):
self.leave()
webbrowser.open(self.url)
class PopupMenu(tk.Menu):
def __init__(self, master):
tk.Menu.__init__(self, master, tearoff=False, bd=0, relief="flat")
self.master.bind("<Button-3>", self.popup)
def popup(self, event):
try:
self.tk_popup(int(event.x_root + 2), int(event.y_root + 2))
finally:
self.grab_release()