1
1
"""Simple text browser for IDLE
2
2
3
3
"""
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
5
7
from tkinter .ttk import Scrollbar
6
8
from tkinter .messagebox import showerror
7
9
@@ -16,6 +18,9 @@ def __init__(self, parent, title, text, modal=True,
16
18
If modal is left True, users cannot interact with other windows
17
19
until the textview window is closed.
18
20
21
+ parent - parent of this dialog
22
+ title - string which is title of popup dialog
23
+ text - text to display in dialog
19
24
_htest - bool; change box location when running htest.
20
25
_utest - bool; don't wait_window when running unittest.
21
26
"""
@@ -29,51 +34,65 @@ def __init__(self, parent, title, text, modal=True,
29
34
self .bg = '#ffffff'
30
35
self .fg = '#000000'
31
36
32
- self .CreateWidgets ()
37
+ self .create_widgets ()
33
38
self .title (title )
34
- self .protocol ("WM_DELETE_WINDOW" , self .Ok )
39
+ self .protocol ("WM_DELETE_WINDOW" , self .ok )
35
40
self .parent = parent
36
- self .textView .focus_set ()
41
+ self .text .focus_set ()
37
42
# 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 )
42
47
43
48
if modal :
44
49
self .transient (parent )
45
50
self .grab_set ()
46
51
if not _utest :
47
52
self .wait_window ()
48
53
49
- def CreateWidgets (self ):
54
+ def create_widgets (self ):
50
55
"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 ,
58
62
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."""
68
74
self .destroy ()
69
75
70
76
71
77
def 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
+ """
73
87
return TextViewer (parent , title , text , modal , _utest = _utest )
74
88
89
+
75
90
def 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
+ """
77
96
try :
78
97
with open (filename , 'r' , encoding = encoding ) as file :
79
98
contents = file .read ()
0 commit comments