[2] | 1 | """Simple text browser for IDLE
|
---|
| 2 |
|
---|
| 3 | """
|
---|
| 4 |
|
---|
| 5 | from Tkinter import *
|
---|
| 6 | import tkMessageBox
|
---|
| 7 |
|
---|
| 8 | class TextViewer(Toplevel):
|
---|
| 9 | """A simple text viewer dialog for IDLE
|
---|
| 10 |
|
---|
| 11 | """
|
---|
[391] | 12 | def __init__(self, parent, title, text, modal=True):
|
---|
[2] | 13 | """Show the given text in a scrollable window with a 'close' button
|
---|
| 14 |
|
---|
| 15 | """
|
---|
| 16 | Toplevel.__init__(self, parent)
|
---|
| 17 | self.configure(borderwidth=5)
|
---|
| 18 | self.geometry("=%dx%d+%d+%d" % (625, 500,
|
---|
| 19 | parent.winfo_rootx() + 10,
|
---|
| 20 | parent.winfo_rooty() + 10))
|
---|
| 21 | #elguavas - config placeholders til config stuff completed
|
---|
| 22 | self.bg = '#ffffff'
|
---|
| 23 | self.fg = '#000000'
|
---|
| 24 |
|
---|
| 25 | self.CreateWidgets()
|
---|
| 26 | self.title(title)
|
---|
| 27 | self.protocol("WM_DELETE_WINDOW", self.Ok)
|
---|
| 28 | self.parent = parent
|
---|
| 29 | self.textView.focus_set()
|
---|
| 30 | #key bindings for this dialog
|
---|
| 31 | self.bind('<Return>',self.Ok) #dismiss dialog
|
---|
| 32 | self.bind('<Escape>',self.Ok) #dismiss dialog
|
---|
| 33 | self.textView.insert(0.0, text)
|
---|
| 34 | self.textView.config(state=DISABLED)
|
---|
| 35 |
|
---|
[391] | 36 | if modal:
|
---|
| 37 | self.transient(parent)
|
---|
| 38 | self.grab_set()
|
---|
| 39 | self.wait_window()
|
---|
| 40 |
|
---|
[2] | 41 | def CreateWidgets(self):
|
---|
| 42 | frameText = Frame(self, relief=SUNKEN, height=700)
|
---|
| 43 | frameButtons = Frame(self)
|
---|
| 44 | self.buttonOk = Button(frameButtons, text='Close',
|
---|
| 45 | command=self.Ok, takefocus=FALSE)
|
---|
| 46 | self.scrollbarView = Scrollbar(frameText, orient=VERTICAL,
|
---|
| 47 | takefocus=FALSE, highlightthickness=0)
|
---|
| 48 | self.textView = Text(frameText, wrap=WORD, highlightthickness=0,
|
---|
| 49 | fg=self.fg, bg=self.bg)
|
---|
| 50 | self.scrollbarView.config(command=self.textView.yview)
|
---|
| 51 | self.textView.config(yscrollcommand=self.scrollbarView.set)
|
---|
| 52 | self.buttonOk.pack()
|
---|
| 53 | self.scrollbarView.pack(side=RIGHT,fill=Y)
|
---|
| 54 | self.textView.pack(side=LEFT,expand=TRUE,fill=BOTH)
|
---|
| 55 | frameButtons.pack(side=BOTTOM,fill=X)
|
---|
| 56 | frameText.pack(side=TOP,expand=TRUE,fill=BOTH)
|
---|
| 57 |
|
---|
| 58 | def Ok(self, event=None):
|
---|
| 59 | self.destroy()
|
---|
| 60 |
|
---|
| 61 |
|
---|
[391] | 62 | def view_text(parent, title, text, modal=True):
|
---|
| 63 | return TextViewer(parent, title, text, modal)
|
---|
[2] | 64 |
|
---|
[391] | 65 | def view_file(parent, title, filename, encoding=None, modal=True):
|
---|
[2] | 66 | try:
|
---|
| 67 | if encoding:
|
---|
| 68 | import codecs
|
---|
| 69 | textFile = codecs.open(filename, 'r')
|
---|
| 70 | else:
|
---|
| 71 | textFile = open(filename, 'r')
|
---|
| 72 | except IOError:
|
---|
| 73 | import tkMessageBox
|
---|
| 74 | tkMessageBox.showerror(title='File Load Error',
|
---|
| 75 | message='Unable to load file %r .' % filename,
|
---|
| 76 | parent=parent)
|
---|
| 77 | else:
|
---|
[391] | 78 | return view_text(parent, title, textFile.read(), modal)
|
---|
[2] | 79 |
|
---|
| 80 |
|
---|
| 81 | if __name__ == '__main__':
|
---|
| 82 | #test the dialog
|
---|
| 83 | root=Tk()
|
---|
| 84 | root.title('textView test')
|
---|
| 85 | filename = './textView.py'
|
---|
| 86 | text = file(filename, 'r').read()
|
---|
| 87 | btn1 = Button(root, text='view_text',
|
---|
[391] | 88 | command=lambda:view_text(root, 'view_text', text))
|
---|
[2] | 89 | btn1.pack(side=LEFT)
|
---|
| 90 | btn2 = Button(root, text='view_file',
|
---|
| 91 | command=lambda:view_file(root, 'view_file', filename))
|
---|
| 92 | btn2.pack(side=LEFT)
|
---|
[391] | 93 | btn3 = Button(root, text='nonmodal view_text',
|
---|
| 94 | command=lambda:view_text(root, 'nonmodal view_text', text,
|
---|
| 95 | modal=False))
|
---|
| 96 | btn3.pack(side=LEFT)
|
---|
[2] | 97 | close = Button(root, text='Close', command=root.destroy)
|
---|
| 98 | close.pack(side=RIGHT)
|
---|
| 99 | root.mainloop()
|
---|