blob: 3f3c24778914713d988c5375e1069027929c080b [file] [log] [blame]
Kurt B. Kaiser09cb74b2003-06-12 04:20:56 +00001"""Simple text browser for IDLE
2
Steven M. Gava44d3d1a2001-07-31 06:59:02 +00003"""
Kurt B. Kaiser09cb74b2003-06-12 04:20:56 +00004
Georg Brandl14fc4272008-05-17 18:39:55 +00005from tkinter import *
6import tkinter.messagebox as tkMessageBox
Steven M. Gava44d3d1a2001-07-31 06:59:02 +00007
8class TextViewer(Toplevel):
Guido van Rossum8ce8a782007-11-01 19:42:39 +00009 """A simple text viewer dialog for IDLE
Kurt B. Kaiser09cb74b2003-06-12 04:20:56 +000010
Guido van Rossum8ce8a782007-11-01 19:42:39 +000011 """
12 def __init__(self, parent, title, text):
13 """Show the given text in a scrollable window with a 'close' button
14
Steven M. Gavad721c482001-07-31 10:46:53 +000015 """
16 Toplevel.__init__(self, parent)
17 self.configure(borderwidth=5)
Kurt B. Kaiser09cb74b2003-06-12 04:20:56 +000018 self.geometry("=%dx%d+%d+%d" % (625, 500,
19 parent.winfo_rootx() + 10,
20 parent.winfo_rooty() + 10))
Steven M. Gavad721c482001-07-31 10:46:53 +000021 #elguavas - config placeholders til config stuff completed
Kurt B. Kaiser09cb74b2003-06-12 04:20:56 +000022 self.bg = '#ffffff'
23 self.fg = '#000000'
Steven M. Gava44d3d1a2001-07-31 06:59:02 +000024
Steven M. Gavad721c482001-07-31 10:46:53 +000025 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
Guido van Rossum8ce8a782007-11-01 19:42:39 +000035 self.textView.insert(0.0, text)
Steven M. Gavad721c482001-07-31 10:46:53 +000036 self.textView.config(state=DISABLED)
37 self.wait_window()
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +000038
Steven M. Gavad721c482001-07-31 10:46:53 +000039 def CreateWidgets(self):
Kurt B. Kaiser09cb74b2003-06-12 04:20:56 +000040 frameText = Frame(self, relief=SUNKEN, height=700)
Steven M. Gavad721c482001-07-31 10:46:53 +000041 frameButtons = Frame(self)
Kurt B. Kaiser09cb74b2003-06-12 04:20:56 +000042 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)
Steven M. Gavad721c482001-07-31 10:46:53 +000048 self.scrollbarView.config(command=self.textView.yview)
49 self.textView.config(yscrollcommand=self.scrollbarView.set)
Kurt B. Kaiser09cb74b2003-06-12 04:20:56 +000050 self.buttonOk.pack()
Steven M. Gavad721c482001-07-31 10:46:53 +000051 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)
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +000055
Steven M. Gavad721c482001-07-31 10:46:53 +000056 def Ok(self, event=None):
57 self.destroy()
Steven M. Gava44d3d1a2001-07-31 06:59:02 +000058
Guido van Rossum8ce8a782007-11-01 19:42:39 +000059
60def view_text(parent, title, text):
61 TextViewer(parent, title, text)
62
63def 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:
Georg Brandl14fc4272008-05-17 18:39:55 +000071 import tkinter.messagebox as tkMessageBox
Guido van Rossum8ce8a782007-11-01 19:42:39 +000072 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())
77
78
Steven M. Gava44d3d1a2001-07-31 06:59:02 +000079if __name__ == '__main__':
Steven M. Gavad721c482001-07-31 10:46:53 +000080 #test the dialog
81 root=Tk()
Guido van Rossum8ce8a782007-11-01 19:42:39 +000082 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)
Steven M. Gavad721c482001-07-31 10:46:53 +000093 root.mainloop()