1 """Simple text browser for IDLE
8 class TextViewer(Toplevel
):
9 """A simple text viewer dialog for IDLE
12 def __init__(self
, parent
, title
, text
):
13 """Show the given text in a scrollable window with a 'close' button
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
27 self
.transient(parent
)
29 self
.protocol("WM_DELETE_WINDOW", self
.Ok
)
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
35 self
.textView
.insert(0.0, text
)
36 self
.textView
.config(state
=DISABLED
)
39 def CreateWidgets(self
):
40 frameText
= Frame(self
, relief
=SUNKEN
, height
=700)
41 frameButtons
= Frame(self
)
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
)
48 self
.scrollbarView
.config(command
=self
.textView
.yview
)
49 self
.textView
.config(yscrollcommand
=self
.scrollbarView
.set)
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
)
56 def Ok(self
, event
=None):
60 def view_text(parent
, title
, text
):
61 TextViewer(parent
, title
, text
)
63 def view_file(parent
, title
, filename
, encoding
=None):
67 textFile
= codecs
.open(filename
, 'r')
69 textFile
= open(filename
, 'r')
72 tkMessageBox
.showerror(title
='File Load Error',
73 message
='Unable to load file %r .' % filename
,
76 return view_text(parent
, title
, textFile
.read())
79 if __name__
== '__main__':
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
))
88 btn2
= Button(root
, text
='view_file',
89 command
=lambda:view_file(root
, 'view_file', filename
))
91 close
= Button(root
, text
='Close', command
=root
.destroy
)
92 close
.pack(side
=RIGHT
)