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