ยปCore Development>Code coverage>Lib/idlelib/textview.py

Python code coverage for Lib/idlelib/textview.py

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