| 1 |
"""Simple text browser for IDLE |
| 2 |
|
| 3 |
""" |
| 4 |
|
| 5 |
from Tkinter import * |
| 6 |
import tkMessageBox |
| 7 |
|
| 8 |
class TextViewer(Toplevel): |
| 9 |
"""A simple text viewer dialog for IDLE |
| 10 |
|
| 11 |
""" |
| 12 |
def __init__(self, parent, title, text, modal=True, _htest=False): |
| 13 |
"""Show the given text in a scrollable window with a 'close' button |
| 14 |
|
| 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. |
| 20 |
""" |
| 21 |
Toplevel.__init__(self, parent) |
| 22 |
self.configure(borderwidth=5) |
| 23 |
|
| 24 |
self.geometry("=%dx%d+%d+%d" % (750, 500, |
| 25 |
parent.winfo_rootx() + 10, |
| 26 |
parent.winfo_rooty() + (10 if not _htest else 100))) |
| 27 |
|
| 28 |
self.bg = '#ffffff' |
| 29 |
self.fg = '#000000' |
| 30 |
|
| 31 |
self.CreateWidgets() |
| 32 |
self.title(title) |
| 33 |
self.protocol("WM_DELETE_WINDOW", self.Ok) |
| 34 |
self.parent = parent |
| 35 |
self.textView.focus_set() |
| 36 |
|
| 37 |
self.bind('<Return>',self.Ok) |
| 38 |
self.bind('<Escape>',self.Ok) |
| 39 |
self.textView.insert(0.0, text) |
| 40 |
self.textView.config(state=DISABLED) |
| 41 |
|
| 42 |
self.is_modal = modal |
| 43 |
if self.is_modal: |
| 44 |
self.transient(parent) |
| 45 |
self.grab_set() |
| 46 |
self.wait_window() |
| 47 |
|
| 48 |
def CreateWidgets(self): |
| 49 |
frameText = Frame(self, relief=SUNKEN, height=700) |
| 50 |
frameButtons = Frame(self) |
| 51 |
self.buttonOk = Button(frameButtons, text='Close', |
| 52 |
command=self.Ok, takefocus=FALSE) |
| 53 |
self.scrollbarView = Scrollbar(frameText, orient=VERTICAL, |
| 54 |
takefocus=FALSE, highlightthickness=0) |
| 55 |
self.textView = Text(frameText, wrap=WORD, highlightthickness=0, |
| 56 |
fg=self.fg, bg=self.bg) |
| 57 |
self.scrollbarView.config(command=self.textView.yview) |
| 58 |
self.textView.config(yscrollcommand=self.scrollbarView.set) |
| 59 |
self.buttonOk.pack() |
| 60 |
self.scrollbarView.pack(side=RIGHT,fill=Y) |
| 61 |
self.textView.pack(side=LEFT,expand=TRUE,fill=BOTH) |
| 62 |
frameButtons.pack(side=BOTTOM,fill=X) |
| 63 |
frameText.pack(side=TOP,expand=TRUE,fill=BOTH) |
| 64 |
|
| 65 |
def Ok(self, event=None): |
| 66 |
if self.is_modal: |
| 67 |
self.grab_release() |
| 68 |
self.destroy() |
| 69 |
|
| 70 |
|
| 71 |
def view_text(parent, title, text, modal=True): |
| 72 |
return TextViewer(parent, title, text, modal) |
| 73 |
|
| 74 |
def view_file(parent, title, filename, encoding=None, modal=True): |
| 75 |
try: |
| 76 |
if encoding: |
| 77 |
import codecs |
| 78 |
textFile = codecs.open(filename, 'r') |
| 79 |
else: |
| 80 |
textFile = open(filename, 'r') |
| 81 |
except IOError: |
| 82 |
tkMessageBox.showerror(title='File Load Error', |
| 83 |
message='Unable to load file %r .' % filename, |
| 84 |
parent=parent) |
| 85 |
except UnicodeDecodeError as err: |
| 86 |
showerror(title='Unicode Decode Error', |
| 87 |
message=str(err), |
| 88 |
parent=parent) |
| 89 |
else: |
| 90 |
return view_text(parent, title, textFile.read(), modal) |
| 91 |
|
| 92 |
|
| 93 |
if __name__ == '__main__': |
| 94 |
import unittest |
| 95 |
unittest.main('idlelib.idle_test.test_textview', verbosity=2, exit=False) |
| 96 |
from idlelib.idle_test.htest import run |
| 97 |
run(TextViewer) |