GUI Python interfaces and DataGrids

Joseph Andrew Knapka jknapka at earthlink.net
Sat Aug 25 16:10:29 EDT 2001


Boudewijn Rempt wrote:
> 
> Maan Hamze <mmhamze at pleiades.net> 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:
    pass

class SequenceGrid(Frame):

    def __init__(self,parent,**kw):
        Frame.__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:
            field.grid_forget()
            field.destroy()
        self.fields = []

        for row in rows:
            self.cols = 0
            for item in row:
                txt = str(item)
                field = Text(self,height=1,width=len(txt))
                field.insert("1.0",txt)
                field.grid(row=self.rows,col=self.cols,sticky="nsew")
                self.fields.append(field)
                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 =
[["Hello","world",42],["I","love","Python!"],["Tkinter","rocks","the
house!"]]

def fill():
    global test, data
    try:
        print "1,2 is %s"%sg.getItem(1,2)
    except:
        print "Oops, no data in grid 1,2"
    sg.setRows(data)
    data.append(["New","row",test])
    test += 1

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

[snip]



> 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  | http://www.valdyas.org

-- 
# 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:
http://home.earthlink.net/~jknapka/linux-mm/vmoutline.html



More information about the Python-list mailing list