jpayne@69
|
1 """Simple text browser for IDLE
|
jpayne@69
|
2
|
jpayne@69
|
3 """
|
jpayne@69
|
4 from tkinter import Toplevel, Text, TclError,\
|
jpayne@69
|
5 HORIZONTAL, VERTICAL, NS, EW, NSEW, NONE, WORD, SUNKEN
|
jpayne@69
|
6 from tkinter.ttk import Frame, Scrollbar, Button
|
jpayne@69
|
7 from tkinter.messagebox import showerror
|
jpayne@69
|
8
|
jpayne@69
|
9 from functools import update_wrapper
|
jpayne@69
|
10 from idlelib.colorizer import color_config
|
jpayne@69
|
11
|
jpayne@69
|
12
|
jpayne@69
|
13 class AutoHideScrollbar(Scrollbar):
|
jpayne@69
|
14 """A scrollbar that is automatically hidden when not needed.
|
jpayne@69
|
15
|
jpayne@69
|
16 Only the grid geometry manager is supported.
|
jpayne@69
|
17 """
|
jpayne@69
|
18 def set(self, lo, hi):
|
jpayne@69
|
19 if float(lo) > 0.0 or float(hi) < 1.0:
|
jpayne@69
|
20 self.grid()
|
jpayne@69
|
21 else:
|
jpayne@69
|
22 self.grid_remove()
|
jpayne@69
|
23 super().set(lo, hi)
|
jpayne@69
|
24
|
jpayne@69
|
25 def pack(self, **kwargs):
|
jpayne@69
|
26 raise TclError(f'{self.__class__.__name__} does not support "pack"')
|
jpayne@69
|
27
|
jpayne@69
|
28 def place(self, **kwargs):
|
jpayne@69
|
29 raise TclError(f'{self.__class__.__name__} does not support "place"')
|
jpayne@69
|
30
|
jpayne@69
|
31
|
jpayne@69
|
32 class ScrollableTextFrame(Frame):
|
jpayne@69
|
33 """Display text with scrollbar(s)."""
|
jpayne@69
|
34
|
jpayne@69
|
35 def __init__(self, master, wrap=NONE, **kwargs):
|
jpayne@69
|
36 """Create a frame for Textview.
|
jpayne@69
|
37
|
jpayne@69
|
38 master - master widget for this frame
|
jpayne@69
|
39 wrap - type of text wrapping to use ('word', 'char' or 'none')
|
jpayne@69
|
40
|
jpayne@69
|
41 All parameters except for 'wrap' are passed to Frame.__init__().
|
jpayne@69
|
42
|
jpayne@69
|
43 The Text widget is accessible via the 'text' attribute.
|
jpayne@69
|
44
|
jpayne@69
|
45 Note: Changing the wrapping mode of the text widget after
|
jpayne@69
|
46 instantiation is not supported.
|
jpayne@69
|
47 """
|
jpayne@69
|
48 super().__init__(master, **kwargs)
|
jpayne@69
|
49
|
jpayne@69
|
50 text = self.text = Text(self, wrap=wrap)
|
jpayne@69
|
51 text.grid(row=0, column=0, sticky=NSEW)
|
jpayne@69
|
52 self.grid_rowconfigure(0, weight=1)
|
jpayne@69
|
53 self.grid_columnconfigure(0, weight=1)
|
jpayne@69
|
54
|
jpayne@69
|
55 # vertical scrollbar
|
jpayne@69
|
56 self.yscroll = AutoHideScrollbar(self, orient=VERTICAL,
|
jpayne@69
|
57 takefocus=False,
|
jpayne@69
|
58 command=text.yview)
|
jpayne@69
|
59 self.yscroll.grid(row=0, column=1, sticky=NS)
|
jpayne@69
|
60 text['yscrollcommand'] = self.yscroll.set
|
jpayne@69
|
61
|
jpayne@69
|
62 # horizontal scrollbar - only when wrap is set to NONE
|
jpayne@69
|
63 if wrap == NONE:
|
jpayne@69
|
64 self.xscroll = AutoHideScrollbar(self, orient=HORIZONTAL,
|
jpayne@69
|
65 takefocus=False,
|
jpayne@69
|
66 command=text.xview)
|
jpayne@69
|
67 self.xscroll.grid(row=1, column=0, sticky=EW)
|
jpayne@69
|
68 text['xscrollcommand'] = self.xscroll.set
|
jpayne@69
|
69 else:
|
jpayne@69
|
70 self.xscroll = None
|
jpayne@69
|
71
|
jpayne@69
|
72
|
jpayne@69
|
73 class ViewFrame(Frame):
|
jpayne@69
|
74 "Display TextFrame and Close button."
|
jpayne@69
|
75 def __init__(self, parent, contents, wrap='word'):
|
jpayne@69
|
76 """Create a frame for viewing text with a "Close" button.
|
jpayne@69
|
77
|
jpayne@69
|
78 parent - parent widget for this frame
|
jpayne@69
|
79 contents - text to display
|
jpayne@69
|
80 wrap - type of text wrapping to use ('word', 'char' or 'none')
|
jpayne@69
|
81
|
jpayne@69
|
82 The Text widget is accessible via the 'text' attribute.
|
jpayne@69
|
83 """
|
jpayne@69
|
84 super().__init__(parent)
|
jpayne@69
|
85 self.parent = parent
|
jpayne@69
|
86 self.bind('<Return>', self.ok)
|
jpayne@69
|
87 self.bind('<Escape>', self.ok)
|
jpayne@69
|
88 self.textframe = ScrollableTextFrame(self, relief=SUNKEN, height=700)
|
jpayne@69
|
89
|
jpayne@69
|
90 text = self.text = self.textframe.text
|
jpayne@69
|
91 text.insert('1.0', contents)
|
jpayne@69
|
92 text.configure(wrap=wrap, highlightthickness=0, state='disabled')
|
jpayne@69
|
93 color_config(text)
|
jpayne@69
|
94 text.focus_set()
|
jpayne@69
|
95
|
jpayne@69
|
96 self.button_ok = button_ok = Button(
|
jpayne@69
|
97 self, text='Close', command=self.ok, takefocus=False)
|
jpayne@69
|
98 self.textframe.pack(side='top', expand=True, fill='both')
|
jpayne@69
|
99 button_ok.pack(side='bottom')
|
jpayne@69
|
100
|
jpayne@69
|
101 def ok(self, event=None):
|
jpayne@69
|
102 """Dismiss text viewer dialog."""
|
jpayne@69
|
103 self.parent.destroy()
|
jpayne@69
|
104
|
jpayne@69
|
105
|
jpayne@69
|
106 class ViewWindow(Toplevel):
|
jpayne@69
|
107 "A simple text viewer dialog for IDLE."
|
jpayne@69
|
108
|
jpayne@69
|
109 def __init__(self, parent, title, contents, modal=True, wrap=WORD,
|
jpayne@69
|
110 *, _htest=False, _utest=False):
|
jpayne@69
|
111 """Show the given text in a scrollable window with a 'close' button.
|
jpayne@69
|
112
|
jpayne@69
|
113 If modal is left True, users cannot interact with other windows
|
jpayne@69
|
114 until the textview window is closed.
|
jpayne@69
|
115
|
jpayne@69
|
116 parent - parent of this dialog
|
jpayne@69
|
117 title - string which is title of popup dialog
|
jpayne@69
|
118 contents - text to display in dialog
|
jpayne@69
|
119 wrap - type of text wrapping to use ('word', 'char' or 'none')
|
jpayne@69
|
120 _htest - bool; change box location when running htest.
|
jpayne@69
|
121 _utest - bool; don't wait_window when running unittest.
|
jpayne@69
|
122 """
|
jpayne@69
|
123 super().__init__(parent)
|
jpayne@69
|
124 self['borderwidth'] = 5
|
jpayne@69
|
125 # Place dialog below parent if running htest.
|
jpayne@69
|
126 x = parent.winfo_rootx() + 10
|
jpayne@69
|
127 y = parent.winfo_rooty() + (10 if not _htest else 100)
|
jpayne@69
|
128 self.geometry(f'=750x500+{x}+{y}')
|
jpayne@69
|
129
|
jpayne@69
|
130 self.title(title)
|
jpayne@69
|
131 self.viewframe = ViewFrame(self, contents, wrap=wrap)
|
jpayne@69
|
132 self.protocol("WM_DELETE_WINDOW", self.ok)
|
jpayne@69
|
133 self.button_ok = button_ok = Button(self, text='Close',
|
jpayne@69
|
134 command=self.ok, takefocus=False)
|
jpayne@69
|
135 self.viewframe.pack(side='top', expand=True, fill='both')
|
jpayne@69
|
136
|
jpayne@69
|
137 self.is_modal = modal
|
jpayne@69
|
138 if self.is_modal:
|
jpayne@69
|
139 self.transient(parent)
|
jpayne@69
|
140 self.grab_set()
|
jpayne@69
|
141 if not _utest:
|
jpayne@69
|
142 self.wait_window()
|
jpayne@69
|
143
|
jpayne@69
|
144 def ok(self, event=None):
|
jpayne@69
|
145 """Dismiss text viewer dialog."""
|
jpayne@69
|
146 if self.is_modal:
|
jpayne@69
|
147 self.grab_release()
|
jpayne@69
|
148 self.destroy()
|
jpayne@69
|
149
|
jpayne@69
|
150
|
jpayne@69
|
151 def view_text(parent, title, contents, modal=True, wrap='word', _utest=False):
|
jpayne@69
|
152 """Create text viewer for given text.
|
jpayne@69
|
153
|
jpayne@69
|
154 parent - parent of this dialog
|
jpayne@69
|
155 title - string which is the title of popup dialog
|
jpayne@69
|
156 contents - text to display in this dialog
|
jpayne@69
|
157 wrap - type of text wrapping to use ('word', 'char' or 'none')
|
jpayne@69
|
158 modal - controls if users can interact with other windows while this
|
jpayne@69
|
159 dialog is displayed
|
jpayne@69
|
160 _utest - bool; controls wait_window on unittest
|
jpayne@69
|
161 """
|
jpayne@69
|
162 return ViewWindow(parent, title, contents, modal, wrap=wrap, _utest=_utest)
|
jpayne@69
|
163
|
jpayne@69
|
164
|
jpayne@69
|
165 def view_file(parent, title, filename, encoding, modal=True, wrap='word',
|
jpayne@69
|
166 _utest=False):
|
jpayne@69
|
167 """Create text viewer for text in filename.
|
jpayne@69
|
168
|
jpayne@69
|
169 Return error message if file cannot be read. Otherwise calls view_text
|
jpayne@69
|
170 with contents of the file.
|
jpayne@69
|
171 """
|
jpayne@69
|
172 try:
|
jpayne@69
|
173 with open(filename, 'r', encoding=encoding) as file:
|
jpayne@69
|
174 contents = file.read()
|
jpayne@69
|
175 except OSError:
|
jpayne@69
|
176 showerror(title='File Load Error',
|
jpayne@69
|
177 message=f'Unable to load file {filename!r} .',
|
jpayne@69
|
178 parent=parent)
|
jpayne@69
|
179 except UnicodeDecodeError as err:
|
jpayne@69
|
180 showerror(title='Unicode Decode Error',
|
jpayne@69
|
181 message=str(err),
|
jpayne@69
|
182 parent=parent)
|
jpayne@69
|
183 else:
|
jpayne@69
|
184 return view_text(parent, title, contents, modal, wrap=wrap,
|
jpayne@69
|
185 _utest=_utest)
|
jpayne@69
|
186 return None
|
jpayne@69
|
187
|
jpayne@69
|
188
|
jpayne@69
|
189 if __name__ == '__main__':
|
jpayne@69
|
190 from unittest import main
|
jpayne@69
|
191 main('idlelib.idle_test.test_textview', verbosity=2, exit=False)
|
jpayne@69
|
192
|
jpayne@69
|
193 from idlelib.idle_test.htest import run
|
jpayne@69
|
194 run(ViewWindow)
|