from Tkinter import Entry from string import split class CommandEntry(Entry): def __init__(self, parent, commands): Entry.__init__(self, parent) self.commands = commands # command shortcuts for command in commands.keys(): func = commands[command] while 1: command = command[:-1] if not command: break try: if commands[command]: commands[command] = None else: break except KeyError: commands[command] = func self.bind("", self.enter) def enter(self, event): words = split(self.get()) if words: command, words = words[0], words[1:] self.delete(0, 'end') func = None try: func = self.commands[command] if func: func(words) else: self.error("" % command) except KeyError: self.error("" % command) def clear(self): self.delete(0, "end") def set(self, message): self.clear() self.insert(0, message) def error(self, error): self.set(error) self.bind("", self.clear_error) def clear_error(self, event): self.clear() self.unbind("")