#!/usr/bin/env python
#
# $Id: displaygrid.py,v 1.4 2001/11/03 11:05:22 doughellmann Exp $
#
# Copyright 2001 Doug Hellmann.
#
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
"""A widget to display a collection of sequences in a grid.
"""
__rcs_info__ = {
#
# Creation Information
#
'module_name' : '$RCSfile: displaygrid.py,v $',
'rcs_id' : '$Id: displaygrid.py,v 1.4 2001/11/03 11:05:22 doughellmann Exp $',
'creator' : 'Doug Hellmann <doug@hellfly.net>',
'project' : 'PmwContribD',
'created' : 'Sat, 05-May-2001 17:06:42 EDT',
#
# Current Information
#
'author' : '$Author: doughellmann $',
'version' : '$Revision: 1.4 $',
'date' : '$Date: 2001/11/03 11:05:22 $',
}
#
# Import system modules
#
import Tkinter, Pmw
#
# Import Local modules
#
#
# Module
#
class DisplayGrid(Pmw.LabeledWidget):
"Widget to show tabular data."
def __init__(self, parent=None, **kw):
"""Create a DisplayGrid.
Options
'ipadx' -- Internal X padding.
'ipady' -- Internal Y padding.
'columnlabels' -- Sequence of labels to use for the column
headings. Required.
'rowlabels' -- Sequence of labels to use for rows.
Optional.
'cellvaluefunc' -- Function which returns the value of a
cell (see below).
Cell Value Function Interface
The 'DisplayGrid' retrieves values for a cell one at a time.
To do this, the user must supply a 'cellvaluefunc' when the
widget is instantiated. The function provided must accept
two arguments, 'row' and 'column'.
For example::
def cellValueFunc(row, column):
return str(row + column)
"""
optiondefs=(
('ipadx', 0, Pmw.INITOPT),
('ipady', 0, Pmw.INITOPT),
('columnlabels', (), Pmw.INITOPT),
('rowlabels', None, Pmw.INITOPT),
('cellvaluefunc', None, Pmw.INITOPT),
)
self.defineoptions(kw, optiondefs)
Pmw.LabeledWidget.__init__(self, parent=parent)
#
# Make the column container
#
self.columnFrames = []
#
# Create a column with row labels
#
if self['rowlabels']:
self.rowLabelColumn = self.newColumn(' ')
for rowLabel in self['rowlabels']:
self.makeCell(self.rowLabelColumn,
text=' %s ' % (rowLabel),
relief=Tkinter.RAISED)
self.rowLabelColumn.pack(side=Tkinter.LEFT,
expand=Tkinter.YES,
fill=Tkinter.Y,
padx=self['ipadx'],
pady=self['ipady'])
#
# Create the columns of labels
#
for columnlabel in self['columnlabels']:
newColumn = self.newColumn(columnlabel)
self.columnFrames.append(newColumn)
#
# Create the cells for the data
#
for columnNumber in range(len(self['columnlabels'])):
for rowNumber in range(len(self['rowlabels'])):
lbl = self.makeCell(self.columnFrames[columnNumber],
self.cellValueGet(rowNumber, columnNumber),
relief=Tkinter.SUNKEN
)
self.columnFrames[columnNumber].rowContents.append(lbl)
#
# Pack columns
#
for i in range(0, len(self['columnlabels'])):
self.columnFrames[i].pack(side=Tkinter.LEFT,
expand=Tkinter.YES,
fill=Tkinter.Y)
# Check for initialization options
# for this class
self.initialiseoptions(DisplayGrid)
def cellValueGet(self, row, column):
if self['cellvaluefunc']:
val = self['cellvaluefunc'](row, column)
else:
val = None
return val
def newColumn(self, label=' '):
"Create a new column, with a label."
#
# Create the frame
#
newCol = Tkinter.Frame(self.interior())
#
# Add an attribute for the rowContents
#
newCol.rowContents = []
#
# Make the label cell
#
self.makeCell(newCol, text=' %s ' % label, relief=Tkinter.RAISED)
#
# Return the frame
#
return newCol
def makeCell(self, master,
text=' ',
side=Tkinter.TOP,
expand=Tkinter.NO,
fill=Tkinter.X,
relief=Tkinter.GROOVE,
bd=1,
**kw):
"""
Create a cell in a column with:
master: the frame representing the column
text: the value which should be displayed
side, expand, fill, relief, bd: standard Tkinter pack options
other options: other options to be sent to configure for the
new cell widget
"""
newLbl = Tkinter.Label(master, text=text, relief=relief, bd=bd)
newLbl.configure(kw)
newLbl.pack(side=side, expand=expand, fill=fill)
return newLbl
def setValue(self, row, column, newVal):
"""Set the value displayed in a specific (row, column)
of the DisplayGrid.
"""
self.columnFrames[column].rowContents[row].configure(text=newVal)
if __name__ == '__main__':
import GuiAppD
class TDGApp(GuiAppD.GuiAppD):
appname='displaygrid.py Demo Program'
def createInterface(self):
self.grid = DisplayGrid(self.interior(),
rowlabels=('A', 'B', 'C'),
columnlabels=('1', '2', '3'),
cellvaluefunc=self.cellvaluefunc,
)
self.grid.pack()
def cellvaluefunc(self, row, column):
val = str(row * column)
#print 'cell value for %d %d: %s' % (row, column, val)
return val
TDGApp().run()
|