From f19ff1ea7dcc21776b939e02a481a1ddac3a8c8c Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Tue, 26 Oct 2010 10:39:14 +0000 Subject: Work a bit more on tkinter demos. --- Demo/tkinter/guido/AttrDialog.py | 450 ------------------------------------ Demo/tkinter/guido/ManPage.py | 220 ------------------ Demo/tkinter/guido/MimeViewer.py | 159 ------------- Demo/tkinter/guido/ShellWindow.py | 146 ------------ Demo/tkinter/guido/attr_dialog.py | 460 +++++++++++++++++++++++++++++++++++++ Demo/tkinter/guido/manpage.py | 215 +++++++++++++++++ Demo/tkinter/guido/mbox.py | 2 +- Demo/tkinter/guido/mimeviewer.py | 159 +++++++++++++ Demo/tkinter/guido/shell_window.py | 146 ++++++++++++ Demo/tkinter/guido/sortvisu.py | 74 +++--- Demo/tkinter/guido/ss1.py | 8 +- Demo/tkinter/guido/tkman.py | 8 +- Demo/tkinter/guido/wish.py | 14 +- 13 files changed, 1035 insertions(+), 1026 deletions(-) delete mode 100644 Demo/tkinter/guido/AttrDialog.py delete mode 100644 Demo/tkinter/guido/ManPage.py delete mode 100755 Demo/tkinter/guido/MimeViewer.py delete mode 100644 Demo/tkinter/guido/ShellWindow.py create mode 100644 Demo/tkinter/guido/attr_dialog.py create mode 100644 Demo/tkinter/guido/manpage.py create mode 100755 Demo/tkinter/guido/mimeviewer.py create mode 100644 Demo/tkinter/guido/shell_window.py diff --git a/Demo/tkinter/guido/AttrDialog.py b/Demo/tkinter/guido/AttrDialog.py deleted file mode 100644 index 7acd341..0000000 --- a/Demo/tkinter/guido/AttrDialog.py +++ /dev/null @@ -1,450 +0,0 @@ - -# The options of a widget are described by the following attributes -# of the Pack and Widget dialogs: -# -# Dialog.current: {name: value} -# -- changes during Widget's lifetime -# -# Dialog.options: {name: (default, klass)} -# -- depends on widget class only -# -# Dialog.classes: {klass: (v0, v1, v2, ...) | 'boolean' | 'other'} -# -- totally static, though different between PackDialog and WidgetDialog -# (but even that could be unified) - -from tkinter import * - -class Option: - - varclass = StringVar # May be overridden - - def __init__(self, dialog, option): - self.dialog = dialog - self.option = option - self.master = dialog.top - self.default, self.klass = dialog.options[option] - self.var = self.varclass(self.master) - self.frame = Frame(self.master) - self.frame.pack(fill=X) - self.label = Label(self.frame, text=(option + ":")) - self.label.pack(side=LEFT) - self.update() - self.addoption() - - def refresh(self): - self.dialog.refresh() - self.update() - - def update(self): - try: - self.current = self.dialog.current[self.option] - except KeyError: - self.current = self.default - self.var.set(self.current) - - def set(self, e=None): # Should be overridden - pass - -class BooleanOption(Option): - - varclass = BooleanVar - - def addoption(self): - self.button = Checkbutton(self.frame, - text='on/off', - onvalue=1, - offvalue=0, - variable=self.var, - relief=RAISED, - borderwidth=2, - command=self.set) - self.button.pack(side=RIGHT) - -class EnumOption(Option): - - def addoption(self): - self.button = Menubutton(self.frame, - textvariable=self.var, - relief=RAISED, borderwidth=2) - self.button.pack(side=RIGHT) - self.menu = Menu(self.button) - self.button['menu'] = self.menu - for v in self.dialog.classes[self.klass]: - self.menu.add_radiobutton( - label=v, - variable=self.var, - value=v, - command=self.set) - -class StringOption(Option): - - def addoption(self): - self.entry = Entry(self.frame, - textvariable=self.var, - width=10, - relief=SUNKEN, - borderwidth=2) - self.entry.pack(side=RIGHT, fill=X, expand=1) - self.entry.bind('', self.set) - -class ReadonlyOption(Option): - - def addoption(self): - self.label = Label(self.frame, textvariable=self.var, - anchor=E) - self.label.pack(side=RIGHT) - -class Dialog: - - def __init__(self, master): - self.master = master - self.fixclasses() - self.refresh() - self.top = Toplevel(self.master) - self.top.title(self.__class__.__name__) - self.top.minsize(1, 1) - self.addchoices() - - def refresh(self): pass # Must override - - def fixclasses(self): pass # May override - - def addchoices(self): - self.choices = {} - list = [] - for k, dc in self.options.items(): - list.append((k, dc)) - list.sort() - for k, (d, c) in list: - try: - cl = self.classes[c] - except KeyError: - cl = 'unknown' - if type(cl) is tuple: - cl = self.enumoption - elif cl == 'boolean': - cl = self.booleanoption - elif cl == 'readonly': - cl = self.readonlyoption - else: - cl = self.stringoption - self.choices[k] = cl(self, k) - - # Must override: - options = {} - classes = {} - - # May override: - booleanoption = BooleanOption - stringoption = StringOption - enumoption = EnumOption - readonlyoption = ReadonlyOption - -class PackDialog(Dialog): - - def __init__(self, widget): - self.widget = widget - Dialog.__init__(self, widget) - - def refresh(self): - self.current = self.widget.info() - self.current['.class'] = self.widget.winfo_class() - self.current['.name'] = self.widget._w - - class packoption: # Mix-in class - def set(self, e=None): - self.current = self.var.get() - try: - self.dialog.widget.pack(**{self.option: self.current}) - except TclError as msg: - print(msg) - self.refresh() - - class booleanoption(packoption, BooleanOption): pass - class enumoption(packoption, EnumOption): pass - class stringoption(packoption, StringOption): pass - class readonlyoption(packoption, ReadonlyOption): pass - - options = { - '.class': (None, 'Class'), - '.name': (None, 'Name'), - 'after': (None, 'Widget'), - 'anchor': ('center', 'Anchor'), - 'before': (None, 'Widget'), - 'expand': ('no', 'Boolean'), - 'fill': ('none', 'Fill'), - 'in': (None, 'Widget'), - 'ipadx': (0, 'Pad'), - 'ipady': (0, 'Pad'), - 'padx': (0, 'Pad'), - 'pady': (0, 'Pad'), - 'side': ('top', 'Side'), - } - - classes = { - 'Anchor': (N, NE, E, SE, S, SW, W, NW, CENTER), - 'Boolean': 'boolean', - 'Class': 'readonly', - 'Expand': 'boolean', - 'Fill': (NONE, X, Y, BOTH), - 'Name': 'readonly', - 'Pad': 'pixel', - 'Side': (TOP, RIGHT, BOTTOM, LEFT), - 'Widget': 'readonly', - } - -class RemotePackDialog(PackDialog): - - def __init__(self, master, app, widget): - self.master = master - self.app = app - self.widget = widget - self.refresh() - self.top = Toplevel(self.master) - self.top.title(self.app + ' PackDialog') - self.top.minsize(1, 1) - self.addchoices() - - def refresh(self): - try: - words = self.master.tk.splitlist( - self.master.send(self.app, - 'pack', - 'info', - self.widget)) - except TclError as msg: - print(msg) - return - dict = {} - for i in range(0, len(words), 2): - key = words[i][1:] - value = words[i+1] - dict[key] = value - dict['.class'] = self.master.send(self.app, - 'winfo', - 'class', - self.widget) - dict['.name'] = self.widget - self.current = dict - - class remotepackoption: # Mix-in class - def set(self, e=None): - self.current = self.var.get() - try: - self.dialog.master.send( - self.dialog.app, - 'pack', - 'config', - self.dialog.widget, - '-'+self.option, - self.dialog.master.tk.merge( - self.current)) - except TclError as msg: - print(msg) - self.refresh() - - class booleanoption(remotepackoption, BooleanOption): pass - class enumoption(remotepackoption, EnumOption): pass - class stringoption(remotepackoption, StringOption): pass - class readonlyoption(remotepackoption, ReadonlyOption): pass - -class WidgetDialog(Dialog): - - def __init__(self, widget): - self.widget = widget - self.klass = widget.winfo_class() - Dialog.__init__(self, widget) - - def fixclasses(self): - if self.klass in self.addclasses: - classes = {} - for c in (self.classes, - self.addclasses[self.klass]): - for k in c.keys(): - classes[k] = c[k] - self.classes = classes - - def refresh(self): - self.configuration = self.widget.config() - self.update() - self.current['.class'] = self.widget.winfo_class() - self.current['.name'] = self.widget._w - - def update(self): - self.current = {} - self.options = {} - for k, v in self.configuration.items(): - if len(v) > 4: - self.current[k] = v[4] - self.options[k] = v[3], v[2] # default, klass - self.options['.class'] = (None, 'Class') - self.options['.name'] = (None, 'Name') - - class widgetoption: # Mix-in class - def set(self, e=None): - self.current = self.var.get() - try: - self.dialog.widget[self.option] = self.current - except TclError as msg: - print(msg) - self.refresh() - - class booleanoption(widgetoption, BooleanOption): pass - class enumoption(widgetoption, EnumOption): pass - class stringoption(widgetoption, StringOption): pass - class readonlyoption(widgetoption, ReadonlyOption): pass - - # Universal classes - classes = { - 'Anchor': (N, NE, E, SE, S, SW, W, NW, CENTER), - 'Aspect': 'integer', - 'Background': 'color', - 'Bitmap': 'bitmap', - 'BorderWidth': 'pixel', - 'Class': 'readonly', - 'CloseEnough': 'double', - 'Command': 'command', - 'Confine': 'boolean', - 'Cursor': 'cursor', - 'CursorWidth': 'pixel', - 'DisabledForeground': 'color', - 'ExportSelection': 'boolean', - 'Font': 'font', - 'Foreground': 'color', - 'From': 'integer', - 'Geometry': 'geometry', - 'Height': 'pixel', - 'InsertWidth': 'time', - 'Justify': (LEFT, CENTER, RIGHT), - 'Label': 'string', - 'Length': 'pixel', - 'MenuName': 'widget', - 'Name': 'readonly', - 'OffTime': 'time', - 'OnTime': 'time', - 'Orient': (HORIZONTAL, VERTICAL), - 'Pad': 'pixel', - 'Relief': (RAISED, SUNKEN, FLAT, RIDGE, GROOVE), - 'RepeatDelay': 'time', - 'RepeatInterval': 'time', - 'ScrollCommand': 'command', - 'ScrollIncrement': 'pixel', - 'ScrollRegion': 'rectangle', - 'ShowValue': 'boolean', - 'SetGrid': 'boolean', - 'Sliderforeground': 'color', - 'SliderLength': 'pixel', - 'Text': 'string', - 'TickInterval': 'integer', - 'To': 'integer', - 'Underline': 'index', - 'Variable': 'variable', - 'Value': 'string', - 'Width': 'pixel', - 'Wrap': (NONE, CHAR, WORD), - } - - # Classes that (may) differ per widget type - _tristate = {'State': (NORMAL, ACTIVE, DISABLED)} - _bistate = {'State': (NORMAL, DISABLED)} - addclasses = { - 'Button': _tristate, - 'Radiobutton': _tristate, - 'Checkbutton': _tristate, - 'Entry': _bistate, - 'Text': _bistate, - 'Menubutton': _tristate, - 'Slider': _bistate, - } - -class RemoteWidgetDialog(WidgetDialog): - - def __init__(self, master, app, widget): - self.app = app - self.widget = widget - self.klass = master.send(self.app, - 'winfo', - 'class', - self.widget) - Dialog.__init__(self, master) - - def refresh(self): - try: - items = self.master.tk.splitlist( - self.master.send(self.app, - self.widget, - 'config')) - except TclError as msg: - print(msg) - return - dict = {} - for item in items: - words = self.master.tk.splitlist(item) - key = words[0][1:] - value = (key,) + words[1:] - dict[key] = value - self.configuration = dict - self.update() - self.current['.class'] = self.klass - self.current['.name'] = self.widget - - class remotewidgetoption: # Mix-in class - def set(self, e=None): - self.current = self.var.get() - try: - self.dialog.master.send( - self.dialog.app, - self.dialog.widget, - 'config', - '-'+self.option, - self.current) - except TclError as msg: - print(msg) - self.refresh() - - class booleanoption(remotewidgetoption, BooleanOption): pass - class enumoption(remotewidgetoption, EnumOption): pass - class stringoption(remotewidgetoption, StringOption): pass - class readonlyoption(remotewidgetoption, ReadonlyOption): pass - -def test(): - import sys - root = Tk() - root.minsize(1, 1) - if sys.argv[1:]: - remotetest(root, sys.argv[1]) - else: - frame = Frame(root, name='frame') - frame.pack(expand=1, fill=BOTH) - button = Button(frame, name='button', text='button') - button.pack(expand=1) - canvas = Canvas(frame, name='canvas') - canvas.pack() - fpd = PackDialog(frame) - fwd = WidgetDialog(frame) - bpd = PackDialog(button) - bwd = WidgetDialog(button) - cpd = PackDialog(canvas) - cwd = WidgetDialog(canvas) - root.mainloop() - -def remotetest(root, app): - from listtree import listtree - list = listtree(root, app) - list.bind('', opendialogs) - list.app = app # Pass it on to handler - -def opendialogs(e): - list = e.widget - sel = list.curselection() - for i in sel: - item = list.get(i) - widget = item.split()[0] - RemoteWidgetDialog(list, list.app, widget) - if widget == '.': continue - try: - RemotePackDialog(list, list.app, widget) - except TclError as msg: - print(msg) - -test() diff --git a/Demo/tkinter/guido/ManPage.py b/Demo/tkinter/guido/ManPage.py deleted file mode 100644 index d4b4abe..0000000 --- a/Demo/tkinter/guido/ManPage.py +++ /dev/null @@ -1,220 +0,0 @@ -# Widget to display a man page - -import re -from tkinter import * -from tkinter import _tkinter -from tkinter.scrolledtext import ScrolledText - -# XXX These fonts may have to be changed to match your system -BOLDFONT = '*-Courier-Bold-R-Normal-*-120-*' -ITALICFONT = '*-Courier-Medium-O-Normal-*-120-*' - -# XXX Recognizing footers is system dependent -# (This one works for IRIX 5.2 and Solaris 2.2) -footerprog = re.compile( - '^ Page [1-9][0-9]*[ \t]+\|^.*Last change:.*[1-9][0-9]*\n') -emptyprog = re.compile('^[ \t]*\n') -ulprog = re.compile('^[ \t]*[Xv!_][Xv!_ \t]*\n') - -# Basic Man Page class -- does not disable editing -class EditableManPage(ScrolledText): - - # Initialize instance - def __init__(self, master=None, **cnf): - # Initialize base class - ScrolledText.__init__(self, master, **cnf) - - # Define tags for formatting styles - self.tag_config('X', underline=1) - self.tag_config('!', font=BOLDFONT) - self.tag_config('_', font=ITALICFONT) - - # Set state to idle - self.fp = None - self.lineno = 0 - - # Test whether we are busy parsing a file - def busy(self): - return self.fp != None - - # Ensure we're not busy - def kill(self): - if self.busy(): - self._endparser() - - # Parse a file, in the background - def asyncparsefile(self, fp): - self._startparser(fp) - self.tk.createfilehandler(fp, _tkinter.READABLE, - self._filehandler) - - parsefile = asyncparsefile # Alias - - # I/O handler used by background parsing - def _filehandler(self, fp, mask): - nextline = self.fp.readline() - if not nextline: - self._endparser() - return - self._parseline(nextline) - - # Parse a file, now (cannot be aborted) - def syncparsefile(self, fp): - from select import select - def avail(fp=fp, tout=0.0, select=select): - return select([fp], [], [], tout)[0] - height = self.getint(self['height']) - self._startparser(fp) - while 1: - nextline = fp.readline() - if not nextline: - break - self._parseline(nextline) - self._endparser() - - # Initialize parsing from a particular file -- must not be busy - def _startparser(self, fp): - if self.busy(): - raise RuntimeError('startparser: still busy') - fp.fileno() # Test for file-ness - self.fp = fp - self.lineno = 0 - self.ok = 0 - self.empty = 0 - self.buffer = None - savestate = self['state'] - self['state'] = NORMAL - self.delete('1.0', END) - self['state'] = savestate - - # End parsing -- must be busy, need not be at EOF - def _endparser(self): - if not self.busy(): - raise RuntimeError('endparser: not busy') - if self.buffer: - self._parseline('') - try: - self.tk.deletefilehandler(self.fp) - except TclError as msg: - pass - self.fp.close() - self.fp = None - del self.ok, self.empty, self.buffer - - # Parse a single line - def _parseline(self, nextline): - if not self.buffer: - # Save this line -- we need one line read-ahead - self.buffer = nextline - return - if emptyprog.match(self.buffer): - # Buffered line was empty -- set a flag - self.empty = 1 - self.buffer = nextline - return - textline = self.buffer - if ulprog.match(nextline): - # Next line is properties for buffered line - propline = nextline - self.buffer = None - else: - # Next line is read-ahead - propline = None - self.buffer = nextline - if not self.ok: - # First non blank line after footer must be header - # -- skip that too - self.ok = 1 - self.empty = 0 - return - if footerprog.match(textline): - # Footer -- start skipping until next non-blank line - self.ok = 0 - self.empty = 0 - return - savestate = self['state'] - self['state'] = NORMAL - if TkVersion >= 4.0: - self.mark_set('insert', 'end-1c') - else: - self.mark_set('insert', END) - if self.empty: - # One or more previous lines were empty - # -- insert one blank line in the text - self._insert_prop('\n') - self.lineno = self.lineno + 1 - self.empty = 0 - if not propline: - # No properties - self._insert_prop(textline) - else: - # Search for properties - p = '' - j = 0 - for i in range(min(len(propline), len(textline))): - if propline[i] != p: - if j < i: - self._insert_prop(textline[j:i], p) - j = i - p = propline[i] - self._insert_prop(textline[j:]) - self.lineno = self.lineno + 1 - self['state'] = savestate - - # Insert a string at the end, with at most one property (tag) - def _insert_prop(self, str, prop = ' '): - here = self.index(AtInsert()) - self.insert(AtInsert(), str) - if TkVersion <= 4.0: - tags = self.tag_names(here) - for tag in tags: - self.tag_remove(tag, here, AtInsert()) - if prop != ' ': - self.tag_add(prop, here, AtInsert()) - -# Readonly Man Page class -- disables editing, otherwise the same -class ReadonlyManPage(EditableManPage): - - # Initialize instance - def __init__(self, master=None, **cnf): - cnf['state'] = DISABLED - EditableManPage.__init__(self, master, **cnf) - -# Alias -ManPage = ReadonlyManPage - -# Test program. -# usage: ManPage [manpage]; or ManPage [-f] file -# -f means that the file is nroff -man output run through ul -i -def test(): - import os - import sys - # XXX This directory may be different on your system - MANDIR = '' - DEFAULTPAGE = 'Tcl' - formatted = 0 - if sys.argv[1:] and sys.argv[1] == '-f': - formatted = 1 - del sys.argv[1] - if sys.argv[1:]: - name = sys.argv[1] - else: - name = DEFAULTPAGE - if not formatted: - if name[-2:-1] != '.': - name = name + '.n' - name = os.path.join(MANDIR, name) - root = Tk() - root.minsize(1, 1) - manpage = ManPage(root, relief=SUNKEN, borderwidth=2) - manpage.pack(expand=1, fill=BOTH) - if formatted: - fp = open(name, 'r') - else: - fp = os.popen('nroff -man %s | ul -i' % name, 'r') - manpage.parsefile(fp) - root.mainloop() - -# Run the test program when called as a script -if __name__ == '__main__': - test() diff --git a/Demo/tkinter/guido/MimeViewer.py b/Demo/tkinter/guido/MimeViewer.py deleted file mode 100755 index babed8f..0000000 --- a/Demo/tkinter/guido/MimeViewer.py +++ /dev/null @@ -1,159 +0,0 @@ -#! /usr/bin/env python3 - -# View a single MIME multipart message. -# Display each part as a box. - -import os -import sys -import getopt -import mailbox -from tkinter import * -from tkinter.scrolledtext import ScrolledText - -MBOXPATH = os.environ['HOME'] + '/Mail' - -class Error(Exception): - pass - -def getcurrent(self): - """Return the current message. Raise Error when there is none.""" - seqs = self.get_sequences() - try: - return max(seqs['cur']) - except (ValueError, KeyError): - raise Error("no cur message") - - -class MimeViewer: - def __init__(self, parent, title, msg): - self.title = title - self.msg = msg - self.frame = Frame(parent, {'relief': 'raised', 'bd': 2}) - self.frame.packing = {'expand': 0, 'fill': 'both'} - self.button = Checkbutton(self.frame, - {'text': title, - 'command': self.toggle}) - self.button.pack({'anchor': 'w'}) - headertext = [] - for item in msg.items(): - headertext.append("%s: %s" % item) - headertext = '\n'.join(headertext) - height = countlines(headertext, 4) - if height: - self.htext = ScrolledText(self.frame, - {'height': height, - 'width': 80, - 'wrap': 'none', - 'relief': 'raised', - 'bd': 2}) - self.htext.packing = {'expand': 1, 'fill': 'both', - 'after': self.button} - self.htext.insert('end', headertext) - else: - self.htext = Frame(self.frame, - {'relief': 'raised', 'bd': 2}) - self.htext.packing = {'side': 'top', - 'ipady': 2, - 'fill': 'x', - 'after': self.button} - body = msg.get_payload() - if type(body) == str: - self.pad = None - height = countlines(body, 10) - if height: - self.btext = ScrolledText(self.frame, - {'height': height, - 'width': 80, - 'wrap': 'none', - 'relief': 'raised', - 'bd': 2}) - self.btext.packing = {'expand': 1, - 'fill': 'both'} - self.btext.insert('end', body) - else: - self.btext = None - self.parts = None - else: - self.pad = Frame(self.frame, - {'relief': 'flat', 'bd': 2}) - self.pad.packing = {'side': 'left', 'ipadx': 10, - 'fill': 'y', 'after': self.htext} - self.parts = [] - for i in range(len(body)): - p = MimeViewer(self.frame, - '%s.%d' % (title, i+1), - body[i]) - self.parts.append(p) - self.btext = None - self.collapsed = 1 - def pack(self): - self.frame.pack(self.frame.packing) - def destroy(self): - self.frame.destroy() - def show(self): - if self.collapsed: - self.button.invoke() - def toggle(self): - if self.collapsed: - self.explode() - else: - self.collapse() - def collapse(self): - self.collapsed = 1 - for comp in self.htext, self.btext, self.pad: - if comp: - comp.forget() - if self.parts: - for part in self.parts: - part.frame.forget() - self.frame.pack({'expand': 0}) - def explode(self): - self.collapsed = 0 - for comp in self.htext, self.btext, self.pad: - if comp: comp.pack(comp.packing) - if self.parts: - for part in self.parts: - part.pack() - self.frame.pack({'expand': 1}) - -def countlines(str, limit): - i = 0 - n = 0 - while n < limit: - i = str.find('\n', i) - if i < 0: break - n = n+1 - i = i+1 - return n - -def main(): - opts, args = getopt.getopt(sys.argv[1:], '') - for o, a in opts: - pass - message = None - folder = 'inbox' - for arg in args: - if arg[:1] == '+': - folder = arg[1:] - else: - message = int(arg) - - mh = mailbox.MH(MBOXPATH) - f = mh.get_folder(folder) - if message is None: - message = getcurrent(f) - m = mailbox.MHMessage(f.get(message)) - - root = Tk() - tk = root.tk - - top = MimeViewer(root, '+%s/%d' % (folder, message), m) - top.pack() - top.show() - - root.minsize(1, 1) - - tk.mainloop() - -if __name__ == '__main__': - main() diff --git a/Demo/tkinter/guido/ShellWindow.py b/Demo/tkinter/guido/ShellWindow.py deleted file mode 100644 index c5a0401..0000000 --- a/Demo/tkinter/guido/ShellWindow.py +++ /dev/null @@ -1,146 +0,0 @@ -import os -import sys -from tkinter import * -from tkinter.scrolledtext import ScrolledText -from tkinter.dialog import Dialog -import signal - -BUFSIZE = 512 - -class ShellWindow(ScrolledText): - - def __init__(self, master=None, shell=None, **cnf): - if not shell: - try: - shell = os.environ['SHELL'] - except KeyError: - shell = '/bin/sh' - shell = shell + ' -i' - args = shell.split() - shell = args[0] - - ScrolledText.__init__(self, master, **cnf) - self.pos = '1.0' - self.bind('', self.inputhandler) - self.bind('', self.sigint) - self.bind('', self.sigterm) - self.bind('', self.sigkill) - self.bind('', self.sendeof) - - self.pid, self.fromchild, self.tochild = spawn(shell, args) - self.tk.createfilehandler(self.fromchild, READABLE, - self.outputhandler) - - def outputhandler(self, file, mask): - data = os.read(file, BUFSIZE).decode() - if not data: - self.tk.deletefilehandler(file) - pid, sts = os.waitpid(self.pid, 0) - print('pid', pid, 'status', sts) - self.pid = None - detail = sts>>8 - cause = sts & 0xff - if cause == 0: - msg = "exit status %d" % detail - else: - msg = "killed by signal %d" % (cause & 0x7f) - if cause & 0x80: - msg = msg + " -- core dumped" - Dialog(self.master, - text=msg, - title="Exit status", - bitmap='warning', - default=0, - strings=('OK',)) - return - self.insert(END, data) - self.pos = self.index("end - 1 char") - self.yview_pickplace(END) - - def inputhandler(self, *args): - if not self.pid: - self.no_process() - return "break" - self.insert(END, "\n") - line = self.get(self.pos, "end - 1 char") - self.pos = self.index(END) - os.write(self.tochild, line.encode()) - return "break" - - def sendeof(self, *args): - if not self.pid: - self.no_process() - return "break" - os.close(self.tochild) - return "break" - - def sendsig(self, sig): - if not self.pid: - self.no_process() - return "break" - os.kill(self.pid, sig) - return "break" - - def sigint(self, *args): - return self.sendsig(signal.SIGINT) - - def sigquit(self, *args): - return self.sendsig(signal.SIGQUIT) - - def sigterm(self, *args): - return self.sendsig(signal.SIGTERM) - - def sigkill(self, *args): - return self.sendsig(signal.SIGKILL) - - def no_process(self): - Dialog(self.master, - text="No active process", - title="No process", - bitmap='error', - default=0, - strings=('OK',)) - -MAXFD = 100 # Max number of file descriptors (os.getdtablesize()???) - -def spawn(prog, args): - p2cread, p2cwrite = os.pipe() - c2pread, c2pwrite = os.pipe() - pid = os.fork() - if pid == 0: - # Child - for i in 0, 1, 2: - try: - os.close(i) - except os.error: - pass - if os.dup(p2cread) != 0: - sys.stderr.write('popen2: bad read dup\n') - if os.dup(c2pwrite) != 1: - sys.stderr.write('popen2: bad write dup\n') - if os.dup(c2pwrite) != 2: - sys.stderr.write('popen2: bad write dup\n') - os.closerange(3, MAXFD) - try: - os.execvp(prog, args) - finally: - sys.stderr.write('execvp failed\n') - os._exit(1) - os.close(p2cread) - os.close(c2pwrite) - return pid, c2pread, p2cwrite - -def test(): - shell = ' '.join(sys.argv[1: ]) - root = Tk() - root.minsize(1, 1) - if shell: - w = ShellWindow(root, shell=shell) - else: - w = ShellWindow(root) - w.pack(expand=1, fill=BOTH) - w.focus_set() - w.tk.mainloop() - -if __name__ == '__main__': - test() diff --git a/Demo/tkinter/guido/attr_dialog.py b/Demo/tkinter/guido/attr_dialog.py new file mode 100644 index 0000000..229a558 --- /dev/null +++ b/Demo/tkinter/guido/attr_dialog.py @@ -0,0 +1,460 @@ + +# The options of a widget are described by the following attributes +# of the Pack and Widget dialogs: +# +# Dialog.current: {name: value} +# -- changes during Widget's lifetime +# +# Dialog.options: {name: (default, klass)} +# -- depends on widget class only +# +# Dialog.classes: {klass: (v0, v1, v2, ...) | 'boolean' | 'other'} +# -- totally static, though different between PackDialog and WidgetDialog +# (but even that could be unified) + +from tkinter import * + + +class Option: + + varclass = StringVar # May be overridden + + def __init__(self, dialog, option): + self.dialog = dialog + self.option = option + self.master = dialog.top + self.default, self.klass = dialog.options[option] + self.var = self.varclass(self.master) + self.frame = Frame(self.master) + self.frame.pack(fill=X) + self.label = Label(self.frame, text=(option + ":")) + self.label.pack(side=LEFT) + self.update() + self.addoption() + + def refresh(self): + self.dialog.refresh() + self.update() + + def update(self): + try: + self.current = self.dialog.current[self.option] + except KeyError: + self.current = self.default + self.var.set(self.current) + + def set(self, e=None): # Should be overridden + pass + + +class BooleanOption(Option): + + varclass = BooleanVar + + def addoption(self): + self.button = Checkbutton(self.frame, + text='on/off', + onvalue=1, + offvalue=0, + variable=self.var, + relief=RAISED, + borderwidth=2, + command=self.set) + self.button.pack(side=RIGHT) + + +class EnumOption(Option): + + def addoption(self): + self.button = Menubutton(self.frame, + textvariable=self.var, + relief=RAISED, borderwidth=2) + self.button.pack(side=RIGHT) + self.menu = Menu(self.button) + self.button['menu'] = self.menu + for v in self.dialog.classes[self.klass]: + self.menu.add_radiobutton( + label=v, + variable=self.var, + value=v, + command=self.set) + + +class StringOption(Option): + + def addoption(self): + self.entry = Entry(self.frame, + textvariable=self.var, + width=10, + relief=SUNKEN, + borderwidth=2) + self.entry.pack(side=RIGHT, fill=X, expand=1) + self.entry.bind('', self.set) + + +class ReadonlyOption(Option): + + def addoption(self): + self.label = Label(self.frame, textvariable=self.var, + anchor=E) + self.label.pack(side=RIGHT) + + +class Dialog: + + def __init__(self, master): + self.master = master + self.fixclasses() + self.refresh() + self.top = Toplevel(self.master) + self.top.title(self.__class__.__name__) + self.top.minsize(1, 1) + self.addchoices() + + def refresh(self): pass # Must override + + def fixclasses(self): pass # May override + + def addchoices(self): + self.choices = {} + list = [] + for k, dc in self.options.items(): + list.append((k, dc)) + list.sort() + for k, (d, c) in list: + try: + cl = self.classes[c] + except KeyError: + cl = 'unknown' + if type(cl) is tuple: + cl = self.enumoption + elif cl == 'boolean': + cl = self.booleanoption + elif cl == 'readonly': + cl = self.readonlyoption + else: + cl = self.stringoption + self.choices[k] = cl(self, k) + + # Must override: + options = {} + classes = {} + + # May override: + booleanoption = BooleanOption + stringoption = StringOption + enumoption = EnumOption + readonlyoption = ReadonlyOption + + +class PackDialog(Dialog): + + def __init__(self, widget): + self.widget = widget + Dialog.__init__(self, widget) + + def refresh(self): + self.current = self.widget.info() + self.current['.class'] = self.widget.winfo_class() + self.current['.name'] = self.widget._w + + class packoption: # Mix-in class + def set(self, e=None): + self.current = self.var.get() + try: + self.dialog.widget.pack(**{self.option: self.current}) + except TclError as msg: + print(msg) + self.refresh() + + class booleanoption(packoption, BooleanOption): pass + class enumoption(packoption, EnumOption): pass + class stringoption(packoption, StringOption): pass + class readonlyoption(packoption, ReadonlyOption): pass + + options = { + '.class': (None, 'Class'), + '.name': (None, 'Name'), + 'after': (None, 'Widget'), + 'anchor': ('center', 'Anchor'), + 'before': (None, 'Widget'), + 'expand': ('no', 'Boolean'), + 'fill': ('none', 'Fill'), + 'in': (None, 'Widget'), + 'ipadx': (0, 'Pad'), + 'ipady': (0, 'Pad'), + 'padx': (0, 'Pad'), + 'pady': (0, 'Pad'), + 'side': ('top', 'Side'), + } + + classes = { + 'Anchor': (N, NE, E, SE, S, SW, W, NW, CENTER), + 'Boolean': 'boolean', + 'Class': 'readonly', + 'Expand': 'boolean', + 'Fill': (NONE, X, Y, BOTH), + 'Name': 'readonly', + 'Pad': 'pixel', + 'Side': (TOP, RIGHT, BOTTOM, LEFT), + 'Widget': 'readonly', + } + +class RemotePackDialog(PackDialog): + + def __init__(self, master, app, widget): + self.master = master + self.app = app + self.widget = widget + self.refresh() + self.top = Toplevel(self.master) + self.top.title(self.app + ' PackDialog') + self.top.minsize(1, 1) + self.addchoices() + + def refresh(self): + try: + words = self.master.tk.splitlist( + self.master.send(self.app, + 'pack', + 'info', + self.widget)) + except TclError as msg: + print(msg) + return + dict = {} + for i in range(0, len(words), 2): + key = words[i][1:] + value = words[i+1] + dict[key] = value + dict['.class'] = self.master.send(self.app, + 'winfo', + 'class', + self.widget) + dict['.name'] = self.widget + self.current = dict + + class remotepackoption: # Mix-in class + def set(self, e=None): + self.current = self.var.get() + try: + self.dialog.master.send( + self.dialog.app, + 'pack', + 'config', + self.dialog.widget, + '-'+self.option, + self.dialog.master.tk.merge( + self.current)) + except TclError as msg: + print(msg) + self.refresh() + + class booleanoption(remotepackoption, BooleanOption): pass + class enumoption(remotepackoption, EnumOption): pass + class stringoption(remotepackoption, StringOption): pass + class readonlyoption(remotepackoption, ReadonlyOption): pass + + +class WidgetDialog(Dialog): + + def __init__(self, widget): + self.widget = widget + self.klass = widget.winfo_class() + Dialog.__init__(self, widget) + + def fixclasses(self): + if self.klass in self.addclasses: + classes = {} + for c in (self.classes, + self.addclasses[self.klass]): + for k in c.keys(): + classes[k] = c[k] + self.classes = classes + + def refresh(self): + self.configuration = self.widget.config() + self.update() + self.current['.class'] = self.widget.winfo_class() + self.current['.name'] = self.widget._w + + def update(self): + self.current = {} + self.options = {} + for k, v in self.configuration.items(): + if len(v) > 4: + self.current[k] = v[4] + self.options[k] = v[3], v[2] # default, klass + self.options['.class'] = (None, 'Class') + self.options['.name'] = (None, 'Name') + + class widgetoption: # Mix-in class + def set(self, e=None): + self.current = self.var.get() + try: + self.dialog.widget[self.option] = self.current + except TclError as msg: + print(msg) + self.refresh() + + class booleanoption(widgetoption, BooleanOption): pass + class enumoption(widgetoption, EnumOption): pass + class stringoption(widgetoption, StringOption): pass + class readonlyoption(widgetoption, ReadonlyOption): pass + + # Universal classes + classes = { + 'Anchor': (N, NE, E, SE, S, SW, W, NW, CENTER), + 'Aspect': 'integer', + 'Background': 'color', + 'Bitmap': 'bitmap', + 'BorderWidth': 'pixel', + 'Class': 'readonly', + 'CloseEnough': 'double', + 'Command': 'command', + 'Confine': 'boolean', + 'Cursor': 'cursor', + 'CursorWidth': 'pixel', + 'DisabledForeground': 'color', + 'ExportSelection': 'boolean', + 'Font': 'font', + 'Foreground': 'color', + 'From': 'integer', + 'Geometry': 'geometry', + 'Height': 'pixel', + 'InsertWidth': 'time', + 'Justify': (LEFT, CENTER, RIGHT), + 'Label': 'string', + 'Length': 'pixel', + 'MenuName': 'widget', + 'Name': 'readonly', + 'OffTime': 'time', + 'OnTime': 'time', + 'Orient': (HORIZONTAL, VERTICAL), + 'Pad': 'pixel', + 'Relief': (RAISED, SUNKEN, FLAT, RIDGE, GROOVE), + 'RepeatDelay': 'time', + 'RepeatInterval': 'time', + 'ScrollCommand': 'command', + 'ScrollIncrement': 'pixel', + 'ScrollRegion': 'rectangle', + 'ShowValue': 'boolean', + 'SetGrid': 'boolean', + 'Sliderforeground': 'color', + 'SliderLength': 'pixel', + 'Text': 'string', + 'TickInterval': 'integer', + 'To': 'integer', + 'Underline': 'index', + 'Variable': 'variable', + 'Value': 'string', + 'Width': 'pixel', + 'Wrap': (NONE, CHAR, WORD), + } + + # Classes that (may) differ per widget type + _tristate = {'State': (NORMAL, ACTIVE, DISABLED)} + _bistate = {'State': (NORMAL, DISABLED)} + addclasses = { + 'Button': _tristate, + 'Radiobutton': _tristate, + 'Checkbutton': _tristate, + 'Entry': _bistate, + 'Text': _bistate, + 'Menubutton': _tristate, + 'Slider': _bistate, + } + + +class RemoteWidgetDialog(WidgetDialog): + + def __init__(self, master, app, widget): + self.app = app + self.widget = widget + self.klass = master.send(self.app, + 'winfo', + 'class', + self.widget) + Dialog.__init__(self, master) + + def refresh(self): + try: + items = self.master.tk.splitlist( + self.master.send(self.app, + self.widget, + 'config')) + except TclError as msg: + print(msg) + return + dict = {} + for item in items: + words = self.master.tk.splitlist(item) + key = words[0][1:] + value = (key,) + words[1:] + dict[key] = value + self.configuration = dict + self.update() + self.current['.class'] = self.klass + self.current['.name'] = self.widget + + class remotewidgetoption: # Mix-in class + def set(self, e=None): + self.current = self.var.get() + try: + self.dialog.master.send( + self.dialog.app, + self.dialog.widget, + 'config', + '-'+self.option, + self.current) + except TclError as msg: + print(msg) + self.refresh() + + class booleanoption(remotewidgetoption, BooleanOption): pass + class enumoption(remotewidgetoption, EnumOption): pass + class stringoption(remotewidgetoption, StringOption): pass + class readonlyoption(remotewidgetoption, ReadonlyOption): pass + + +def test(): + import sys + root = Tk() + root.minsize(1, 1) + if sys.argv[1:]: + remotetest(root, sys.argv[1]) + else: + frame = Frame(root, name='frame') + frame.pack(expand=1, fill=BOTH) + button = Button(frame, name='button', text='button') + button.pack(expand=1) + canvas = Canvas(frame, name='canvas') + canvas.pack() + fpd = PackDialog(frame) + fwd = WidgetDialog(frame) + bpd = PackDialog(button) + bwd = WidgetDialog(button) + cpd = PackDialog(canvas) + cwd = WidgetDialog(canvas) + root.mainloop() + +def remotetest(root, app): + from listtree import listtree + list = listtree(root, app) + list.bind('', opendialogs) + list.app = app # Pass it on to handler + +def opendialogs(e): + list = e.widget + sel = list.curselection() + for i in sel: + item = list.get(i) + widget = item.split()[0] + RemoteWidgetDialog(list, list.app, widget) + if widget == '.': continue + try: + RemotePackDialog(list, list.app, widget) + except TclError as msg: + print(msg) + +test() diff --git a/Demo/tkinter/guido/manpage.py b/Demo/tkinter/guido/manpage.py new file mode 100644 index 0000000..750c675 --- /dev/null +++ b/Demo/tkinter/guido/manpage.py @@ -0,0 +1,215 @@ +# Widget to display a man page + +import os +import re +import sys + +from tkinter import * +from tkinter.font import Font +from tkinter.scrolledtext import ScrolledText + +# XXX Recognizing footers is system dependent +# (This one works for IRIX 5.2 and Solaris 2.2) +footerprog = re.compile( + '^ Page [1-9][0-9]*[ \t]+\|^.*Last change:.*[1-9][0-9]*\n') +emptyprog = re.compile('^[ \t]*\n') +ulprog = re.compile('^[ \t]*[Xv!_][Xv!_ \t]*\n') + + +class EditableManPage(ScrolledText): + """Basic Man Page class -- does not disable editing.""" + + def __init__(self, master=None, **cnf): + ScrolledText.__init__(self, master, **cnf) + + bold = Font(font=self['font']).copy() + bold.config(weight='bold') + italic = Font(font=self['font']).copy() + italic.config(slant='italic') + + # Define tags for formatting styles + self.tag_config('X', underline=1) + self.tag_config('!', font=bold) + self.tag_config('_', font=italic) + + # Set state to idle + self.fp = None + self.lineno = 0 + + def busy(self): + """Test whether we are busy parsing a file.""" + return self.fp != None + + def kill(self): + """Ensure we're not busy.""" + if self.busy(): + self._endparser() + + def asyncparsefile(self, fp): + """Parse a file, in the background.""" + self._startparser(fp) + self.tk.createfilehandler(fp, READABLE, + self._filehandler) + + parsefile = asyncparsefile # Alias + + def _filehandler(self, fp, mask): + """I/O handler used by background parsing.""" + nextline = self.fp.readline() + if not nextline: + self._endparser() + return + self._parseline(nextline) + + def syncparsefile(self, fp): + """Parse a file, now (cannot be aborted).""" + self._startparser(fp) + while True: + nextline = fp.readline() + if not nextline: + break + self._parseline(nextline) + self._endparser() + + def _startparser(self, fp): + """Initialize parsing from a particular file -- must not be busy.""" + if self.busy(): + raise RuntimeError('startparser: still busy') + fp.fileno() # Test for file-ness + self.fp = fp + self.lineno = 0 + self.ok = 0 + self.empty = 0 + self.buffer = None + savestate = self['state'] + self['state'] = NORMAL + self.delete('1.0', END) + self['state'] = savestate + + def _endparser(self): + """End parsing -- must be busy, need not be at EOF.""" + if not self.busy(): + raise RuntimeError('endparser: not busy') + if self.buffer: + self._parseline('') + try: + self.tk.deletefilehandler(self.fp) + except TclError: + pass + self.fp.close() + self.fp = None + del self.ok, self.empty, self.buffer + + def _parseline(self, nextline): + """Parse a single line.""" + if not self.buffer: + # Save this line -- we need one line read-ahead + self.buffer = nextline + return + if emptyprog.match(self.buffer): + # Buffered line was empty -- set a flag + self.empty = 1 + self.buffer = nextline + return + textline = self.buffer + if ulprog.match(nextline): + # Next line is properties for buffered line + propline = nextline + self.buffer = None + else: + # Next line is read-ahead + propline = None + self.buffer = nextline + if not self.ok: + # First non blank line after footer must be header + # -- skip that too + self.ok = 1 + self.empty = 0 + return + if footerprog.match(textline): + # Footer -- start skipping until next non-blank line + self.ok = 0 + self.empty = 0 + return + savestate = self['state'] + self['state'] = NORMAL + if TkVersion >= 4.0: + self.mark_set('insert', 'end-1c') + else: + self.mark_set('insert', END) + if self.empty: + # One or more previous lines were empty + # -- insert one blank line in the text + self._insert_prop('\n') + self.lineno = self.lineno + 1 + self.empty = 0 + if not propline: + # No properties + self._insert_prop(textline) + else: + # Search for properties + p = '' + j = 0 + for i in range(min(len(propline), len(textline))): + if propline[i] != p: + if j < i: + self._insert_prop(textline[j:i], p) + j = i + p = propline[i] + self._insert_prop(textline[j:]) + self.lineno = self.lineno + 1 + self['state'] = savestate + + def _insert_prop(self, str, prop = ' '): + """Insert a string at the end, with at most one property (tag).""" + here = self.index(AtInsert()) + self.insert(AtInsert(), str) + if TkVersion <= 4.0: + tags = self.tag_names(here) + for tag in tags: + self.tag_remove(tag, here, AtInsert()) + if prop != ' ': + self.tag_add(prop, here, AtInsert()) + + +class ReadonlyManPage(EditableManPage): + """Readonly Man Page class -- disables editing, otherwise the same.""" + + def __init__(self, master=None, **cnf): + cnf['state'] = DISABLED + EditableManPage.__init__(self, master, **cnf) + +# Alias +ManPage = ReadonlyManPage + +# usage: ManPage [manpage]; or ManPage [-f] file +# -f means that the file is nroff -man output run through ul -i +def main(): + # XXX This directory may be different on your system + MANDIR = '' + DEFAULTPAGE = 'Tcl' + formatted = 0 + if sys.argv[1:] and sys.argv[1] == '-f': + formatted = 1 + del sys.argv[1] + if sys.argv[1:]: + name = sys.argv[1] + else: + name = DEFAULTPAGE + if not formatted: + if name[-2:-1] != '.': + name = name + '.n' + name = os.path.join(MANDIR, name) + root = Tk() + root.minsize(1, 1) + manpage = ManPage(root, relief=SUNKEN, borderwidth=2) + manpage.pack(expand=1, fill=BOTH) + if formatted: + fp = open(name, 'r') + else: + fp = os.popen('nroff -man -c %s | ul -i' % name, 'r') + manpage.parsefile(fp) + root.mainloop() + +if __name__ == '__main__': + main() diff --git a/Demo/tkinter/guido/mbox.py b/Demo/tkinter/guido/mbox.py index ccd6581..0a421cf 100755 --- a/Demo/tkinter/guido/mbox.py +++ b/Demo/tkinter/guido/mbox.py @@ -192,7 +192,7 @@ def open_message(e=None): num = int(m.group(1)) m = mhf.get_message(num) if viewer: viewer.destroy() - from MimeViewer import MimeViewer + from mimeviewer import MimeViewer viewer = MimeViewer(bot, '+%s/%d' % (folder, num), m) viewer.pack() viewer.show() diff --git a/Demo/tkinter/guido/mimeviewer.py b/Demo/tkinter/guido/mimeviewer.py new file mode 100755 index 0000000..babed8f --- /dev/null +++ b/Demo/tkinter/guido/mimeviewer.py @@ -0,0 +1,159 @@ +#! /usr/bin/env python3 + +# View a single MIME multipart message. +# Display each part as a box. + +import os +import sys +import getopt +import mailbox +from tkinter import * +from tkinter.scrolledtext import ScrolledText + +MBOXPATH = os.environ['HOME'] + '/Mail' + +class Error(Exception): + pass + +def getcurrent(self): + """Return the current message. Raise Error when there is none.""" + seqs = self.get_sequences() + try: + return max(seqs['cur']) + except (ValueError, KeyError): + raise Error("no cur message") + + +class MimeViewer: + def __init__(self, parent, title, msg): + self.title = title + self.msg = msg + self.frame = Frame(parent, {'relief': 'raised', 'bd': 2}) + self.frame.packing = {'expand': 0, 'fill': 'both'} + self.button = Checkbutton(self.frame, + {'text': title, + 'command': self.toggle}) + self.button.pack({'anchor': 'w'}) + headertext = [] + for item in msg.items(): + headertext.append("%s: %s" % item) + headertext = '\n'.join(headertext) + height = countlines(headertext, 4) + if height: + self.htext = ScrolledText(self.frame, + {'height': height, + 'width': 80, + 'wrap': 'none', + 'relief': 'raised', + 'bd': 2}) + self.htext.packing = {'expand': 1, 'fill': 'both', + 'after': self.button} + self.htext.insert('end', headertext) + else: + self.htext = Frame(self.frame, + {'relief': 'raised', 'bd': 2}) + self.htext.packing = {'side': 'top', + 'ipady': 2, + 'fill': 'x', + 'after': self.button} + body = msg.get_payload() + if type(body) == str: + self.pad = None + height = countlines(body, 10) + if height: + self.btext = ScrolledText(self.frame, + {'height': height, + 'width': 80, + 'wrap': 'none', + 'relief': 'raised', + 'bd': 2}) + self.btext.packing = {'expand': 1, + 'fill': 'both'} + self.btext.insert('end', body) + else: + self.btext = None + self.parts = None + else: + self.pad = Frame(self.frame, + {'relief': 'flat', 'bd': 2}) + self.pad.packing = {'side': 'left', 'ipadx': 10, + 'fill': 'y', 'after': self.htext} + self.parts = [] + for i in range(len(body)): + p = MimeViewer(self.frame, + '%s.%d' % (title, i+1), + body[i]) + self.parts.append(p) + self.btext = None + self.collapsed = 1 + def pack(self): + self.frame.pack(self.frame.packing) + def destroy(self): + self.frame.destroy() + def show(self): + if self.collapsed: + self.button.invoke() + def toggle(self): + if self.collapsed: + self.explode() + else: + self.collapse() + def collapse(self): + self.collapsed = 1 + for comp in self.htext, self.btext, self.pad: + if comp: + comp.forget() + if self.parts: + for part in self.parts: + part.frame.forget() + self.frame.pack({'expand': 0}) + def explode(self): + self.collapsed = 0 + for comp in self.htext, self.btext, self.pad: + if comp: comp.pack(comp.packing) + if self.parts: + for part in self.parts: + part.pack() + self.frame.pack({'expand': 1}) + +def countlines(str, limit): + i = 0 + n = 0 + while n < limit: + i = str.find('\n', i) + if i < 0: break + n = n+1 + i = i+1 + return n + +def main(): + opts, args = getopt.getopt(sys.argv[1:], '') + for o, a in opts: + pass + message = None + folder = 'inbox' + for arg in args: + if arg[:1] == '+': + folder = arg[1:] + else: + message = int(arg) + + mh = mailbox.MH(MBOXPATH) + f = mh.get_folder(folder) + if message is None: + message = getcurrent(f) + m = mailbox.MHMessage(f.get(message)) + + root = Tk() + tk = root.tk + + top = MimeViewer(root, '+%s/%d' % (folder, message), m) + top.pack() + top.show() + + root.minsize(1, 1) + + tk.mainloop() + +if __name__ == '__main__': + main() diff --git a/Demo/tkinter/guido/shell_window.py b/Demo/tkinter/guido/shell_window.py new file mode 100644 index 0000000..c5a0401 --- /dev/null +++ b/Demo/tkinter/guido/shell_window.py @@ -0,0 +1,146 @@ +import os +import sys +from tkinter import * +from tkinter.scrolledtext import ScrolledText +from tkinter.dialog import Dialog +import signal + +BUFSIZE = 512 + +class ShellWindow(ScrolledText): + + def __init__(self, master=None, shell=None, **cnf): + if not shell: + try: + shell = os.environ['SHELL'] + except KeyError: + shell = '/bin/sh' + shell = shell + ' -i' + args = shell.split() + shell = args[0] + + ScrolledText.__init__(self, master, **cnf) + self.pos = '1.0' + self.bind('', self.inputhandler) + self.bind('', self.sigint) + self.bind('', self.sigterm) + self.bind('', self.sigkill) + self.bind('', self.sendeof) + + self.pid, self.fromchild, self.tochild = spawn(shell, args) + self.tk.createfilehandler(self.fromchild, READABLE, + self.outputhandler) + + def outputhandler(self, file, mask): + data = os.read(file, BUFSIZE).decode() + if not data: + self.tk.deletefilehandler(file) + pid, sts = os.waitpid(self.pid, 0) + print('pid', pid, 'status', sts) + self.pid = None + detail = sts>>8 + cause = sts & 0xff + if cause == 0: + msg = "exit status %d" % detail + else: + msg = "killed by signal %d" % (cause & 0x7f) + if cause & 0x80: + msg = msg + " -- core dumped" + Dialog(self.master, + text=msg, + title="Exit status", + bitmap='warning', + default=0, + strings=('OK',)) + return + self.insert(END, data) + self.pos = self.index("end - 1 char") + self.yview_pickplace(END) + + def inputhandler(self, *args): + if not self.pid: + self.no_process() + return "break" + self.insert(END, "\n") + line = self.get(self.pos, "end - 1 char") + self.pos = self.index(END) + os.write(self.tochild, line.encode()) + return "break" + + def sendeof(self, *args): + if not self.pid: + self.no_process() + return "break" + os.close(self.tochild) + return "break" + + def sendsig(self, sig): + if not self.pid: + self.no_process() + return "break" + os.kill(self.pid, sig) + return "break" + + def sigint(self, *args): + return self.sendsig(signal.SIGINT) + + def sigquit(self, *args): + return self.sendsig(signal.SIGQUIT) + + def sigterm(self, *args): + return self.sendsig(signal.SIGTERM) + + def sigkill(self, *args): + return self.sendsig(signal.SIGKILL) + + def no_process(self): + Dialog(self.master, + text="No active process", + title="No process", + bitmap='error', + default=0, + strings=('OK',)) + +MAXFD = 100 # Max number of file descriptors (os.getdtablesize()???) + +def spawn(prog, args): + p2cread, p2cwrite = os.pipe() + c2pread, c2pwrite = os.pipe() + pid = os.fork() + if pid == 0: + # Child + for i in 0, 1, 2: + try: + os.close(i) + except os.error: + pass + if os.dup(p2cread) != 0: + sys.stderr.write('popen2: bad read dup\n') + if os.dup(c2pwrite) != 1: + sys.stderr.write('popen2: bad write dup\n') + if os.dup(c2pwrite) != 2: + sys.stderr.write('popen2: bad write dup\n') + os.closerange(3, MAXFD) + try: + os.execvp(prog, args) + finally: + sys.stderr.write('execvp failed\n') + os._exit(1) + os.close(p2cread) + os.close(c2pwrite) + return pid, c2pread, p2cwrite + +def test(): + shell = ' '.join(sys.argv[1: ]) + root = Tk() + root.minsize(1, 1) + if shell: + w = ShellWindow(root, shell=shell) + else: + w = ShellWindow(root) + w.pack(expand=1, fill=BOTH) + w.focus_set() + w.tk.mainloop() + +if __name__ == '__main__': + test() diff --git a/Demo/tkinter/guido/sortvisu.py b/Demo/tkinter/guido/sortvisu.py index cbc911d..4173121 100644 --- a/Demo/tkinter/guido/sortvisu.py +++ b/Demo/tkinter/guido/sortvisu.py @@ -18,7 +18,6 @@ stand-alone application. """ - from tkinter import * import random @@ -201,27 +200,28 @@ class ArrayItem: self.value = value self.canvas = array.canvas x1, y1, x2, y2 = self.position() - self.item = array.canvas.create_rectangle(x1, y1, x2, y2, + self.item_id = array.canvas.create_rectangle(x1, y1, x2, y2, fill='red', outline='black', width=1) - array.canvas.tag_bind(self.item, '', self.mouse_down) - array.canvas.tag_bind(self.item, '', self.mouse_move) - array.canvas.tag_bind(self.item, '', self.mouse_up) + self.canvas.tag_bind(self.item_id, '', self.mouse_down) + self.canvas.tag_bind(self.item_id, '', self.mouse_move) + self.canvas.tag_bind(self.item_id, '', self.mouse_up) def delete(self): - item = self.item + item_id = self.item_id self.array = None - self.item = None - item.delete() + self.item_id = None + self.canvas.delete(item_id) def mouse_down(self, event): self.lastx = event.x self.lasty = event.y self.origx = event.x self.origy = event.y - self.item.tkraise() + self.canvas.tag_raise(self.item_id) def mouse_move(self, event): - self.item.move(event.x - self.lastx, event.y - self.lasty) + self.canvas.move(self.item_id, + event.x - self.lastx, event.y - self.lasty) self.lastx = event.x self.lasty = event.y @@ -236,7 +236,7 @@ class ArrayItem: self.array.items[here], self.array.items[i] = other, self self.index = i x1, y1, x2, y2 = self.position() - self.canvas.coords(self.item, (x1, y1, x2, y2)) + self.canvas.coords(self.item_id, (x1, y1, x2, y2)) other.setindex(here) def setindex(self, index): @@ -248,9 +248,9 @@ class ArrayItem: self.index = index newpts = self.position() trajectory = interpolate(oldpts, newpts, nsteps) - self.item.tkraise() + self.canvas.tag_raise(self.item_id) for pts in trajectory: - self.canvas.coords(self.item, pts) + self.canvas.coords(self.item_id, pts) self.array.wait(50) def swapwith(self, other): @@ -263,45 +263,45 @@ class ArrayItem: self.index, other.index = other.index, self.index mynewpts = self.position() othernewpts = other.position() - myfill = self.canvas.itemcget(self.item, 'fill') - otherfill = self.canvas.itemcget(other.item, 'fill') - self.canvas.itemconfig(self.item, fill='green') - self.canvas.itemconfig(other.item, fill='yellow') + myfill = self.canvas.itemcget(self.item_id, 'fill') + otherfill = self.canvas.itemcget(other.item_id, 'fill') + self.canvas.itemconfig(self.item_id, fill='green') + self.canvas.itemconfig(other.item_id, fill='yellow') self.array.master.update() if self.array.speed == "single-step": - self.canvas.coords(self.item, mynewpts) - self.canvas.coords(other.item, othernewpts) + self.canvas.coords(self.item_id, mynewpts) + self.canvas.coords(other.item_id, othernewpts) self.array.master.update() - self.canvas.itemconfig(self.item, fill=myfill) - self.canvas.itemconfig(other.item, fill=otherfill) + self.canvas.itemconfig(self.item_id, fill=myfill) + self.canvas.itemconfig(other.item_id, fill=otherfill) self.array.wait(0) return mytrajectory = interpolate(myoldpts, mynewpts, nsteps) othertrajectory = interpolate(otheroldpts, othernewpts, nsteps) if self.value > other.value: - self.canvas.tag_raise(self.item) - self.canvas.tag_raise(other.item) + self.canvas.tag_raise(self.item_id) + self.canvas.tag_raise(other.item_id) else: - self.canvas.tag_raise(other.item) - self.canvas.tag_raise(self.item) + self.canvas.tag_raise(other.item_id) + self.canvas.tag_raise(self.item_id) try: for i in range(len(mytrajectory)): mypts = mytrajectory[i] otherpts = othertrajectory[i] - self.canvas.coords(self.item, mypts) - self.canvas.coords(other.item, otherpts) + self.canvas.coords(self.item_id, mypts) + self.canvas.coords(other.item_id, otherpts) self.array.wait(50) finally: mypts = mytrajectory[-1] otherpts = othertrajectory[-1] - self.canvas.coords(self.item, mypts) - self.canvas.coords(other.item, otherpts) - self.canvas.itemconfig(self.item, fill=myfill) - self.canvas.itemconfig(other.item, fill=otherfill) + self.canvas.coords(self.item_id, mypts) + self.canvas.coords(other.item_id, otherpts) + self.canvas.itemconfig(self.item_id, fill=myfill) + self.canvas.itemconfig(other.item_id, fill=otherfill) def compareto(self, other): - myfill = self.canvas.itemcget(self.item, 'fill') - otherfill = self.canvas.itemcget(other.item, 'fill') + myfill = self.canvas.itemcget(self.item_id, 'fill') + otherfill = self.canvas.itemcget(other.item_id, 'fill') if self.value < other.value: myflash = 'white' otherflash = 'black' @@ -314,12 +314,12 @@ class ArrayItem: myflash = otherflash = 'grey' outcome = 0 try: - self.canvas.itemconfig(self.item, fill=myflash) - self.canvas.itemconfig(other.item, fill=otherflash) + self.canvas.itemconfig(self.item_id, fill=myflash) + self.canvas.itemconfig(other.item_id, fill=otherflash) self.array.wait(500) finally: - self.canvas.itemconfig(self.item, fill=myfill) - self.canvas.itemconfig(other.item, fill=otherfill) + self.canvas.itemconfig(self.item_id, fill=myfill) + self.canvas.itemconfig(other.item_id, fill=otherfill) return outcome def position(self): diff --git a/Demo/tkinter/guido/ss1.py b/Demo/tkinter/guido/ss1.py index a6c8c21..8b07489 100644 --- a/Demo/tkinter/guido/ss1.py +++ b/Demo/tkinter/guido/ss1.py @@ -3,7 +3,7 @@ import os import re import sys -import cgi +import html from xml.parsers import expat LEFT, CENTER, RIGHT = "LEFT", "CENTER", "RIGHT" @@ -201,7 +201,7 @@ class Sheet: if hasattr(cell, 'xml'): cellxml = cell.xml() else: - cellxml = '%s' % cgi.escape(cell) + cellxml = '%s' % html.escape(cell) out.append('\n %s\n' % (y, x, cellxml)) out.append('') @@ -216,7 +216,7 @@ class Sheet: f.close() def load(self, filename): - f = open(filename, 'r') + f = open(filename, 'rb') SheetParser(self).parsefile(f) f.close() @@ -382,7 +382,7 @@ class StringCell(BaseCell): return s % ( align2xml[self.alignment], self.fmt, - cgi.escape(self.text)) + html.escape(self.text)) class FormulaCell(BaseCell): diff --git a/Demo/tkinter/guido/tkman.py b/Demo/tkinter/guido/tkman.py index c50258e..c7081a6 100755 --- a/Demo/tkinter/guido/tkman.py +++ b/Demo/tkinter/guido/tkman.py @@ -7,10 +7,10 @@ import re import sys from tkinter import * -from ManPage import ManPage +from manpage import ManPage -MANNDIRLIST = ['/depot/sundry/man/mann','/usr/local/man/mann'] -MAN3DIRLIST = ['/depot/sundry/man/man3','/usr/local/man/man3'] +MANNDIRLIST = ['/usr/local/man/mann', '/usr/share/man/mann'] +MAN3DIRLIST = ['/usr/local/man/man3', '/usr/share/man/man3'] foundmanndir = 0 for dir in MANNDIRLIST: @@ -197,7 +197,7 @@ class SelectionBox: def show_page(self, name): file = '%s/%s.?' % (self.chaptervar.get(), name) - fp = os.popen('nroff -man %s | ul -i' % file, 'r') + fp = os.popen('nroff -man -c %s | ul -i' % file, 'r') self.text.kill() self.title['text'] = name self.text.parsefile(fp) diff --git a/Demo/tkinter/guido/wish.py b/Demo/tkinter/guido/wish.py index bebab1e..332501d 100644 --- a/Demo/tkinter/guido/wish.py +++ b/Demo/tkinter/guido/wish.py @@ -4,21 +4,25 @@ import _tkinter import os import sys -tk = _tkinter.create(os.environ['DISPLAY'], 'wish', 'Tk', 1) +tk = _tkinter.create(os.environ['DISPLAY'], 'wish', 'Tk', 1, 1) tk.call('update') cmd = '' -while 1: - if cmd: prompt = '' - else: prompt = '% ' +while True: + if cmd: + prompt = '' + else: + prompt = '% ' try: sys.stdout.write(prompt) sys.stdout.flush() line = sys.stdin.readline() + if not line: + break except EOFError: break - cmd = cmd + (line + '\n') + cmd += line if tk.getboolean(tk.call('info', 'complete', cmd)): tk.record(line) try: -- cgit v0.12