Home | History | Annotate | Download | only in idlelib
      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     """
     12     def __init__(self, parent, title, text, modal=True, _htest=False):
     13         """Show the given text in a scrollable window with a 'close' button
     14 
     15         If modal option set to False, user can interact with other windows,
     16         otherwise they will be unable to interact with other windows until
     17         the textview window is closed.
     18 
     19         _htest - bool; change box location when running htest.
     20         """
     21         Toplevel.__init__(self, parent)
     22         self.configure(borderwidth=5)
     23         # place dialog below parent if running htest
     24         self.geometry("=%dx%d+%d+%d" % (750, 500,
     25                            parent.winfo_rootx() + 10,
     26                            parent.winfo_rooty() + (10 if not _htest else 100)))
     27         #elguavas - config placeholders til config stuff completed
     28         self.bg = '#ffffff'
     29         self.fg = '#000000'
     30 
     31         self.CreateWidgets()
     32         self.title(title)
     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.textView.insert(0.0, text)
     40         self.textView.config(state=DISABLED)
     41 
     42         if modal:
     43             self.transient(parent)
     44             self.grab_set()
     45             self.wait_window()
     46 
     47     def CreateWidgets(self):
     48         frameText = Frame(self, relief=SUNKEN, height=700)
     49         frameButtons = Frame(self)
     50         self.buttonOk = Button(frameButtons, text='Close',
     51                                command=self.Ok, takefocus=FALSE)
     52         self.scrollbarView = Scrollbar(frameText, orient=VERTICAL,
     53                                        takefocus=FALSE, highlightthickness=0)
     54         self.textView = Text(frameText, wrap=WORD, highlightthickness=0,
     55                              fg=self.fg, bg=self.bg)
     56         self.scrollbarView.config(command=self.textView.yview)
     57         self.textView.config(yscrollcommand=self.scrollbarView.set)
     58         self.buttonOk.pack()
     59         self.scrollbarView.pack(side=RIGHT,fill=Y)
     60         self.textView.pack(side=LEFT,expand=TRUE,fill=BOTH)
     61         frameButtons.pack(side=BOTTOM,fill=X)
     62         frameText.pack(side=TOP,expand=TRUE,fill=BOTH)
     63 
     64     def Ok(self, event=None):
     65         self.destroy()
     66 
     67 
     68 def view_text(parent, title, text, modal=True):
     69     return TextViewer(parent, title, text, modal)
     70 
     71 def view_file(parent, title, filename, encoding=None, modal=True):
     72     try:
     73         if encoding:
     74             import codecs
     75             textFile = codecs.open(filename, 'r')
     76         else:
     77             textFile = open(filename, 'r')
     78     except IOError:
     79         tkMessageBox.showerror(title='File Load Error',
     80                                message='Unable to load file %r .' % filename,
     81                                parent=parent)
     82     except UnicodeDecodeError as err:
     83         showerror(title='Unicode Decode Error',
     84                   message=str(err),
     85                   parent=parent)
     86     else:
     87         return view_text(parent, title, textFile.read(), modal)
     88 
     89 
     90 if __name__ == '__main__':
     91     import unittest
     92     unittest.main('idlelib.idle_test.test_textview', verbosity=2, exit=False)
     93     from idlelib.idle_test.htest import run
     94     run(TextViewer)
     95