Require implementations for warnings.showwarning() support the 'line' argument.
[python.git] / Lib / idlelib / textView.py
blob0e7e6631856702ddf98947aaec180646b40b16f2
1 """Simple text browser for IDLE
3 """
5 from Tkinter import *
6 import tkMessageBox
8 class TextViewer(Toplevel):
9 """A simple text viewer dialog for IDLE
11 """
12 def __init__(self, parent, title, text):
13 """Show the given text in a scrollable window with a 'close' button
15 """
16 Toplevel.__init__(self, parent)
17 self.configure(borderwidth=5)
18 self.geometry("=%dx%d+%d+%d" % (625, 500,
19 parent.winfo_rootx() + 10,
20 parent.winfo_rooty() + 10))
21 #elguavas - config placeholders til config stuff completed
22 self.bg = '#ffffff'
23 self.fg = '#000000'
25 self.CreateWidgets()
26 self.title(title)
27 self.transient(parent)
28 self.grab_set()
29 self.protocol("WM_DELETE_WINDOW", self.Ok)
30 self.parent = parent
31 self.textView.focus_set()
32 #key bindings for this dialog
33 self.bind('<Return>',self.Ok) #dismiss dialog
34 self.bind('<Escape>',self.Ok) #dismiss dialog
35 self.textView.insert(0.0, text)
36 self.textView.config(state=DISABLED)
37 self.wait_window()
39 def CreateWidgets(self):
40 frameText = Frame(self, relief=SUNKEN, height=700)
41 frameButtons = Frame(self)
42 self.buttonOk = Button(frameButtons, text='Close',
43 command=self.Ok, takefocus=FALSE)
44 self.scrollbarView = Scrollbar(frameText, orient=VERTICAL,
45 takefocus=FALSE, highlightthickness=0)
46 self.textView = Text(frameText, wrap=WORD, highlightthickness=0,
47 fg=self.fg, bg=self.bg)
48 self.scrollbarView.config(command=self.textView.yview)
49 self.textView.config(yscrollcommand=self.scrollbarView.set)
50 self.buttonOk.pack()
51 self.scrollbarView.pack(side=RIGHT,fill=Y)
52 self.textView.pack(side=LEFT,expand=TRUE,fill=BOTH)
53 frameButtons.pack(side=BOTTOM,fill=X)
54 frameText.pack(side=TOP,expand=TRUE,fill=BOTH)
56 def Ok(self, event=None):
57 self.destroy()
60 def view_text(parent, title, text):
61 TextViewer(parent, title, text)
63 def view_file(parent, title, filename, encoding=None):
64 try:
65 if encoding:
66 import codecs
67 textFile = codecs.open(filename, 'r')
68 else:
69 textFile = open(filename, 'r')
70 except IOError:
71 import tkMessageBox
72 tkMessageBox.showerror(title='File Load Error',
73 message='Unable to load file %r .' % filename,
74 parent=parent)
75 else:
76 return view_text(parent, title, textFile.read())
79 if __name__ == '__main__':
80 #test the dialog
81 root=Tk()
82 root.title('textView test')
83 filename = './textView.py'
84 text = file(filename, 'r').read()
85 btn1 = Button(root, text='view_text',
86 command=lambda:view_text(root, 'view_text', text))
87 btn1.pack(side=LEFT)
88 btn2 = Button(root, text='view_file',
89 command=lambda:view_file(root, 'view_file', filename))
90 btn2.pack(side=LEFT)
91 close = Button(root, text='Close', command=root.destroy)
92 close.pack(side=RIGHT)
93 root.mainloop()