22
33"""
44from tkinter import Toplevel , Text , TclError ,\
5- HORIZONTAL , VERTICAL , N , S , E , W
5+ HORIZONTAL , VERTICAL , NS , EW , NSEW , NONE , WORD , SUNKEN
66from tkinter .ttk import Frame , Scrollbar , Button
77from tkinter .messagebox import showerror
88
9+ from functools import update_wrapper
910from idlelib .colorizer import color_config
1011
1112
12- class AutoHiddenScrollbar (Scrollbar ):
13+ class AutoHideScrollbar (Scrollbar ):
1314 """A scrollbar that is automatically hidden when not needed.
1415
1516 Only the grid geometry manager is supported.
@@ -28,52 +29,70 @@ def place(self, **kwargs):
2829 raise TclError (f'{ self .__class__ .__name__ } does not support "place"' )
2930
3031
31- class TextFrame (Frame ):
32- "Display text with scrollbar. "
32+ class ScrollableTextFrame (Frame ):
33+ """ Display text with scrollbar(s)."" "
3334
34- def __init__ (self , parent , rawtext , wrap = 'word' ):
35+ def __init__ (self , master , wrap = NONE , ** kwargs ):
3536 """Create a frame for Textview.
3637
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.
3947 """
40- super ().__init__ (parent )
41- self ['relief' ] = 'sunken'
42- self ['height' ] = 700
48+ super ().__init__ (master , ** kwargs )
4349
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 )
4752 self .grid_rowconfigure (0 , weight = 1 )
4853 self .grid_columnconfigure (0 , weight = 1 )
49- text .insert (0.0 , rawtext )
50- text ['state' ] = 'disabled'
51- text .focus_set ()
5254
5355 # 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
6771
6872
6973class ViewFrame (Frame ):
7074 "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+ """
7284 super ().__init__ (parent )
7385 self .parent = parent
7486 self .bind ('<Return>' , self .ok )
7587 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+
7796 self .button_ok = button_ok = Button (
7897 self , text = 'Close' , command = self .ok , takefocus = False )
7998 self .textframe .pack (side = 'top' , expand = True , fill = 'both' )
@@ -87,7 +106,7 @@ def ok(self, event=None):
87106class ViewWindow (Toplevel ):
88107 "A simple text viewer dialog for IDLE."
89108
90- def __init__ (self , parent , title , text , modal = True , wrap = 'word' ,
109+ def __init__ (self , parent , title , contents , modal = True , wrap = WORD ,
91110 * , _htest = False , _utest = False ):
92111 """Show the given text in a scrollable window with a 'close' button.
93112
@@ -96,7 +115,7 @@ def __init__(self, parent, title, text, modal=True, wrap='word',
96115
97116 parent - parent of this dialog
98117 title - string which is title of popup dialog
99- text - text to display in dialog
118+ contents - text to display in dialog
100119 wrap - type of text wrapping to use ('word', 'char' or 'none')
101120 _htest - bool; change box location when running htest.
102121 _utest - bool; don't wait_window when running unittest.
@@ -109,7 +128,7 @@ def __init__(self, parent, title, text, modal=True, wrap='word',
109128 self .geometry (f'=750x500+{ x } +{ y } ' )
110129
111130 self .title (title )
112- self .viewframe = ViewFrame (self , text , wrap = wrap )
131+ self .viewframe = ViewFrame (self , contents , wrap = wrap )
113132 self .protocol ("WM_DELETE_WINDOW" , self .ok )
114133 self .button_ok = button_ok = Button (self , text = 'Close' ,
115134 command = self .ok , takefocus = False )
@@ -129,18 +148,18 @@ def ok(self, event=None):
129148 self .destroy ()
130149
131150
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 ):
133152 """Create text viewer for given text.
134153
135154 parent - parent of this dialog
136155 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
138157 wrap - type of text wrapping to use ('word', 'char' or 'none')
139158 modal - controls if users can interact with other windows while this
140159 dialog is displayed
141160 _utest - bool; controls wait_window on unittest
142161 """
143- return ViewWindow (parent , title , text , modal , wrap = wrap , _utest = _utest )
162+ return ViewWindow (parent , title , contents , modal , wrap = wrap , _utest = _utest )
144163
145164
146165def view_file (parent , title , filename , encoding , modal = True , wrap = 'word' ,
0 commit comments