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 | |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 5 | from Tkinter import * |
| 6 | import tkMessageBox |
| 7 | |
| 8 | class TextViewer(Toplevel): |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 9 | """ |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 10 | simple text viewer dialog for idle |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 11 | """ |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 12 | def __init__(self, parent, title, fileName, data=None): |
| 13 | """If data exists, load it into viewer, otherwise try to load file. |
| 14 | |
| 15 | fileName - string, should be an absoulute filename |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 16 | """ |
| 17 | Toplevel.__init__(self, parent) |
| 18 | self.configure(borderwidth=5) |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 19 | self.geometry("=%dx%d+%d+%d" % (625, 500, |
| 20 | parent.winfo_rootx() + 10, |
| 21 | parent.winfo_rooty() + 10)) |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 22 | #elguavas - config placeholders til config stuff completed |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 23 | self.bg = '#ffffff' |
| 24 | self.fg = '#000000' |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 25 | |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 26 | self.CreateWidgets() |
| 27 | self.title(title) |
| 28 | self.transient(parent) |
| 29 | self.grab_set() |
| 30 | self.protocol("WM_DELETE_WINDOW", self.Ok) |
| 31 | self.parent = parent |
| 32 | self.textView.focus_set() |
| 33 | #key bindings for this dialog |
| 34 | self.bind('<Return>',self.Ok) #dismiss dialog |
| 35 | self.bind('<Escape>',self.Ok) #dismiss dialog |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 36 | if data: |
| 37 | self.textView.insert(0.0, data) |
| 38 | else: |
| 39 | self.LoadTextFile(fileName) |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 40 | self.textView.config(state=DISABLED) |
| 41 | self.wait_window() |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 42 | |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 43 | def LoadTextFile(self, fileName): |
| 44 | textFile = None |
| 45 | try: |
| 46 | textFile = open(fileName, 'r') |
| 47 | except IOError: |
| 48 | tkMessageBox.showerror(title='File Load Error', |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 49 | message='Unable to load file %r .' % (fileName,)) |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 50 | else: |
| 51 | self.textView.insert(0.0,textFile.read()) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 52 | |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 53 | def CreateWidgets(self): |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 54 | frameText = Frame(self, relief=SUNKEN, height=700) |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 55 | frameButtons = Frame(self) |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 56 | self.buttonOk = Button(frameButtons, text='Close', |
| 57 | command=self.Ok, takefocus=FALSE) |
| 58 | self.scrollbarView = Scrollbar(frameText, orient=VERTICAL, |
| 59 | takefocus=FALSE, highlightthickness=0) |
| 60 | self.textView = Text(frameText, wrap=WORD, highlightthickness=0, |
| 61 | fg=self.fg, bg=self.bg) |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 62 | self.scrollbarView.config(command=self.textView.yview) |
| 63 | self.textView.config(yscrollcommand=self.scrollbarView.set) |
Kurt B. Kaiser | 09cb74b | 2003-06-12 04:20:56 +0000 | [diff] [blame] | 64 | self.buttonOk.pack() |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 65 | self.scrollbarView.pack(side=RIGHT,fill=Y) |
| 66 | self.textView.pack(side=LEFT,expand=TRUE,fill=BOTH) |
| 67 | frameButtons.pack(side=BOTTOM,fill=X) |
| 68 | frameText.pack(side=TOP,expand=TRUE,fill=BOTH) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 69 | |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 70 | def Ok(self, event=None): |
| 71 | self.destroy() |
Steven M. Gava | 44d3d1a | 2001-07-31 06:59:02 +0000 | [diff] [blame] | 72 | |
| 73 | if __name__ == '__main__': |
Steven M. Gava | d721c48 | 2001-07-31 10:46:53 +0000 | [diff] [blame] | 74 | #test the dialog |
| 75 | root=Tk() |
| 76 | Button(root,text='View', |
| 77 | command=lambda:TextViewer(root,'Text','./textView.py')).pack() |
| 78 | root.mainloop() |