11"""Simple text browser for IDLE
22
33"""
4- from tkinter import *
4+ from tkinter import Toplevel , Frame , Button , Text
5+ from tkinter import DISABLED , SUNKEN , VERTICAL , WORD
6+ from tkinter import RIGHT , LEFT , TOP , BOTTOM , BOTH , X , Y
57from tkinter .ttk import Scrollbar
68from tkinter .messagebox import showerror
79
@@ -16,6 +18,9 @@ def __init__(self, parent, title, text, modal=True,
1618 If modal is left True, users cannot interact with other windows
1719 until the textview window is closed.
1820
21+ parent - parent of this dialog
22+ title - string which is title of popup dialog
23+ text - text to display in dialog
1924 _htest - bool; change box location when running htest.
2025 _utest - bool; don't wait_window when running unittest.
2126 """
@@ -29,51 +34,65 @@ def __init__(self, parent, title, text, modal=True,
2934 self .bg = '#ffffff'
3035 self .fg = '#000000'
3136
32- self .CreateWidgets ()
37+ self .create_widgets ()
3338 self .title (title )
34- self .protocol ("WM_DELETE_WINDOW" , self .Ok )
39+ self .protocol ("WM_DELETE_WINDOW" , self .ok )
3540 self .parent = parent
36- self .textView .focus_set ()
41+ self .text .focus_set ()
3742 # Bind keys for closing this dialog.
38- self .bind ('<Return>' ,self .Ok )
39- self .bind ('<Escape>' ,self .Ok )
40- self .textView .insert (0.0 , text )
41- self .textView .config (state = DISABLED )
43+ self .bind ('<Return>' , self .ok )
44+ self .bind ('<Escape>' , self .ok )
45+ self .text .insert (0.0 , text )
46+ self .text .config (state = DISABLED )
4247
4348 if modal :
4449 self .transient (parent )
4550 self .grab_set ()
4651 if not _utest :
4752 self .wait_window ()
4853
49- def CreateWidgets (self ):
54+ def create_widgets (self ):
5055 "Create Frame with Text (with vertical Scrollbar) and Button."
51- frameText = Frame (self , relief = SUNKEN , height = 700 )
52- frameButtons = Frame (self )
53- self .buttonOk = Button (frameButtons , text = 'Close' ,
54- command = self .Ok , takefocus = FALSE )
55- self .scrollbarView = Scrollbar (frameText , orient = VERTICAL ,
56- takefocus = FALSE )
57- self .textView = Text (frameText , wrap = WORD , highlightthickness = 0 ,
56+ frame = Frame (self , relief = SUNKEN , height = 700 )
57+ frame_buttons = Frame (self )
58+ self .button_ok = Button (frame_buttons , text = 'Close' ,
59+ command = self .ok , takefocus = False )
60+ self .scrollbar = Scrollbar (frame , orient = VERTICAL , takefocus = False )
61+ self .text = Text (frame , wrap = WORD , highlightthickness = 0 ,
5862 fg = self .fg , bg = self .bg )
59- self .scrollbarView .config (command = self .textView .yview )
60- self .textView .config (yscrollcommand = self .scrollbarView .set )
61- self .buttonOk .pack ()
62- self .scrollbarView .pack (side = RIGHT ,fill = Y )
63- self .textView .pack (side = LEFT ,expand = TRUE ,fill = BOTH )
64- frameButtons .pack (side = BOTTOM ,fill = X )
65- frameText .pack (side = TOP ,expand = TRUE ,fill = BOTH )
66-
67- def Ok (self , event = None ):
63+ self .scrollbar .config (command = self .text .yview )
64+ self .text .config (yscrollcommand = self .scrollbar .set )
65+
66+ self .button_ok .pack ()
67+ self .scrollbar .pack (side = RIGHT , fill = Y )
68+ self .text .pack (side = LEFT , expand = True , fill = BOTH )
69+ frame_buttons .pack (side = BOTTOM , fill = X )
70+ frame .pack (side = TOP , expand = True , fill = BOTH )
71+
72+ def ok (self , event = None ):
73+ """Dismiss text viewer dialog."""
6874 self .destroy ()
6975
7076
7177def view_text (parent , title , text , modal = True , _utest = False ):
72- "Display text in a TextViewer."
78+ """Create TextViewer for given text.
79+
80+ parent - parent of this dialog
81+ title - string which is the title of popup dialog
82+ text - text to display in this dialog
83+ modal - controls if users can interact with other windows while this
84+ dialog is displayed
85+ _utest - bool; controls wait_window on unittest
86+ """
7387 return TextViewer (parent , title , text , modal , _utest = _utest )
7488
89+
7590def view_file (parent , title , filename , encoding = None , modal = True , _utest = False ):
76- "Display file in a TextViever or show error message."
91+ """Create TextViewer for text in filename.
92+
93+ Return error message if file cannot be read. Otherwise calls view_text
94+ with contents of the file.
95+ """
7796 try :
7897 with open (filename , 'r' , encoding = encoding ) as file :
7998 contents = file .read ()
0 commit comments