rmt.py 4.42 KB
Newer Older
1
#! /usr/bin/env python
Guido van Rossum's avatar
Guido van Rossum committed
2 3 4 5 6 7 8 9 10 11

# A Python program implementing rmt, an application for remotely
# controlling other Tk applications.
# Cf. Ousterhout, Tcl and the Tk Toolkit, Figs. 27.5-8, pp. 273-276.

# Note that because of forward references in the original, we
# sometimes delay bindings until after the corresponding procedure is
# defined.  We also introduce names for some unnamed code blocks in
# the original because of restrictions on lambda forms in Python.

12 13
# XXX This should be written in a more Python-like style!!!

Guido van Rossum's avatar
Guido van Rossum committed
14
from Tkinter import *
15
import sys
Guido van Rossum's avatar
Guido van Rossum committed
16 17 18 19 20 21

# 1. Create basic application structure: menu bar on top of
# text widget, scrollbar on right.

root = Tk()
tk = root.tk
22 23 24
mBar = Frame(root, relief=RAISED, borderwidth=2)
mBar.pack(fill=X)

Guido van Rossum's avatar
Guido van Rossum committed
25
f = Frame(root)
26 27 28 29 30
f.pack(expand=1, fill=BOTH)
s = Scrollbar(f, relief=FLAT)
s.pack(side=RIGHT, fill=Y)
t = Text(f, relief=RAISED, borderwidth=2, yscrollcommand=s.set, setgrid=1)
t.pack(side=LEFT, fill=BOTH, expand=1)
31
t.tag_config('bold', font='-Adobe-Courier-Bold-R-Normal-*-120-*')
32 33
s['command'] = t.yview

Guido van Rossum's avatar
Guido van Rossum committed
34 35 36 37 38
root.title('Tk Remote Controller')
root.iconname('Tk Remote')

# 2. Create menu button and menus.

39 40
file = Menubutton(mBar, text='File', underline=0)
file.pack(side=LEFT)
Guido van Rossum's avatar
Guido van Rossum committed
41 42
file_m = Menu(file)
file['menu'] = file_m
43 44
file_m_apps = Menu(file_m, tearoff=0)
file_m.add_cascade(label='Select Application', underline=0,
45
                   menu=file_m_apps)
46
file_m.add_command(label='Quit', underline=0, command=sys.exit)
Guido van Rossum's avatar
Guido van Rossum committed
47 48 49 50 51 52 53

# 3. Create bindings for text widget to allow commands to be
# entered and information to be selected.  New characters
# can only be added at the end of the text (can't ever move
# insertion point).

def single1(e):
54 55 56 57 58
    x = e.x
    y = e.y
    t.setvar('tk_priv(selectMode)', 'char')
    t.mark_set('anchor', At(x, y))
    # Should focus W
Guido van Rossum's avatar
Guido van Rossum committed
59 60 61
t.bind('<1>', single1)

def double1(e):
62 63 64 65
    x = e.x
    y = e.y
    t.setvar('tk_priv(selectMode)', 'word')
    t.tk_textSelectTo(At(x, y))
Guido van Rossum's avatar
Guido van Rossum committed
66 67 68
t.bind('<Double-1>', double1)

def triple1(e):
69 70 71 72
    x = e.x
    y = e.y
    t.setvar('tk_priv(selectMode)', 'line')
    t.tk_textSelectTo(At(x, y))
Guido van Rossum's avatar
Guido van Rossum committed
73 74 75
t.bind('<Triple-1>', triple1)

def returnkey(e):
76 77
    t.insert(AtInsert(), '\n')
    invoke()
Guido van Rossum's avatar
Guido van Rossum committed
78 79 80
t.bind('<Return>', returnkey)

def controlv(e):
81 82 83 84
    t.insert(AtInsert(), t.selection_get())
    t.yview_pickplace(AtInsert())
    if t.index(AtInsert())[-2:] == '.0':
        invoke()
Guido van Rossum's avatar
Guido van Rossum committed
85 86 87 88 89 90
t.bind('<Control-v>', controlv)

# 4. Procedure to backspace over one character, as long as
# the character isn't part of the prompt.

def backspace(e):
91 92 93
    if t.index('promptEnd') != t.index('insert - 1 char'):
        t.delete('insert - 1 char', AtInsert())
        t.yview_pickplace(AtInsert())
Guido van Rossum's avatar
Guido van Rossum committed
94 95 96 97 98 99 100 101 102 103 104 105
t.bind('<BackSpace>', backspace)
t.bind('<Control-h>', backspace)
t.bind('<Delete>', backspace)


# 5. Procedure that's invoked when return is typed:  if
# there's not yet a complete command (e.g. braces are open)
# then do nothing.  Otherwise, execute command (locally or
# remotely), output the result or error message, and issue
# a new prompt.

def invoke():
106 107 108 109 110 111 112 113 114 115
    cmd = t.get('promptEnd + 1 char', AtInsert())
    if t.getboolean(tk.call('info', 'complete', cmd)): # XXX
        if app == root.winfo_name():
            msg = tk.call('eval', cmd) # XXX
        else:
            msg = t.send(app, cmd)
        if msg:
            t.insert(AtInsert(), msg + '\n')
        prompt()
    t.yview_pickplace(AtInsert())
Guido van Rossum's avatar
Guido van Rossum committed
116 117

def prompt():
118 119 120
    t.insert(AtInsert(), app + ': ')
    t.mark_set('promptEnd', 'insert - 1 char')
    t.tag_add('bold', 'insert linestart', 'promptEnd')
Guido van Rossum's avatar
Guido van Rossum committed
121 122 123 124 125 126

# 6. Procedure to select a new application.  Also changes
# the prompt on the current command line to reflect the new
# name.

def newApp(appName):
127 128 129 130 131
    global app
    app = appName
    t.delete('promptEnd linestart', 'promptEnd')
    t.insert('promptEnd', appName + ':')
    t.tag_add('bold', 'promptEnd linestart', 'promptEnd')
Guido van Rossum's avatar
Guido van Rossum committed
132 133

def fillAppsMenu():
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    file_m_apps.add('command')
    file_m_apps.delete(0, 'last')
    names = root.winfo_interps()
    names = map(None, names) # convert tuple to list
    names.sort()
    for name in names:
        try:
            root.send(name, 'winfo name .')
        except TclError:
            # Inoperative window -- ignore it
            pass
        else:
            file_m_apps.add_command(
                label=name,
                command=lambda name=name: newApp(name))
Guido van Rossum's avatar
Guido van Rossum committed
149 150 151 152 153 154

file_m_apps['postcommand'] = fillAppsMenu
mBar.tk_menuBar(file)

# 7. Miscellaneous initialization.

155
app = root.winfo_name()
Guido van Rossum's avatar
Guido van Rossum committed
156
prompt()
157
t.focus()
Guido van Rossum's avatar
Guido van Rossum committed
158 159

root.mainloop()