0001 """Simple text browser for IDLE 0002 0003 """ 0004 0005 from Tkinter import * 0006 import tkMessageBox 0007 0008 class TextViewer(Toplevel): 0009 """ 0010 simple text viewer dialog for idle 0011 """ 0012 def __init__(self, parent, title, fileName, data=None): 0013 """If data exists, load it into viewer, otherwise try to load file. 0014 0015 fileName - string, should be an absoulute filename 0016 """ 0017 Toplevel.__init__(self, parent) 0018 self.configure(borderwidth=5) 0019 self.geometry("=%dx%d+%d+%d" % (625, 500, 0020 parent.winfo_rootx() + 10, 0021 parent.winfo_rooty() + 10)) 0022 #elguavas - config placeholders til config stuff completed 0023 self.bg = '#ffffff' 0024 self.fg = '#000000' 0025 0026 self.CreateWidgets() 0027 self.title(title) 0028 self.transient(parent) 0029 self.grab_set() 0030 self.protocol("WM_DELETE_WINDOW", self.Ok) 0031 self.parent = parent 0032 self.textView.focus_set() 0033 #key bindings for this dialog 0034 self.bind('<Return>',self.Ok) #dismiss dialog 0035 self.bind('<Escape>',self.Ok) #dismiss dialog 0036 if data: 0037 self.textView.insert(0.0, data) 0038 else: 0039 self.LoadTextFile(fileName) 0040 self.textView.config(state=DISABLED) 0041 self.wait_window() 0042 0043 def LoadTextFile(self, fileName): 0044 textFile = None 0045 try: 0046 textFile = open(fileName, 'r') 0047 except IOError: 0048 tkMessageBox.showerror(title='File Load Error', 0049 message='Unable to load file %r .' % (fileName,)) 0050 else: 0051 self.textView.insert(0.0,textFile.read()) 0052 0053 def CreateWidgets(self): 0054 frameText = Frame(self, relief=SUNKEN, height=700) 0055 frameButtons = Frame(self) 0056 self.buttonOk = Button(frameButtons, text='Close', 0057 command=self.Ok, takefocus=FALSE) 0058 self.scrollbarView = Scrollbar(frameText, orient=VERTICAL, 0059 takefocus=FALSE, highlightthickness=0) 0060 self.textView = Text(frameText, wrap=WORD, highlightthickness=0, 0061 fg=self.fg, bg=self.bg) 0062 self.scrollbarView.config(command=self.textView.yview) 0063 self.textView.config(yscrollcommand=self.scrollbarView.set) 0064 self.buttonOk.pack() 0065 self.scrollbarView.pack(side=RIGHT,fill=Y) 0066 self.textView.pack(side=LEFT,expand=TRUE,fill=BOTH) 0067 frameButtons.pack(side=BOTTOM,fill=X) 0068 frameText.pack(side=TOP,expand=TRUE,fill=BOTH) 0069 0070 def Ok(self, event=None): 0071 self.destroy() 0072 0073 if __name__ == '__main__': 0074 #test the dialog 0075 root=Tk() 0076 Button(root,text='View', 0077 command=lambda:TextViewer(root,'Text','./textView.py')).pack() 0078 root.mainloop() 0079
Generated by PyXR 0.9.4