Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1 | #! /ufs/guido/bin/sgi/tkpython |
| 2 | |
| 3 | # A Python program implementing rmt, an application for remotely |
| 4 | # controlling other Tk applications. |
| 5 | # Cf. Ousterhout, Tcl and the Tk Toolkit, Figs. 27.5-8, pp. 273-276. |
| 6 | |
| 7 | # Note that because of forward references in the original, we |
| 8 | # sometimes delay bindings until after the corresponding procedure is |
| 9 | # defined. We also introduce names for some unnamed code blocks in |
| 10 | # the original because of restrictions on lambda forms in Python. |
| 11 | |
| 12 | from Tkinter import * |
| 13 | |
| 14 | # 1. Create basic application structure: menu bar on top of |
| 15 | # text widget, scrollbar on right. |
| 16 | |
| 17 | root = Tk() |
| 18 | tk = root.tk |
| 19 | mBar = Frame(root, {'relief': 'raised', 'bd': 2, |
| 20 | Pack: {'side': 'top', 'fill': 'x'}}) |
| 21 | f = Frame(root) |
| 22 | f.pack({'expand': 1, 'fill': 'both'}) |
| 23 | s = Scrollbar(f, {'relief': 'flat', |
| 24 | Pack: {'side': 'right', 'fill': 'y'}}) |
| 25 | t = Text(f, {'relief': 'raised', 'bd': 2, 'yscrollcommand': (s, 'set'), |
| 26 | 'setgrid': 1, |
| 27 | Pack: {'side': 'left', 'fill': 'both', 'expand': 1}}) |
| 28 | |
| 29 | t.tag_config('bold', {'font': '-Adobe-Courier-Bold-R-Normal-*-120-*'}) |
| 30 | s['command'] = (t, 'yview') |
| 31 | root.title('Tk Remote Controller') |
| 32 | root.iconname('Tk Remote') |
| 33 | |
| 34 | # 2. Create menu button and menus. |
| 35 | |
| 36 | file = Menubutton(mBar, {'text': 'File', 'underline': 0, |
| 37 | Pack: {'side': 'left'}}) |
| 38 | file_m = Menu(file) |
| 39 | file['menu'] = file_m |
| 40 | file_m_apps = Menu(file_m) |
| 41 | file_m.add('cascade', {'label': 'Select Application', 'underline': 0, |
| 42 | 'menu': file_m_apps}) |
| 43 | file_m.add('command', {'label': 'Quit', 'underline': 0, 'command': 'exit'}) |
| 44 | |
| 45 | # 3. Create bindings for text widget to allow commands to be |
| 46 | # entered and information to be selected. New characters |
| 47 | # can only be added at the end of the text (can't ever move |
| 48 | # insertion point). |
| 49 | |
| 50 | def single1(e): |
| 51 | x = e.x |
| 52 | y = e.y |
Guido van Rossum | df09691 | 1994-06-20 09:08:51 +0000 | [diff] [blame] | 53 | t.setvar('tk_priv(selectMode)', 'char') |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 54 | t.mark_set('anchor', At(x, y)) |
| 55 | # Should focus W |
| 56 | t.bind('<1>', single1) |
| 57 | |
| 58 | def double1(e): |
| 59 | x = e.x |
| 60 | y = e.y |
Guido van Rossum | df09691 | 1994-06-20 09:08:51 +0000 | [diff] [blame] | 61 | t.setvar('tk_priv(selectMode)', 'word') |
| 62 | t.tk_textSelectTo(At(x, y)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 63 | t.bind('<Double-1>', double1) |
| 64 | |
| 65 | def triple1(e): |
| 66 | x = e.x |
| 67 | y = e.y |
Guido van Rossum | df09691 | 1994-06-20 09:08:51 +0000 | [diff] [blame] | 68 | t.setvar('tk_priv(selectMode)', 'line') |
| 69 | t.tk_textSelectTo(At(x, y)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 70 | t.bind('<Triple-1>', triple1) |
| 71 | |
| 72 | def returnkey(e): |
Guido van Rossum | df09691 | 1994-06-20 09:08:51 +0000 | [diff] [blame] | 73 | t.insert(AtInsert(), '\n') |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 74 | invoke() |
| 75 | t.bind('<Return>', returnkey) |
| 76 | |
| 77 | def controlv(e): |
Guido van Rossum | df09691 | 1994-06-20 09:08:51 +0000 | [diff] [blame] | 78 | t.insert(AtInsert(), t.selection_get()) |
| 79 | t.yview_pickplace(AtInsert()) |
| 80 | if t.index(AtInsert())[-2:] == '.0': |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 81 | invoke() |
| 82 | t.bind('<Control-v>', controlv) |
| 83 | |
| 84 | # 4. Procedure to backspace over one character, as long as |
| 85 | # the character isn't part of the prompt. |
| 86 | |
| 87 | def backspace(e): |
| 88 | if t.index('promptEnd') != t.index('insert - 1 char'): |
Guido van Rossum | df09691 | 1994-06-20 09:08:51 +0000 | [diff] [blame] | 89 | t.delete('insert - 1 char', AtInsert()) |
| 90 | t.yview_pickplace(AtInsert()) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 91 | t.bind('<BackSpace>', backspace) |
| 92 | t.bind('<Control-h>', backspace) |
| 93 | t.bind('<Delete>', backspace) |
| 94 | |
| 95 | |
| 96 | # 5. Procedure that's invoked when return is typed: if |
| 97 | # there's not yet a complete command (e.g. braces are open) |
| 98 | # then do nothing. Otherwise, execute command (locally or |
| 99 | # remotely), output the result or error message, and issue |
| 100 | # a new prompt. |
| 101 | |
| 102 | def invoke(): |
Guido van Rossum | df09691 | 1994-06-20 09:08:51 +0000 | [diff] [blame] | 103 | cmd = t.get('promptEnd + 1 char', AtInsert()) |
| 104 | if t.getboolean(tk.call('info', 'complete', cmd)): # XXX |
| 105 | if app == root.winfo_name(): |
| 106 | msg = tk.call('eval', cmd) # XXX |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 107 | else: |
Guido van Rossum | df09691 | 1994-06-20 09:08:51 +0000 | [diff] [blame] | 108 | msg = t.send(app, cmd) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 109 | if msg: |
Guido van Rossum | df09691 | 1994-06-20 09:08:51 +0000 | [diff] [blame] | 110 | t.insert(AtInsert(), msg + '\n') |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 111 | prompt() |
Guido van Rossum | df09691 | 1994-06-20 09:08:51 +0000 | [diff] [blame] | 112 | t.yview_pickplace(AtInsert()) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 113 | |
| 114 | def prompt(): |
Guido van Rossum | df09691 | 1994-06-20 09:08:51 +0000 | [diff] [blame] | 115 | t.insert(AtInsert(), app + ': ') |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 116 | t.mark_set('promptEnd', 'insert - 1 char') |
| 117 | t.tag_add('bold', 'insert linestart', 'promptEnd') |
| 118 | |
| 119 | # 6. Procedure to select a new application. Also changes |
| 120 | # the prompt on the current command line to reflect the new |
| 121 | # name. |
| 122 | |
| 123 | def newApp(appName): |
| 124 | global app |
| 125 | app = appName |
| 126 | t.delete('promptEnd linestart', 'promptEnd') |
| 127 | t.insert('promptEnd', appName + ':') |
| 128 | t.tag_add('bold', 'promptEnd linestart', 'promptEnd') |
| 129 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 130 | def fillAppsMenu(): |
| 131 | file_m_apps.add('command') |
| 132 | file_m_apps.delete(0, 'last') |
Guido van Rossum | df09691 | 1994-06-20 09:08:51 +0000 | [diff] [blame] | 133 | names = root.winfo_interps() |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 134 | names = map(None, names) # convert tuple to list |
| 135 | names.sort() |
| 136 | for name in names: |
Guido van Rossum | df09691 | 1994-06-20 09:08:51 +0000 | [diff] [blame] | 137 | try: |
| 138 | root.send(name, 'winfo name .') |
| 139 | except TclError: |
| 140 | # Inoperative window -- ignore it |
| 141 | pass |
| 142 | else: |
| 143 | file_m_apps.add('command', {'label': name, |
| 144 | 'command': |
| 145 | lambda name=name: |
| 146 | newApp(name)}) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 147 | |
| 148 | file_m_apps['postcommand'] = fillAppsMenu |
| 149 | mBar.tk_menuBar(file) |
| 150 | |
| 151 | # 7. Miscellaneous initialization. |
| 152 | |
Guido van Rossum | df09691 | 1994-06-20 09:08:51 +0000 | [diff] [blame] | 153 | app = root.winfo_name() |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 154 | prompt() |
Guido van Rossum | df09691 | 1994-06-20 09:08:51 +0000 | [diff] [blame] | 155 | t.focus() |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 156 | |
| 157 | root.mainloop() |