table widget?

Michael Lunnay mlunnay at bigpond.net.au
Thu May 24 04:34:18 EDT 2001


I have just created a simple, static table. It is not a Tkinter subclass,
but it may be a start or may even fit your needs as is. I will look at
creating a Pmw widget when this semester finishes and make it available,
untill then see if you can use this (please excuse lack of doc strings or
comments, but it should be pretty straight forward):

from Tkinter import *
import Pmw

class Table:
    def __init__(self, parent, headings):
        self._rows = 0
        self._headings = headings
        self._cells = []
        self._table = Pmw.ScrolledFrame(parent)
        self._table.pack(padx = 5, fill='both', expand = 1)
 self._frame = self._table.interior()
        self._row = 1

        col = 0
        for h in headings:
            Label(self._frame, text=h, relief=RAISED,
borderwidth=2).grid(row = 0, column = col, sticky = 'nsew')

            self._frame.grid_rowconfigure(0, weight = 1)
            self._frame.grid_columnconfigure(col, weight = 1)
            self._table.reposition()
            col = col + 1

        self._table.configure(horizflex = 'expand')

    def addRow(self, data, color='#ffffff'):
        col = 0
        self._cells.append([])
        for d in data:
            l = Label(self._frame, text=d, relief=SOLID, borderwidth=1,
background=color, padx=2, pady=2)
            l.grid(row=self._row, column=col, sticky = 'nsew')

            self._frame.grid_rowconfigure(self._row, weight = 1)
            self._frame.grid_columnconfigure(col, weight = 1)
            self._table.reposition()

            self._cells[self._row - 1].append(l)

            col = col + 1

        self._row = self._row + 1

    def getData(self, row, col):
        return self.cells[row][col]['text']

    def setData(self, row, col, data):
        print "%d %d, %s" %(row, col, data)
        self.cells[row][col]['text'] = data

    def clear(self):
 for window in self._frame.winfo_children():
  window.destroy()

 col = 0
 for h in self._headings:
            Label(self._frame, text=h, relief=RAISED,
borderwidth=2).grid(row = 0, column = col, sticky = 'nsew')

            self._frame.grid_rowconfigure(0, weight = 1)
            self._frame.grid_columnconfigure(col, weight = 1)
            self._table.reposition()
            col = col + 1


"Stijn Gunst" <stijn.gunst at student.kuleuven.ac.be> wrote in message
news:3B0C31C8.35D882F3 at student.kuleuven.ac.be...
> hi
>
> i can't find a tablewidget for Python (not even in the Pmw megawidget
> module)
> how should i visually represent sql-data with those modules? a array of
> editboxes seems a bit cumbersome
>
> tx
>





More information about the Python-list mailing list