| 1 | title = 'Pmw.HistoryText demonstration' |
| 2 | |
| 3 | # Import Pmw from this directory tree. |
| 4 | import sys |
| 5 | sys.path[:0] = ['../../..'] |
| 6 | |
| 7 | import Tkinter |
| 8 | import Pmw |
| 9 | |
| 10 | class Demo: |
| 11 | def __init__(self, parent): |
| 12 | # Create and pack the PanedWidget to hold the query and result |
| 13 | # windows. |
| 14 | # !! panedwidget should automatically size to requested size |
| 15 | panedWidget = Pmw.PanedWidget(parent, |
| 16 | orient = 'vertical', |
| 17 | hull_height = 400, |
| 18 | hull_width = 550) |
| 19 | panedWidget.add('query', min = 0.05, size = 0.2) |
| 20 | panedWidget.add('buttons', min = 0.1, max = 0.1) |
| 21 | panedWidget.add('results', min = 0.05) |
| 22 | panedWidget.pack(fill = 'both', expand = 1) |
| 23 | |
| 24 | # Create and pack the HistoryText. |
| 25 | self.historyText = Pmw.HistoryText(panedWidget.pane('query'), |
| 26 | text_wrap = 'none', |
| 27 | text_width = 60, |
| 28 | text_height = 10, |
| 29 | historycommand = self.statechange, |
| 30 | ) |
| 31 | self.historyText.pack(fill = 'both', expand = 1) |
| 32 | self.historyText.component('text').focus() |
| 33 | |
| 34 | buttonList = ( |
| 35 | [20, None], |
| 36 | ['Clear', self.clear], |
| 37 | ['Undo', self.historyText.undo], |
| 38 | ['Redo', self.historyText.redo], |
| 39 | [20, None], |
| 40 | ['Prev', self.historyText.prev], |
| 41 | ['Next', self.historyText.next], |
| 42 | [30, None], |
| 43 | ['Execute', Pmw.busycallback(self.executeQuery)], |
| 44 | ) |
| 45 | self.buttonDict = {} |
| 46 | |
| 47 | buttonFrame = panedWidget.pane('buttons') |
| 48 | for text, cmd in buttonList: |
| 49 | if type(text) == type(69): |
| 50 | frame = Tkinter.Frame(buttonFrame, width = text) |
| 51 | frame.pack(side = 'left') |
| 52 | else: |
| 53 | button = Tkinter.Button(buttonFrame, text = text, command = cmd) |
| 54 | button.pack(side = 'left') |
| 55 | self.buttonDict[text] = button |
| 56 | |
| 57 | for text in ('Prev', 'Next'): |
| 58 | self.buttonDict[text].configure(state = 'disabled') |
| 59 | |
| 60 | self.results = Pmw.ScrolledText(panedWidget.pane('results'), text_wrap = 'none') |
| 61 | self.results.pack(fill = 'both', expand = 1) |
| 62 | |
| 63 | def statechange(self, prevstate, nextstate): |
| 64 | self.buttonDict['Prev'].configure(state = prevstate) |
| 65 | self.buttonDict['Next'].configure(state = nextstate) |
| 66 | |
| 67 | def clear(self): |
| 68 | self.historyText.delete('1.0', 'end') |
| 69 | |
| 70 | def addnewlines(self, text): |
| 71 | if len(text) == 1: |
| 72 | text = text + '\n' |
| 73 | if text[-1] != '\n': |
| 74 | text = text + '\n' |
| 75 | if text[-2] != '\n': |
| 76 | text = text + '\n' |
| 77 | return text |
| 78 | |
| 79 | def executeQuery(self): |
| 80 | sql = self.historyText.get() |
| 81 | self.results.insert('end', 'Query:\n' + self.addnewlines(sql)) |
| 82 | self.results.see('end') |
| 83 | self.results.update_idletasks() |
| 84 | self.historyText.addhistory() |
| 85 | results = 'Results:\nfoo' |
| 86 | if len(results) > 0: |
| 87 | self.results.insert('end', self.addnewlines(results)) |
| 88 | self.results.see('end') |
| 89 | |
| 90 | |
| 91 | ###################################################################### |
| 92 | |
| 93 | # Create demo in root window for testing. |
| 94 | if __name__ == '__main__': |
| 95 | root = Tkinter.Tk() |
| 96 | Pmw.initialise(root) |
| 97 | root.title(title) |
| 98 | |
| 99 | exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy) |
| 100 | exitButton.pack(side = 'bottom') |
| 101 | widget = Demo(root) |
| 102 | root.mainloop() |