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 | """ |
csabella | 42bc8be | 2017-06-29 18:42:17 -0400 | [diff] [blame] | 4 | from tkinter import Toplevel, Text |
| 5 | from tkinter.ttk import Frame, Scrollbar, Button |
Terry Jan Reedy | 82c4615 | 2016-06-22 04:54:18 -0400 | [diff] [blame] | 6 | from tkinter.messagebox import showerror |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 7 | |
Tal Einat | c87d9f4 | 2018-09-23 15:23:15 +0300 | [diff] [blame^] | 8 | from idlelib.colorizer import color_config |
| 9 | |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 10 | |
csabella | 42bc8be | 2017-06-29 18:42:17 -0400 | [diff] [blame] | 11 | class TextFrame(Frame): |
| 12 | "Display text with scrollbar." |
| 13 | |
| 14 | def __init__(self, parent, rawtext): |
| 15 | """Create a frame for Textview. |
| 16 | |
| 17 | parent - parent widget for this frame |
| 18 | rawtext - text to display |
| 19 | """ |
| 20 | super().__init__(parent) |
| 21 | self['relief'] = 'sunken' |
| 22 | self['height'] = 700 |
csabella | 42bc8be | 2017-06-29 18:42:17 -0400 | [diff] [blame] | 23 | |
Tal Einat | c87d9f4 | 2018-09-23 15:23:15 +0300 | [diff] [blame^] | 24 | self.text = text = Text(self, wrap='word', highlightthickness=0) |
| 25 | color_config(text) |
csabella | 42bc8be | 2017-06-29 18:42:17 -0400 | [diff] [blame] | 26 | self.scroll = scroll = Scrollbar(self, orient='vertical', |
| 27 | takefocus=False, command=text.yview) |
| 28 | text['yscrollcommand'] = scroll.set |
| 29 | text.insert(0.0, rawtext) |
| 30 | text['state'] = 'disabled' |
| 31 | text.focus_set() |
| 32 | |
| 33 | scroll.pack(side='right', fill='y') |
| 34 | text.pack(side='left', expand=True, fill='both') |
| 35 | |
| 36 | |
| 37 | class ViewFrame(Frame): |
| 38 | "Display TextFrame and Close button." |
| 39 | def __init__(self, parent, text): |
| 40 | super().__init__(parent) |
| 41 | self.parent = parent |
| 42 | self.bind('<Return>', self.ok) |
| 43 | self.bind('<Escape>', self.ok) |
| 44 | self.textframe = TextFrame(self, text) |
| 45 | self.button_ok = button_ok = Button( |
| 46 | self, text='Close', command=self.ok, takefocus=False) |
| 47 | self.textframe.pack(side='top', expand=True, fill='both') |
| 48 | button_ok.pack(side='bottom') |
| 49 | |
| 50 | def ok(self, event=None): |
| 51 | """Dismiss text viewer dialog.""" |
| 52 | self.parent.destroy() |
| 53 | |
| 54 | |
| 55 | class ViewWindow(Toplevel): |
Terry Jan Reedy | a3623c8 | 2016-08-31 19:45:39 -0400 | [diff] [blame] | 56 | "A simple text viewer dialog for IDLE." |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 57 | |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 58 | def __init__(self, parent, title, text, modal=True, |
Terry Jan Reedy | bfebfd8 | 2017-09-30 17:37:53 -0400 | [diff] [blame] | 59 | *, _htest=False, _utest=False): |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 60 | """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] | 61 | |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 62 | If modal is left True, users cannot interact with other windows |
| 63 | until the textview window is closed. |
Terry Jan Reedy | 537e2c8 | 2014-06-05 03:38:34 -0400 | [diff] [blame] | 64 | |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame] | 65 | parent - parent of this dialog |
| 66 | title - string which is title of popup dialog |
| 67 | text - text to display in dialog |
Terry Jan Reedy | 537e2c8 | 2014-06-05 03:38:34 -0400 | [diff] [blame] | 68 | _htest - bool; change box location when running htest. |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 69 | _utest - bool; don't wait_window when running unittest. |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 70 | """ |
csabella | 42bc8be | 2017-06-29 18:42:17 -0400 | [diff] [blame] | 71 | super().__init__(parent) |
| 72 | self['borderwidth'] = 5 |
Terry Jan Reedy | a3623c8 | 2016-08-31 19:45:39 -0400 | [diff] [blame] | 73 | # Place dialog below parent if running htest. |
csabella | 42bc8be | 2017-06-29 18:42:17 -0400 | [diff] [blame] | 74 | x = parent.winfo_rootx() + 10 |
| 75 | y = parent.winfo_rooty() + (10 if not _htest else 100) |
| 76 | self.geometry(f'=750x500+{x}+{y}') |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 77 | |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 78 | self.title(title) |
csabella | 42bc8be | 2017-06-29 18:42:17 -0400 | [diff] [blame] | 79 | self.viewframe = ViewFrame(self, text) |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame] | 80 | self.protocol("WM_DELETE_WINDOW", self.ok) |
csabella | 42bc8be | 2017-06-29 18:42:17 -0400 | [diff] [blame] | 81 | self.button_ok = button_ok = Button(self, text='Close', |
| 82 | command=self.ok, takefocus=False) |
| 83 | self.viewframe.pack(side='top', expand=True, fill='both') |
Terry Jan Reedy | e91e763 | 2012-02-05 15:14:20 -0500 | [diff] [blame] | 84 | |
Tal Einat | dd74369 | 2018-08-02 10:30:06 +0300 | [diff] [blame] | 85 | self.is_modal = modal |
| 86 | if self.is_modal: |
Terry Jan Reedy | e91e763 | 2012-02-05 15:14:20 -0500 | [diff] [blame] | 87 | self.transient(parent) |
| 88 | self.grab_set() |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 89 | if not _utest: |
| 90 | self.wait_window() |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 91 | |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame] | 92 | def ok(self, event=None): |
| 93 | """Dismiss text viewer dialog.""" |
Tal Einat | dd74369 | 2018-08-02 10:30:06 +0300 | [diff] [blame] | 94 | if self.is_modal: |
| 95 | self.grab_release() |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 96 | self.destroy() |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 98 | |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 99 | def view_text(parent, title, text, modal=True, _utest=False): |
csabella | 42bc8be | 2017-06-29 18:42:17 -0400 | [diff] [blame] | 100 | """Create text viewer for given text. |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame] | 101 | |
| 102 | parent - parent of this dialog |
| 103 | title - string which is the title of popup dialog |
| 104 | text - text to display in this dialog |
| 105 | modal - controls if users can interact with other windows while this |
| 106 | dialog is displayed |
| 107 | _utest - bool; controls wait_window on unittest |
| 108 | """ |
csabella | 42bc8be | 2017-06-29 18:42:17 -0400 | [diff] [blame] | 109 | return ViewWindow(parent, title, text, modal, _utest=_utest) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 110 | |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame] | 111 | |
Terry Jan Reedy | 688722c | 2018-02-12 17:42:41 -0500 | [diff] [blame] | 112 | def view_file(parent, title, filename, encoding, modal=True, _utest=False): |
csabella | 42bc8be | 2017-06-29 18:42:17 -0400 | [diff] [blame] | 113 | """Create text viewer for text in filename. |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame] | 114 | |
| 115 | Return error message if file cannot be read. Otherwise calls view_text |
| 116 | with contents of the file. |
| 117 | """ |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 118 | try: |
Éric Araujo | ccf03a1 | 2011-08-01 17:29:36 +0200 | [diff] [blame] | 119 | with open(filename, 'r', encoding=encoding) as file: |
| 120 | contents = file.read() |
Terry Jan Reedy | 82c4615 | 2016-06-22 04:54:18 -0400 | [diff] [blame] | 121 | except OSError: |
| 122 | showerror(title='File Load Error', |
csabella | 42bc8be | 2017-06-29 18:42:17 -0400 | [diff] [blame] | 123 | message=f'Unable to load file {filename!r} .', |
Terry Jan Reedy | 82c4615 | 2016-06-22 04:54:18 -0400 | [diff] [blame] | 124 | parent=parent) |
| 125 | except UnicodeDecodeError as err: |
| 126 | showerror(title='Unicode Decode Error', |
| 127 | message=str(err), |
| 128 | parent=parent) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 129 | else: |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 130 | return view_text(parent, title, contents, modal, _utest=_utest) |
terryjreedy | 295304d | 2017-05-17 22:59:46 -0400 | [diff] [blame] | 131 | return None |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 132 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 133 | |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 134 | if __name__ == '__main__': |
Terry Jan Reedy | 4d92158 | 2018-06-19 19:12:52 -0400 | [diff] [blame] | 135 | from unittest import main |
| 136 | main('idlelib.idle_test.test_textview', verbosity=2, exit=False) |
| 137 | |
Terry Jan Reedy | 1b392ff | 2014-05-24 18:48:18 -0400 | [diff] [blame] | 138 | from idlelib.idle_test.htest import run |
csabella | 42bc8be | 2017-06-29 18:42:17 -0400 | [diff] [blame] | 139 | run(ViewWindow) |