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 | """ |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 4 | |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 5 | from tkinter import * |
| 6 | import tkinter.messagebox as tkMessageBox |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 7 | |
| 8 | class TextViewer(Toplevel): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 9 | """A simple text viewer dialog for IDLE |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 11 | """ |
Terry Jan Reedy | 1b392ff | 2014-05-24 18:48:18 -0400 | [diff] [blame] | 12 | def __init__(self, parent, title, text, modal=True, _htest=False): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 13 | """Show the given text in a scrollable window with a 'close' button |
| 14 | |
Terry Jan Reedy | 537e2c8 | 2014-06-05 03:38:34 -0400 | [diff] [blame] | 15 | If modal option set to False, user can interact with other windows, |
| 16 | otherwise they will be unable to interact with other windows until |
| 17 | the textview window is closed. |
| 18 | |
| 19 | _htest - bool; change box location when running htest. |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 20 | """ |
| 21 | Toplevel.__init__(self, parent) |
| 22 | self.configure(borderwidth=5) |
Terry Jan Reedy | 1b392ff | 2014-05-24 18:48:18 -0400 | [diff] [blame] | 23 | # place dialog below parent if running htest |
Terry Jan Reedy | d0cadba | 2015-10-11 22:07:31 -0400 | [diff] [blame^] | 24 | self.geometry("=%dx%d+%d+%d" % (750, 500, |
Terry Jan Reedy | 1b392ff | 2014-05-24 18:48:18 -0400 | [diff] [blame] | 25 | parent.winfo_rootx() + 10, |
| 26 | parent.winfo_rooty() + (10 if not _htest else 100))) |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 27 | #elguavas - config placeholders til config stuff completed |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 28 | self.bg = '#ffffff' |
| 29 | self.fg = '#000000' |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 30 | |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 31 | self.CreateWidgets() |
| 32 | self.title(title) |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 33 | self.protocol("WM_DELETE_WINDOW", self.Ok) |
| 34 | self.parent = parent |
| 35 | self.textView.focus_set() |
| 36 | #key bindings for this dialog |
| 37 | self.bind('<Return>',self.Ok) #dismiss dialog |
| 38 | self.bind('<Escape>',self.Ok) #dismiss dialog |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 39 | self.textView.insert(0.0, text) |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 40 | self.textView.config(state=DISABLED) |
Terry Jan Reedy | e91e763 | 2012-02-05 15:14:20 -0500 | [diff] [blame] | 41 | |
| 42 | if modal: |
| 43 | self.transient(parent) |
| 44 | self.grab_set() |
| 45 | self.wait_window() |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 46 | |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 47 | def CreateWidgets(self): |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 48 | frameText = Frame(self, relief=SUNKEN, height=700) |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 49 | frameButtons = Frame(self) |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 50 | self.buttonOk = Button(frameButtons, text='Close', |
| 51 | command=self.Ok, takefocus=FALSE) |
| 52 | self.scrollbarView = Scrollbar(frameText, orient=VERTICAL, |
| 53 | takefocus=FALSE, highlightthickness=0) |
| 54 | self.textView = Text(frameText, wrap=WORD, highlightthickness=0, |
| 55 | fg=self.fg, bg=self.bg) |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 56 | self.scrollbarView.config(command=self.textView.yview) |
| 57 | self.textView.config(yscrollcommand=self.scrollbarView.set) |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 58 | self.buttonOk.pack() |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 59 | self.scrollbarView.pack(side=RIGHT,fill=Y) |
| 60 | self.textView.pack(side=LEFT,expand=TRUE,fill=BOTH) |
| 61 | frameButtons.pack(side=BOTTOM,fill=X) |
| 62 | frameText.pack(side=TOP,expand=TRUE,fill=BOTH) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 63 | |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 64 | def Ok(self, event=None): |
| 65 | self.destroy() |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 67 | |
Terry Jan Reedy | e91e763 | 2012-02-05 15:14:20 -0500 | [diff] [blame] | 68 | def view_text(parent, title, text, modal=True): |
| 69 | return TextViewer(parent, title, text, modal) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 70 | |
Terry Jan Reedy | e91e763 | 2012-02-05 15:14:20 -0500 | [diff] [blame] | 71 | def view_file(parent, title, filename, encoding=None, modal=True): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 72 | try: |
Éric Araujo | ccf03a1 | 2011-08-01 17:29:36 +0200 | [diff] [blame] | 73 | with open(filename, 'r', encoding=encoding) as file: |
| 74 | contents = file.read() |
Terry Jan Reedy | 537e2c8 | 2014-06-05 03:38:34 -0400 | [diff] [blame] | 75 | except IOError: |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 76 | tkMessageBox.showerror(title='File Load Error', |
| 77 | message='Unable to load file %r .' % filename, |
| 78 | parent=parent) |
| 79 | else: |
Terry Jan Reedy | e91e763 | 2012-02-05 15:14:20 -0500 | [diff] [blame] | 80 | return view_text(parent, title, contents, modal) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 81 | |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 82 | if __name__ == '__main__': |
Terry Jan Reedy | 537e2c8 | 2014-06-05 03:38:34 -0400 | [diff] [blame] | 83 | import unittest |
| 84 | unittest.main('idlelib.idle_test.test_textview', verbosity=2, exit=False) |
Terry Jan Reedy | 1b392ff | 2014-05-24 18:48:18 -0400 | [diff] [blame] | 85 | from idlelib.idle_test.htest import run |
| 86 | run(TextViewer) |