Guido van Rossum | 57cd21f | 2003-04-29 10:23:27 +0000 | [diff] [blame^] | 1 | ##---------------------------------------------------------------------------## |
| 2 | ## |
| 3 | ## idle - simple text view dialog |
| 4 | ## elguavas |
| 5 | ## |
| 6 | ##---------------------------------------------------------------------------## |
| 7 | """ |
| 8 | simple text browser for idle |
| 9 | """ |
| 10 | from Tkinter import * |
| 11 | import tkMessageBox |
| 12 | |
| 13 | class TextViewer(Toplevel): |
| 14 | """ |
| 15 | simple text viewer dialog for idle |
| 16 | """ |
| 17 | def __init__(self,parent,title,fileName): |
| 18 | """ |
| 19 | fileName - string,should be an absoulute filename |
| 20 | """ |
| 21 | Toplevel.__init__(self, parent) |
| 22 | self.configure(borderwidth=5) |
| 23 | self.geometry("+%d+%d" % (parent.winfo_rootx()+10, |
| 24 | parent.winfo_rooty()+10)) |
| 25 | #elguavas - config placeholders til config stuff completed |
| 26 | self.bg=None |
| 27 | self.fg=None |
| 28 | |
| 29 | self.CreateWidgets() |
| 30 | self.title(title) |
| 31 | self.transient(parent) |
| 32 | self.grab_set() |
| 33 | self.protocol("WM_DELETE_WINDOW", self.Ok) |
| 34 | self.parent = parent |
| 35 | self.textView.focus_set() |
| 36 | #key bindings for this dialog |
| 37 | self.bind('<Return>',self.Ok) #dismiss dialog |
| 38 | self.bind('<Escape>',self.Ok) #dismiss dialog |
| 39 | self.LoadTextFile(fileName) |
| 40 | self.textView.config(state=DISABLED) |
| 41 | self.wait_window() |
| 42 | |
| 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', |
| 49 | message='Unable to load file '+`fileName`+' .') |
| 50 | else: |
| 51 | self.textView.insert(0.0,textFile.read()) |
| 52 | |
| 53 | def CreateWidgets(self): |
| 54 | frameText = Frame(self) |
| 55 | frameButtons = Frame(self) |
| 56 | self.buttonOk = Button(frameButtons,text='Ok', |
| 57 | command=self.Ok,takefocus=FALSE,default=ACTIVE) |
| 58 | self.scrollbarView = Scrollbar(frameText,orient=VERTICAL, |
| 59 | takefocus=FALSE,highlightthickness=0) |
| 60 | self.textView = Text(frameText,wrap=WORD,highlightthickness=0) |
| 61 | self.scrollbarView.config(command=self.textView.yview) |
| 62 | self.textView.config(yscrollcommand=self.scrollbarView.set) |
| 63 | self.buttonOk.pack(padx=5,pady=5) |
| 64 | self.scrollbarView.pack(side=RIGHT,fill=Y) |
| 65 | self.textView.pack(side=LEFT,expand=TRUE,fill=BOTH) |
| 66 | frameButtons.pack(side=BOTTOM,fill=X) |
| 67 | frameText.pack(side=TOP,expand=TRUE,fill=BOTH) |
| 68 | |
| 69 | def Ok(self, event=None): |
| 70 | self.destroy() |
| 71 | |
| 72 | if __name__ == '__main__': |
| 73 | #test the dialog |
| 74 | root=Tk() |
| 75 | Button(root,text='View', |
| 76 | command=lambda:TextViewer(root,'Text','./textView.py')).pack() |
| 77 | root.mainloop() |