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 | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame^] | 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 |
Terry Jan Reedy | 01e3575 | 2016-06-10 18:19:21 -0400 | [diff] [blame] | 7 | from tkinter.ttk import Scrollbar |
Terry Jan Reedy | 82c4615 | 2016-06-22 04:54:18 -0400 | [diff] [blame] | 8 | from tkinter.messagebox import showerror |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 9 | |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 10 | |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 11 | class TextViewer(Toplevel): |
Terry Jan Reedy | a3623c8 | 2016-08-31 19:45:39 -0400 | [diff] [blame] | 12 | "A simple text viewer dialog for IDLE." |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 13 | |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 14 | def __init__(self, parent, title, text, modal=True, |
| 15 | _htest=False, _utest=False): |
| 16 | """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] | 17 | |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 18 | If modal is left True, users cannot interact with other windows |
| 19 | until the textview window is closed. |
Terry Jan Reedy | 537e2c8 | 2014-06-05 03:38:34 -0400 | [diff] [blame] | 20 | |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame^] | 21 | parent - parent of this dialog |
| 22 | title - string which is title of popup dialog |
| 23 | text - text to display in dialog |
Terry Jan Reedy | 537e2c8 | 2014-06-05 03:38:34 -0400 | [diff] [blame] | 24 | _htest - bool; change box location when running htest. |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 25 | _utest - bool; don't wait_window when running unittest. |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 26 | """ |
| 27 | Toplevel.__init__(self, parent) |
| 28 | self.configure(borderwidth=5) |
Terry Jan Reedy | a3623c8 | 2016-08-31 19:45:39 -0400 | [diff] [blame] | 29 | # Place dialog below parent if running htest. |
Terry Jan Reedy | d0cadba | 2015-10-11 22:07:31 -0400 | [diff] [blame] | 30 | self.geometry("=%dx%d+%d+%d" % (750, 500, |
Terry Jan Reedy | 1b392ff | 2014-05-24 18:48:18 -0400 | [diff] [blame] | 31 | parent.winfo_rootx() + 10, |
| 32 | parent.winfo_rooty() + (10 if not _htest else 100))) |
Terry Jan Reedy | a3623c8 | 2016-08-31 19:45:39 -0400 | [diff] [blame] | 33 | # TODO: get fg/bg from theme. |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 34 | self.bg = '#ffffff' |
| 35 | self.fg = '#000000' |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 36 | |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame^] | 37 | self.create_widgets() |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 38 | self.title(title) |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame^] | 39 | self.protocol("WM_DELETE_WINDOW", self.ok) |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 40 | self.parent = parent |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame^] | 41 | self.text.focus_set() |
Terry Jan Reedy | a3623c8 | 2016-08-31 19:45:39 -0400 | [diff] [blame] | 42 | # Bind keys for closing this dialog. |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame^] | 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) |
Terry Jan Reedy | e91e763 | 2012-02-05 15:14:20 -0500 | [diff] [blame] | 47 | |
| 48 | if modal: |
| 49 | self.transient(parent) |
| 50 | self.grab_set() |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 51 | if not _utest: |
| 52 | self.wait_window() |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 53 | |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame^] | 54 | def create_widgets(self): |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 55 | "Create Frame with Text (with vertical Scrollbar) and Button." |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame^] | 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, |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 62 | fg=self.fg, bg=self.bg) |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame^] | 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) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 71 | |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame^] | 72 | def ok(self, event=None): |
| 73 | """Dismiss text viewer dialog.""" |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 74 | self.destroy() |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 76 | |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 77 | def view_text(parent, title, text, modal=True, _utest=False): |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame^] | 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 | """ |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 87 | return TextViewer(parent, title, text, modal, _utest=_utest) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 88 | |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame^] | 89 | |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 90 | def view_file(parent, title, filename, encoding=None, modal=True, _utest=False): |
csabella | 0aa0a06 | 2017-05-28 06:50:55 -0400 | [diff] [blame^] | 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 | """ |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 96 | try: |
Éric Araujo | ccf03a1 | 2011-08-01 17:29:36 +0200 | [diff] [blame] | 97 | with open(filename, 'r', encoding=encoding) as file: |
| 98 | contents = file.read() |
Terry Jan Reedy | 82c4615 | 2016-06-22 04:54:18 -0400 | [diff] [blame] | 99 | except OSError: |
| 100 | showerror(title='File Load Error', |
| 101 | message='Unable to load file %r .' % filename, |
| 102 | parent=parent) |
| 103 | except UnicodeDecodeError as err: |
| 104 | showerror(title='Unicode Decode Error', |
| 105 | message=str(err), |
| 106 | parent=parent) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 107 | else: |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 108 | return view_text(parent, title, contents, modal, _utest=_utest) |
terryjreedy | 295304d | 2017-05-17 22:59:46 -0400 | [diff] [blame] | 109 | return None |
Louie Lu | ba365da | 2017-05-18 05:51:31 +0800 | [diff] [blame] | 110 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 111 | |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 112 | if __name__ == '__main__': |
Terry Jan Reedy | 537e2c8 | 2014-06-05 03:38:34 -0400 | [diff] [blame] | 113 | import unittest |
| 114 | unittest.main('idlelib.idle_test.test_textview', verbosity=2, exit=False) |
Terry Jan Reedy | 1b392ff | 2014-05-24 18:48:18 -0400 | [diff] [blame] | 115 | from idlelib.idle_test.htest import run |
| 116 | run(TextViewer) |