Skip to content

Commit a9db5cb

Browse files
gh-151678: Add tests for tkinter.scrolledtext (GH-151753)
Add a test for the ScrolledText widget, which had no tests: that it is a Text widget held in a Frame with a Scrollbar, that Text methods work, that the geometry manager methods are redirected to the frame while configure is not, and that the scrollbar tracks the text view. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 66cc048 commit a9db5cb

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import unittest
2+
import tkinter
3+
from tkinter.scrolledtext import ScrolledText
4+
from test.support import requires
5+
from test.test_tkinter.support import setUpModule # noqa: F401
6+
from test.test_tkinter.support import AbstractTkTest
7+
8+
requires('gui')
9+
10+
11+
class ScrolledTextTest(AbstractTkTest, unittest.TestCase):
12+
13+
def create(self, **kwargs):
14+
st = ScrolledText(self.root, **kwargs)
15+
self.addCleanup(st.destroy)
16+
return st
17+
18+
def test_create(self):
19+
st = self.create(background='red', height=5)
20+
# It is a Text widget held in a Frame together with a Scrollbar.
21+
self.assertIsInstance(st, tkinter.Text)
22+
self.assertIsInstance(st.frame, tkinter.Frame)
23+
self.assertIsInstance(st.vbar, tkinter.Scrollbar)
24+
self.assertEqual(st.winfo_parent(), str(st.frame))
25+
# str() returns the frame, so that geometry managers manage it.
26+
self.assertEqual(str(st), str(st.frame))
27+
# Keyword options configure the Text.
28+
self.assertEqual(str(st['background']), 'red')
29+
self.assertEqual(st['height'], 5 if self.wantobjects else '5')
30+
31+
def test_text_methods(self):
32+
st = self.create()
33+
st.insert('1.0', 'hello\nworld')
34+
self.assertEqual(st.get('1.0', 'end-1c'), 'hello\nworld')
35+
self.assertEqual(st.index('end-1c'), '2.5')
36+
st.delete('1.0', 'end')
37+
self.assertEqual(st.get('1.0', 'end-1c'), '')
38+
39+
def test_geometry_methods(self):
40+
st = self.create()
41+
# configure is not redirected; it configures the Text.
42+
st.configure(height=8)
43+
self.assertEqual(st['height'], 8 if self.wantobjects else '8')
44+
# Pack, Grid and Place methods are redirected to the frame.
45+
st.pack()
46+
self.root.update()
47+
self.assertEqual(st.frame.winfo_manager(), 'pack')
48+
self.assertEqual(st.pack_info(), st.frame.pack_info())
49+
st.pack_forget()
50+
self.assertEqual(st.frame.winfo_manager(), '')
51+
52+
def test_scrollbar(self):
53+
st = self.create(height=5)
54+
st.pack()
55+
st.insert('1.0', '\n'.join(map(str, range(100))))
56+
self.root.update()
57+
# The scrollbar tracks the text view.
58+
self.assertEqual(st.vbar.get(), st.yview())
59+
st.yview_moveto(1.0)
60+
self.root.update()
61+
self.assertEqual(st.vbar.get()[1], 1.0)
62+
63+
64+
if __name__ == "__main__":
65+
unittest.main()

0 commit comments

Comments
 (0)