-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathconsoletext.py
More file actions
executable file
·54 lines (45 loc) · 1.81 KB
/
consoletext.py
File metadata and controls
executable file
·54 lines (45 loc) · 1.81 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/env python3
#
#--------------------------------------------------------
"""
consoletext --- extends tkinter class Text
with stdout and stderr redirection.
"""
#--------------------------------------------------------
# Written by Tim Edwards
# efabless, inc.
# September 11, 2016
# Version 0.1
#--------------------------------------------------------
import sys
import tkinter
class ConsoleText(tkinter.Text):
linelimit = 500
class IORedirector(object):
'''A general class for redirecting I/O to this Text widget.'''
def __init__(self,text_area):
self.text_area = text_area
class StdoutRedirector(IORedirector):
'''A class for redirecting stdout to this Text widget.'''
def write(self,str):
self.text_area.write(str,False)
class StderrRedirector(IORedirector):
'''A class for redirecting stderr to this Text widget.'''
def write(self,str):
self.text_area.write(str,True)
def __init__(self, master=None, cnf={}, **kw):
'''See the __init__ for Tkinter.Text.'''
tkinter.Text.__init__(self, master, cnf, **kw)
self.tag_configure('stdout',background='white',foreground='black')
self.tag_configure('stderr',background='white',foreground='red')
# None of these works! Cannot change selected text background!
self.config(selectbackground='blue', selectforeground='white')
self.tag_configure('sel',background='blue',foreground='white')
def write(self, val, is_stderr=False):
lines = int(self.index('end-1c').split('.')[0])
if lines > self.linelimit:
self.delete('1.0', str(lines - self.linelimit) + '.0')
self.insert('end',val,'stderr' if is_stderr else 'stdout')
self.see('end')
def limit(self, val):
self.linelimit = val