GUI Python interfaces and DataGrids

Joseph Andrew Knapka jknapka at
Sat Aug 25 22:10:29 CEST 2001

Boudewijn Rempt wrote:
> Maan Hamze <mmhamze at> wrote:
> > Unless I am missing something, I have looked and I Tkinter does not seem to
> > have a DataGrid to receive the rows of a query (a dataset) and display them
> > or to bind it into a dataset.  There is the Python DB API after all.  So I
> > believe the issue of Datasets should be standardized across the different
> > database packages.
> > Now, I believe I have an idea on how to build a class and use it in Python
> > that can do exactly this.
> > But does Tkinter have a DataGrid?  If not, are there any other GUI Python
> > packages that come with a DataGrid?
> Not if you mean a ready-made data-aware grid control like Visual Basic
> has. You'll have to roll your own. One problem is that Tkinter, to the
> best of my knowledge doesn't have a grid control at all.

True, but the grid layout manager makes it extremely easy
to create one.

from Tkinter import *

class SequenceGridException:

class SequenceGrid(Frame):

    def __init__(self,parent,**kw):
        self.fields = []
        self.rows = 0
        self.cols = 0

    def setRows(self,rows):
        self.rows = 0
        self.cols = 0

        for field in self.fields:
        self.fields = []

        for row in rows:
            self.cols = 0
            for item in row:
                txt = str(item)
                field = Text(self,height=1,width=len(txt))
                self.cols += 1
            self.rows += 1

    def getItem(self,row,col):
        if row < 0 or col < 0 or row >= self.rows or col >= self.cols:
            raise SequenceGridException
        idx = row * self.cols + col
        return self.fields[idx].get("1.0","end")

# Test code:
test = 0
data =

def fill():
    global test, data
        print "1,2 is %s"%sg.getItem(1,2)
        print "Oops, no data in grid 1,2"
    test += 1

if __name__ == "__main__":
    root = Tk()
    sg = SequenceGrid(root,width=100,height=100)
    btn = Button(command=fill,text="Fill grid")


> I know that
> both wxPython and PyQt have a good grid control, and filling a table
> yourself from the results of a query is easy enough, and gives you a
> lot of flexibility. In fact, at the last VB shop where I worked, use
> of data-aware controls was forbidden: we had to fill the grids
> ourselves anyway.
> --
> Boudewijn Rempt  |

# Joe Knapka
# "You know how many remote castles there are along the
#  gorges? You can't MOVE for remote castles!" - Lu Tze re. Uberwald
# Linux MM docs:

More information about the Python-list mailing list