ยป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/a
5n/afrom tkinter import *
6n/aimport tkinter.messagebox as tkMessageBox
7n/a
8n/aclass TextViewer(Toplevel):
9n/a """A simple text viewer dialog for IDLE
10n/a
11n/a """
12n/a def __init__(self, parent, title, text, modal=True):
13n/a """Show the given text in a scrollable window with a 'close' button
14n/a
15n/a """
16n/a Toplevel.__init__(self, parent)
17n/a self.configure(borderwidth=5)
18n/a self.geometry("=%dx%d+%d+%d" % (625, 500,
19n/a parent.winfo_rootx() + 10,
20n/a parent.winfo_rooty() + 10))
21n/a #elguavas - config placeholders til config stuff completed
22n/a self.bg = '#ffffff'
23n/a self.fg = '#000000'
24n/a
25n/a self.CreateWidgets()
26n/a self.title(title)
27n/a self.protocol("WM_DELETE_WINDOW", self.Ok)
28n/a self.parent = parent
29n/a self.textView.focus_set()
30n/a #key bindings for this dialog
31n/a self.bind('<Return>',self.Ok) #dismiss dialog
32n/a self.bind('<Escape>',self.Ok) #dismiss dialog
33n/a self.textView.insert(0.0, text)
34n/a self.textView.config(state=DISABLED)
35n/a
36n/a if modal:
37n/a self.transient(parent)
38n/a self.grab_set()
39n/a self.wait_window()
40n/a
41n/a def CreateWidgets(self):
42n/a frameText = Frame(self, relief=SUNKEN, height=700)
43n/a frameButtons = Frame(self)
44n/a self.buttonOk = Button(frameButtons, text='Close',
45n/a command=self.Ok, takefocus=FALSE)
46n/a self.scrollbarView = Scrollbar(frameText, orient=VERTICAL,
47n/a takefocus=FALSE, highlightthickness=0)
48n/a self.textView = Text(frameText, wrap=WORD, highlightthickness=0,
49n/a fg=self.fg, bg=self.bg)
50n/a self.scrollbarView.config(command=self.textView.yview)
51n/a self.textView.config(yscrollcommand=self.scrollbarView.set)
52n/a self.buttonOk.pack()
53n/a self.scrollbarView.pack(side=RIGHT,fill=Y)
54n/a self.textView.pack(side=LEFT,expand=TRUE,fill=BOTH)
55n/a frameButtons.pack(side=BOTTOM,fill=X)
56n/a frameText.pack(side=TOP,expand=TRUE,fill=BOTH)
57n/a
58n/a def Ok(self, event=None):
59n/a self.destroy()
60n/a
61n/a
62n/adef view_text(parent, title, text, modal=True):
63n/a return TextViewer(parent, title, text, modal)
64n/a
65n/adef view_file(parent, title, filename, encoding=None, modal=True):
66n/a try:
67n/a with open(filename, 'r', encoding=encoding) as file:
68n/a contents = file.read()
69n/a except OSError:
70n/a import tkinter.messagebox as tkMessageBox
71n/a tkMessageBox.showerror(title='File Load Error',
72n/a message='Unable to load file %r .' % filename,
73n/a parent=parent)
74n/a else:
75n/a return view_text(parent, title, contents, modal)
76n/a
77n/a
78n/aif __name__ == '__main__':
79n/a #test the dialog
80n/a root=Tk()
81n/a root.title('textView test')
82n/a filename = './textView.py'
83n/a with open(filename, 'r') as f:
84n/a text = f.read()
85n/a btn1 = Button(root, text='view_text',
86n/a command=lambda:view_text(root, 'view_text', text))
87n/a btn1.pack(side=LEFT)
88n/a btn2 = Button(root, text='view_file',
89n/a command=lambda:view_file(root, 'view_file', filename))
90n/a btn2.pack(side=LEFT)
91n/a btn3 = Button(root, text='nonmodal view_text',
92n/a command=lambda:view_text(root, 'nonmodal view_text', text,
93n/a modal=False))
94n/a btn3.pack(side=LEFT)
95n/a close = Button(root, text='Close', command=root.destroy)
96n/a close.pack(side=RIGHT)
97n/a root.mainloop()