2
2
3
3
"""
4
4
from tkinter import Toplevel , Text , TclError ,\
5
- HORIZONTAL , VERTICAL , N , S , E , W
5
+ HORIZONTAL , VERTICAL , NS , EW , NSEW , NONE , WORD , SUNKEN
6
6
from tkinter .ttk import Frame , Scrollbar , Button
7
7
from tkinter .messagebox import showerror
8
8
9
+ from functools import update_wrapper
9
10
from idlelib .colorizer import color_config
10
11
11
12
12
- class AutoHiddenScrollbar (Scrollbar ):
13
+ class AutoHideScrollbar (Scrollbar ):
13
14
"""A scrollbar that is automatically hidden when not needed.
14
15
15
16
Only the grid geometry manager is supported.
@@ -28,52 +29,70 @@ def place(self, **kwargs):
28
29
raise TclError (f'{ self .__class__ .__name__ } does not support "place"' )
29
30
30
31
31
- class TextFrame (Frame ):
32
- "Display text with scrollbar. "
32
+ class ScrollableTextFrame (Frame ):
33
+ """ Display text with scrollbar(s)."" "
33
34
34
- def __init__ (self , parent , rawtext , wrap = 'word' ):
35
+ def __init__ (self , master , wrap = NONE , ** kwargs ):
35
36
"""Create a frame for Textview.
36
37
37
- parent - parent widget for this frame
38
- rawtext - text to display
38
+ master - master widget for this frame
39
+ wrap - type of text wrapping to use ('word', 'char' or 'none')
40
+
41
+ All parameters except for 'wrap' are passed to Frame.__init__().
42
+
43
+ The Text widget is accessible via the 'text' attribute.
44
+
45
+ Note: Changing the wrapping mode of the text widget after
46
+ instantiation is not supported.
39
47
"""
40
- super ().__init__ (parent )
41
- self ['relief' ] = 'sunken'
42
- self ['height' ] = 700
48
+ super ().__init__ (master , ** kwargs )
43
49
44
- self .text = text = Text (self , wrap = wrap , highlightthickness = 0 )
45
- color_config (text )
46
- text .grid (row = 0 , column = 0 , sticky = N + S + E + W )
50
+ text = self .text = Text (self , wrap = wrap )
51
+ text .grid (row = 0 , column = 0 , sticky = NSEW )
47
52
self .grid_rowconfigure (0 , weight = 1 )
48
53
self .grid_columnconfigure (0 , weight = 1 )
49
- text .insert (0.0 , rawtext )
50
- text ['state' ] = 'disabled'
51
- text .focus_set ()
52
54
53
55
# vertical scrollbar
54
- self .yscroll = yscroll = AutoHiddenScrollbar (self , orient = VERTICAL ,
55
- takefocus = False ,
56
- command = text .yview )
57
- text ['yscrollcommand' ] = yscroll .set
58
- yscroll .grid (row = 0 , column = 1 , sticky = N + S )
59
-
60
- if wrap == 'none' :
61
- # horizontal scrollbar
62
- self .xscroll = xscroll = AutoHiddenScrollbar (self , orient = HORIZONTAL ,
63
- takefocus = False ,
64
- command = text .xview )
65
- text ['xscrollcommand' ] = xscroll .set
66
- xscroll .grid (row = 1 , column = 0 , sticky = E + W )
56
+ self .yscroll = AutoHideScrollbar (self , orient = VERTICAL ,
57
+ takefocus = False ,
58
+ command = text .yview )
59
+ self .yscroll .grid (row = 0 , column = 1 , sticky = NS )
60
+ text ['yscrollcommand' ] = self .yscroll .set
61
+
62
+ # horizontal scrollbar - only when wrap is set to NONE
63
+ if wrap == NONE :
64
+ self .xscroll = AutoHideScrollbar (self , orient = HORIZONTAL ,
65
+ takefocus = False ,
66
+ command = text .xview )
67
+ self .xscroll .grid (row = 1 , column = 0 , sticky = EW )
68
+ text ['xscrollcommand' ] = self .xscroll .set
69
+ else :
70
+ self .xscroll = None
67
71
68
72
69
73
class ViewFrame (Frame ):
70
74
"Display TextFrame and Close button."
71
- def __init__ (self , parent , text , wrap = 'word' ):
75
+ def __init__ (self , parent , contents , wrap = 'word' ):
76
+ """Create a frame for viewing text with a "Close" button.
77
+
78
+ parent - parent widget for this frame
79
+ contents - text to display
80
+ wrap - type of text wrapping to use ('word', 'char' or 'none')
81
+
82
+ The Text widget is accessible via the 'text' attribute.
83
+ """
72
84
super ().__init__ (parent )
73
85
self .parent = parent
74
86
self .bind ('<Return>' , self .ok )
75
87
self .bind ('<Escape>' , self .ok )
76
- self .textframe = TextFrame (self , text , wrap = wrap )
88
+ self .textframe = ScrollableTextFrame (self , relief = SUNKEN , height = 700 )
89
+
90
+ text = self .text = self .textframe .text
91
+ text .insert ('1.0' , contents )
92
+ text .configure (wrap = wrap , highlightthickness = 0 , state = 'disabled' )
93
+ color_config (text )
94
+ text .focus_set ()
95
+
77
96
self .button_ok = button_ok = Button (
78
97
self , text = 'Close' , command = self .ok , takefocus = False )
79
98
self .textframe .pack (side = 'top' , expand = True , fill = 'both' )
@@ -87,7 +106,7 @@ def ok(self, event=None):
87
106
class ViewWindow (Toplevel ):
88
107
"A simple text viewer dialog for IDLE."
89
108
90
- def __init__ (self , parent , title , text , modal = True , wrap = 'word' ,
109
+ def __init__ (self , parent , title , contents , modal = True , wrap = WORD ,
91
110
* , _htest = False , _utest = False ):
92
111
"""Show the given text in a scrollable window with a 'close' button.
93
112
@@ -96,7 +115,7 @@ def __init__(self, parent, title, text, modal=True, wrap='word',
96
115
97
116
parent - parent of this dialog
98
117
title - string which is title of popup dialog
99
- text - text to display in dialog
118
+ contents - text to display in dialog
100
119
wrap - type of text wrapping to use ('word', 'char' or 'none')
101
120
_htest - bool; change box location when running htest.
102
121
_utest - bool; don't wait_window when running unittest.
@@ -109,7 +128,7 @@ def __init__(self, parent, title, text, modal=True, wrap='word',
109
128
self .geometry (f'=750x500+{ x } +{ y } ' )
110
129
111
130
self .title (title )
112
- self .viewframe = ViewFrame (self , text , wrap = wrap )
131
+ self .viewframe = ViewFrame (self , contents , wrap = wrap )
113
132
self .protocol ("WM_DELETE_WINDOW" , self .ok )
114
133
self .button_ok = button_ok = Button (self , text = 'Close' ,
115
134
command = self .ok , takefocus = False )
@@ -129,18 +148,18 @@ def ok(self, event=None):
129
148
self .destroy ()
130
149
131
150
132
- def view_text (parent , title , text , modal = True , wrap = 'word' , _utest = False ):
151
+ def view_text (parent , title , contents , modal = True , wrap = 'word' , _utest = False ):
133
152
"""Create text viewer for given text.
134
153
135
154
parent - parent of this dialog
136
155
title - string which is the title of popup dialog
137
- text - text to display in this dialog
156
+ contents - text to display in this dialog
138
157
wrap - type of text wrapping to use ('word', 'char' or 'none')
139
158
modal - controls if users can interact with other windows while this
140
159
dialog is displayed
141
160
_utest - bool; controls wait_window on unittest
142
161
"""
143
- return ViewWindow (parent , title , text , modal , wrap = wrap , _utest = _utest )
162
+ return ViewWindow (parent , title , contents , modal , wrap = wrap , _utest = _utest )
144
163
145
164
146
165
def view_file (parent , title , filename , encoding , modal = True , wrap = 'word' ,
0 commit comments