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