Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 1 | """Simple text browser for IDLE |
| 2 | |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 3 | """ |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 4 | |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 5 | from tkinter import * |
| 6 | import tkinter.messagebox as tkMessageBox |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 7 | |
| 8 | class TextViewer(Toplevel): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 9 | """A simple text viewer dialog for IDLE |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 11 | """ |
| 12 | def __init__(self, parent, title, text): |
| 13 | """Show the given text in a scrollable window with a 'close' button |
| 14 | |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 15 | """ |
| 16 | Toplevel.__init__(self, parent) |
| 17 | self.configure(borderwidth=5) |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 18 | self.geometry("=%dx%d+%d+%d" % (625, 500, |
| 19 | parent.winfo_rootx() + 10, |
| 20 | parent.winfo_rooty() + 10)) |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 21 | #elguavas - config placeholders til config stuff completed |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 22 | self.bg = '#ffffff' |
| 23 | self.fg = '#000000' |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 24 | |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 35 | self.textView.insert(0.0, text) |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 36 | self.textView.config(state=DISABLED) |
| 37 | self.wait_window() |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 38 | |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 39 | def CreateWidgets(self): |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 40 | frameText = Frame(self, relief=SUNKEN, height=700) |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 41 | frameButtons = Frame(self) |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 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) |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 48 | self.scrollbarView.config(command=self.textView.yview) |
| 49 | self.textView.config(yscrollcommand=self.scrollbarView.set) |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 50 | self.buttonOk.pack() |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 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) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 55 | |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 56 | def Ok(self, event=None): |
| 57 | self.destroy() |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 59 | |
| 60 | def view_text(parent, title, text): |
| 61 | TextViewer(parent, title, text) |
| 62 | |
| 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: |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 71 | import tkinter.messagebox as tkMessageBox |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 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()) |
| 77 | |
| 78 | |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 79 | if __name__ == '__main__': |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 80 | #test the dialog |
| 81 | root=Tk() |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 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) |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 93 | root.mainloop() |