Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | """Simple text browser for IDLE |
2 | ||
3 | """ | |
4 | ||
5 | from Tkinter import * | |
6 | import tkMessageBox | |
7 | ||
8 | class TextViewer(Toplevel): | |
9 | """ | |
10 | simple text viewer dialog for idle | |
11 | """ | |
12 | def __init__(self, parent, title, fileName, data=None): | |
13 | """If data exists, load it into viewer, otherwise try to load file. | |
14 | ||
15 | fileName - string, should be an absoulute filename | |
16 | """ | |
17 | Toplevel.__init__(self, parent) | |
18 | self.configure(borderwidth=5) | |
19 | self.geometry("=%dx%d+%d+%d" % (625, 500, | |
20 | parent.winfo_rootx() + 10, | |
21 | parent.winfo_rooty() + 10)) | |
22 | #elguavas - config placeholders til config stuff completed | |
23 | self.bg = '#ffffff' | |
24 | self.fg = '#000000' | |
25 | ||
26 | self.CreateWidgets() | |
27 | self.title(title) | |
28 | self.transient(parent) | |
29 | self.grab_set() | |
30 | self.protocol("WM_DELETE_WINDOW", self.Ok) | |
31 | self.parent = parent | |
32 | self.textView.focus_set() | |
33 | #key bindings for this dialog | |
34 | self.bind('<Return>',self.Ok) #dismiss dialog | |
35 | self.bind('<Escape>',self.Ok) #dismiss dialog | |
36 | if data: | |
37 | self.textView.insert(0.0, data) | |
38 | else: | |
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 %r .' % (fileName,)) | |
50 | else: | |
51 | self.textView.insert(0.0,textFile.read()) | |
52 | ||
53 | def CreateWidgets(self): | |
54 | frameText = Frame(self, relief=SUNKEN, height=700) | |
55 | frameButtons = Frame(self) | |
56 | self.buttonOk = Button(frameButtons, text='Close', | |
57 | command=self.Ok, takefocus=FALSE) | |
58 | self.scrollbarView = Scrollbar(frameText, orient=VERTICAL, | |
59 | takefocus=FALSE, highlightthickness=0) | |
60 | self.textView = Text(frameText, wrap=WORD, highlightthickness=0, | |
61 | fg=self.fg, bg=self.bg) | |
62 | self.scrollbarView.config(command=self.textView.yview) | |
63 | self.textView.config(yscrollcommand=self.scrollbarView.set) | |
64 | self.buttonOk.pack() | |
65 | self.scrollbarView.pack(side=RIGHT,fill=Y) | |
66 | self.textView.pack(side=LEFT,expand=TRUE,fill=BOTH) | |
67 | frameButtons.pack(side=BOTTOM,fill=X) | |
68 | frameText.pack(side=TOP,expand=TRUE,fill=BOTH) | |
69 | ||
70 | def Ok(self, event=None): | |
71 | self.destroy() | |
72 | ||
73 | if __name__ == '__main__': | |
74 | #test the dialog | |
75 | root=Tk() | |
76 | Button(root,text='View', | |
77 | command=lambda:TextViewer(root,'Text','./textView.py')).pack() | |
78 | root.mainloop() |