comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/idlelib/textview.py @ 69:33d812a61356

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