Skip to content

Commit 15be171

Browse files
committed
tkinter
1 parent 962e6db commit 15be171

File tree

4 files changed

+226
-8
lines changed

4 files changed

+226
-8
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python3
2+
3+
# date: 2020.06.05
4+
# https://stackoverflow.com/questions/62217254/python-tkinter-make-text-widget-automatically-scroll-with-text-used-with-redir/
5+
6+
import tkinter as tk
7+
import sys
8+
import datetime
9+
10+
# --- classes ---
11+
12+
class RedirectLabel(object):
13+
14+
def __init__(self, widget):
15+
"""Constructor"""
16+
self.output = widget
17+
18+
def write(self, string):
19+
"""Add text to the end"""
20+
self.output['text'] = self.output['text'] + string
21+
# label can't be scrolled
22+
23+
# some widgets may need it if they don't have it
24+
def flush(self):
25+
pass
26+
27+
# --- functions ---
28+
29+
def add_time():
30+
"""Add current time every 2 seconds"""
31+
print(datetime.datetime.now())
32+
root.after(2000, add_time)
33+
34+
# --- main ---
35+
36+
root = tk.Tk()
37+
38+
label = tk.Label(root)
39+
label.pack()
40+
41+
# label can't be scrolled (it would need Canvas but it more complex)
42+
#button_top = tk.Button(root, text="Move to TOP", command=lambda:label.see('1.0'))
43+
#button_top.pack()
44+
45+
# label can't be scrolled (it would need Canvas but it more complex)
46+
#button_end = tk.Button(root, text="Move to END", command=lambda:label.see('end'))
47+
#button_end.pack()
48+
49+
# keep original `stdout` and assing `RedirectText` as `stdout`
50+
old_stdout = sys.stdout
51+
sys.stdout = RedirectLabel(label)
52+
53+
# add some datetime at the beginning
54+
print('--- TOP ---')
55+
for _ in range(5):
56+
print(datetime.datetime.now())
57+
58+
# add new datetime every 2 seconds
59+
add_time()
60+
61+
# write to console when `print()` is redirected to `RedirectText()`
62+
old_stdout.write('Hello World!\n') # need '\n'
63+
print('Hello World!', file=old_stdout) # no need '\n'
64+
65+
root.mainloop()
66+
67+
# assign back original `stdout`
68+
sys.stdout = old_stdout
69+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python3
2+
3+
# date: 2020.06.05
4+
# https://stackoverflow.com/questions/62217254/python-tkinter-make-text-widget-automatically-scroll-with-text-used-with-redir/
5+
6+
import tkinter as tk
7+
import sys
8+
import datetime
9+
10+
# --- classes ---
11+
12+
class RedirectListbox(object):
13+
14+
def __init__(self, widget):
15+
"""Constructor"""
16+
self.output = widget
17+
18+
def write(self, string):
19+
"""Add text to the end and scroll to the end"""
20+
if string != '\n': # skip '\n' added by `print()`
21+
self.output.insert('end', string)
22+
self.output.see('end')
23+
24+
# some widgets may need it if they don't have it
25+
#def flush(self):
26+
# pass
27+
28+
# --- functions ---
29+
30+
def add_time():
31+
"""Add current time every 2 seconds"""
32+
print(datetime.datetime.now())
33+
root.after(2000, add_time)
34+
35+
# --- main ---
36+
37+
root = tk.Tk()
38+
39+
listbox = tk.Listbox(root, width=25) # width in chars (to show full datetime)
40+
listbox.pack(fill='x')
41+
42+
button_top = tk.Button(root, text="Move to TOP", command=lambda:listbox.see('0')) # Listbox needs only `"0"`, Text needs `"1.0"`
43+
button_top.pack()
44+
45+
button_end = tk.Button(root, text="Move to END", command=lambda:listbox.see('end'))
46+
button_end.pack()
47+
48+
# keep original `stdout` and assing `RedirectText` as `stdout`
49+
old_stdout = sys.stdout
50+
sys.stdout = RedirectListbox(listbox)
51+
52+
# add some datetime at the beginning
53+
print('--- TOP ---')
54+
for _ in range(50):
55+
print(datetime.datetime.now())
56+
57+
# add new datetime every 2 seconds
58+
add_time()
59+
60+
# write to console when `print()` is redirected to `RedirectText()`
61+
old_stdout.write('Hello World!\n') # need '\n'
62+
print('Hello World!', file=old_stdout) # no need '\n'
63+
64+
root.mainloop()
65+
66+
# assign back original `stdout`
67+
sys.stdout = old_stdout
68+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python3
2+
3+
# date: 2020.06.05
4+
# https://stackoverflow.com/questions/62217254/python-tkinter-make-text-widget-automatically-scroll-with-text-used-with-redir/
5+
6+
import tkinter as tk
7+
import sys
8+
import datetime
9+
10+
# --- classes ---
11+
12+
class RedirectText(object):
13+
14+
def __init__(self, widget):
15+
"""Constructor"""
16+
self.output = widget
17+
18+
def write(self, string):
19+
"""Add text to the end and scroll to the end"""
20+
self.output.insert('end', string)
21+
self.output.see('end')
22+
23+
# some widgets may need it if they don't have it
24+
#def flush(self):
25+
# pass
26+
27+
# --- functions ---
28+
29+
def add_time():
30+
"""Add current time every 2 seconds"""
31+
print(datetime.datetime.now())
32+
root.after(2000, add_time)
33+
34+
# --- main ---
35+
36+
root = tk.Tk()
37+
38+
text = tk.Text(root)
39+
text.pack()
40+
41+
button_top = tk.Button(root, text="Move to TOP", command=lambda:text.see('1.0'))
42+
button_top.pack()
43+
44+
button_end = tk.Button(root, text="Move to END", command=lambda:text.see('end'))
45+
button_end.pack()
46+
47+
# keep original `stdout` and assing `RedirectText` as `stdout`
48+
old_stdout = sys.stdout
49+
sys.stdout = RedirectText(text)
50+
51+
# add some datetime at the beginning
52+
print('--- TOP ---')
53+
for _ in range(50):
54+
print(datetime.datetime.now())
55+
56+
# add new datetime every 2 seconds
57+
add_time()
58+
59+
# write to console when `print()` is redirected to `RedirectText()`
60+
old_stdout.write('Hello World!\n') # need '\n'
61+
print('Hello World!', file=old_stdout) # no need '\n'
62+
63+
root.mainloop()
64+
65+
# assign back original `stdout`
66+
sys.stdout = old_stdout
67+
Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
1+
#!/usr/bin/env python3
2+
13
import tkinter as tk
24
import sys
35

6+
# date: 2019.10.22
7+
8+
# --- classes ---
9+
410
class Redirect():
11+
512
def __init__(self, output):
613
self.output = output
714

815
def write(self, text):
9-
self.output.insert('end', text + '\n')
16+
self.output.insert('end', text)
17+
#self.output.see('end')
1018

1119
def flush(self):
1220
pass
1321

22+
# --- main ---
1423

1524
root = tk.Tk()
16-
root.title('Output')
1725

1826
text_output = tk.Text(root)
19-
text_output.pack(fill='both', expand=True)
27+
text_output.pack()
2028

29+
# keep original `stdout` and assing class `Redirect`
2130
old_stdout = sys.stdout
22-
sys.stdout = Redirect(text_output)
31+
redirect = Redirect(text_output)
32+
#sys.stdout = redirect
2333

24-
print("Text")
34+
# write some text to new output
35+
for x in range(100):
36+
print("Line:", x, file=redirect)
2537

26-
button = tk.Button(root, text='OK', command=root.destroy)
27-
button.pack()
38+
# write to console
39+
#old_stdout.write('Hello World!\n')
40+
#print('Hello World!', file=old_stdout)
2841

2942
root.mainloop()
3043

44+
# assign back original `stdout`
3145
sys.stdout = old_stdout
32-
print('koniec')
46+

0 commit comments

Comments
 (0)