Work a bit more on tkinter demos.

This commit is contained in:
Georg Brandl 2010-10-26 10:39:14 +00:00
parent 19208900f4
commit f19ff1ea7d
9 changed files with 99 additions and 90 deletions

View File

@ -14,6 +14,7 @@
from tkinter import *
class Option:
varclass = StringVar # May be overridden
@ -45,6 +46,7 @@ class Option:
def set(self, e=None): # Should be overridden
pass
class BooleanOption(Option):
varclass = BooleanVar
@ -60,6 +62,7 @@ class BooleanOption(Option):
command=self.set)
self.button.pack(side=RIGHT)
class EnumOption(Option):
def addoption(self):
@ -76,6 +79,7 @@ class EnumOption(Option):
value=v,
command=self.set)
class StringOption(Option):
def addoption(self):
@ -87,6 +91,7 @@ class StringOption(Option):
self.entry.pack(side=RIGHT, fill=X, expand=1)
self.entry.bind('<Return>', self.set)
class ReadonlyOption(Option):
def addoption(self):
@ -94,6 +99,7 @@ class ReadonlyOption(Option):
anchor=E)
self.label.pack(side=RIGHT)
class Dialog:
def __init__(self, master):
@ -140,6 +146,7 @@ class Dialog:
enumoption = EnumOption
readonlyoption = ReadonlyOption
class PackDialog(Dialog):
def __init__(self, widget):
@ -248,6 +255,7 @@ class RemotePackDialog(PackDialog):
class stringoption(remotepackoption, StringOption): pass
class readonlyoption(remotepackoption, ReadonlyOption): pass
class WidgetDialog(Dialog):
def __init__(self, widget):
@ -357,6 +365,7 @@ class WidgetDialog(Dialog):
'Slider': _bistate,
}
class RemoteWidgetDialog(WidgetDialog):
def __init__(self, master, app, widget):
@ -407,6 +416,7 @@ class RemoteWidgetDialog(WidgetDialog):
class stringoption(remotewidgetoption, StringOption): pass
class readonlyoption(remotewidgetoption, ReadonlyOption): pass
def test():
import sys
root = Tk()

View File

@ -1,13 +1,12 @@
# Widget to display a man page
import os
import re
from tkinter import *
from tkinter import _tkinter
from tkinter.scrolledtext import ScrolledText
import sys
# XXX These fonts may have to be changed to match your system
BOLDFONT = '*-Courier-Bold-R-Normal-*-120-*'
ITALICFONT = '*-Courier-Medium-O-Normal-*-120-*'
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)
@ -16,64 +15,64 @@ footerprog = re.compile(
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
class EditableManPage(ScrolledText):
"""Basic Man Page class -- does not disable editing."""
def __init__(self, master=None, **cnf):
# Initialize base class
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=BOLDFONT)
self.tag_config('_', font=ITALICFONT)
self.tag_config('!', font=bold)
self.tag_config('_', font=italic)
# Set state to idle
self.fp = None
self.lineno = 0
# Test whether we are busy parsing a file
def busy(self):
"""Test whether we are busy parsing a file."""
return self.fp != None
# Ensure we're not busy
def kill(self):
"""Ensure we're not busy."""
if self.busy():
self._endparser()
# Parse a file, in the background
def asyncparsefile(self, fp):
"""Parse a file, in the background."""
self._startparser(fp)
self.tk.createfilehandler(fp, _tkinter.READABLE,
self.tk.createfilehandler(fp, READABLE,
self._filehandler)
parsefile = asyncparsefile # Alias
# I/O handler used by background parsing
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)
# 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'])
"""Parse a file, now (cannot be aborted)."""
self._startparser(fp)
while 1:
while True:
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):
"""Initialize parsing from a particular file -- must not be busy."""
if self.busy():
raise RuntimeError('startparser: still busy')
fp.fileno() # Test for file-ness
@ -87,22 +86,22 @@ class EditableManPage(ScrolledText):
self.delete('1.0', END)
self['state'] = savestate
# End parsing -- must be busy, need not be at EOF
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 as msg:
except TclError:
pass
self.fp.close()
self.fp = None
del self.ok, self.empty, self.buffer
# Parse a single line
def _parseline(self, nextline):
"""Parse a single line."""
if not self.buffer:
# Save this line -- we need one line read-ahead
self.buffer = nextline
@ -161,8 +160,8 @@ class EditableManPage(ScrolledText):
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 = ' '):
"""Insert a string at the end, with at most one property (tag)."""
here = self.index(AtInsert())
self.insert(AtInsert(), str)
if TkVersion <= 4.0:
@ -172,10 +171,10 @@ class EditableManPage(ScrolledText):
if prop != ' ':
self.tag_add(prop, here, AtInsert())
# Readonly Man Page class -- disables editing, otherwise the same
class ReadonlyManPage(EditableManPage):
# Initialize instance
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)
@ -183,12 +182,9 @@ class ReadonlyManPage(EditableManPage):
# 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
def main():
# XXX This directory may be different on your system
MANDIR = ''
DEFAULTPAGE = 'Tcl'
@ -211,10 +207,9 @@ def test():
if formatted:
fp = open(name, 'r')
else:
fp = os.popen('nroff -man %s | ul -i' % name, 'r')
fp = os.popen('nroff -man -c %s | ul -i' % name, 'r')
manpage.parsefile(fp)
root.mainloop()
# Run the test program when called as a script
if __name__ == '__main__':
test()
main()

View File

@ -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()

View File

@ -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, '<Button-1>', self.mouse_down)
array.canvas.tag_bind(self.item, '<Button1-Motion>', self.mouse_move)
array.canvas.tag_bind(self.item, '<ButtonRelease-1>', self.mouse_up)
self.canvas.tag_bind(self.item_id, '<Button-1>', self.mouse_down)
self.canvas.tag_bind(self.item_id, '<Button1-Motion>', self.mouse_move)
self.canvas.tag_bind(self.item_id, '<ButtonRelease-1>', 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):

View File

@ -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 = '<value>%s</value>' % cgi.escape(cell)
cellxml = '<value>%s</value>' % html.escape(cell)
out.append('<cell row="%s" col="%s">\n %s\n</cell>' %
(y, x, cellxml))
out.append('</spreadsheet>')
@ -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):

View File

@ -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)

View File

@ -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: