[wxPython] wxPython and wxGrid / wxPyGridTableBase

Hello,

I finally got round to wirte a code snipped, which shows the problems I
do have with the wxPyGridTableBase:

a) when the program starts, I would like that the fields span the whole
width of the window. (I would like to resize the table to the window
size)

b) when I double-click on a number, I can edit the number. However when
I resize the width, so that the fields are larger then the current
window size, I get the expected behavior when double-clicking on a
number: the number is printed to stdout

Advice welcome,

Horst

I am using wxPython 2.3.1 with python 2.1 on linux 2.4.9

#----------------------------CODE---------------------------------------
#!/usr/bin/python
# -*- coding: utf-8; -*-

···

#

#

from wxPython.wx import *
from wxPython.grid import *

class Wordlist(wxPyGridTableBase):
    def __init__(self, dict = None):
        wxPyGridTableBase.__init__(self)
        self.dict = dict
        
    def GetNumberRows(self):
        if (self.dict == None):
            return 0
        else:
            return self.dict.GetNumberEntries()
    
    def GetNumberCols(self):
        return 1
    
    def IsEmptyCell(self,row, col):
        return false
    
    def IsEditable(self):
        return false
    
    def GetEditable(self):
        return false
    
    def GetValue(self, row, col):
        if (self.dict == None):
            return ""
        else:
            return self.dict.GetEntry(row)
    
    def SetValue(self, row, col, value):
        pass
    
    def SetDictionary(self, dict):
        msg = wxGridTableMessage( self,
                                    wxGRIDTABLE_NOTIFY_ROWS_DELETED,
                                    self.GetNumberRows() )
        self.GetView().ProcessTableMessage(msg)
        
        self.dict = dict

        msg = wxGridTableMessage( self,
                                    wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
                                    self.GetNumberRows() )
        self.GetView().ProcessTableMessage(msg)

    def EntryChanged(self, dict):
        msg = wxGridTableMessage( self,
                                    wxGRIDTABLE_NOTIFY_ROWS_DELETED,
                                    self.GetNumberRows() )
        self.GetView().ProcessTableMessage(msg)
                
        msg = wxGridTableMessage( self,
                                    wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
                                    self.GetNumberRows() )
        self.GetView().ProcessTableMessage(msg)

    def EntryDelete(self, position):
        msg = wxGridTableMessage( self,
                                    wxGRIDTABLE_NOTIFY_ROWS_DELETED,
                                    position,
                                    1 )
        self.GetView().ProcessTableMessage(msg)

    def EntryInsert(self, position):
        msg = wxGridTableMessage( self,
                                    wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
                                    position,
                                    1 )
        self.GetView().ProcessTableMessage(msg)
        
class WordListGrid(wxGrid):
    def __init__(self, parent,size,pos):
        wxGrid.__init__(self, parent, 1, size=size, pos=pos)
        self.table = Wordlist()
        self.SetTable(self.table, true)
        self.EnableGridLines(false)
        self.SetColLabelSize(0)
        self.SetRowLabelSize(0)
        #EVT_SIZE(self, self.SetMySize)
        
    def SetMySize(self):
        size = self.GetSize()
        self.table.SetColSize(0,size)
        self.SetSize(self.GetSize())
        
    def SetDictionary(self, dict):
        self.table.SetDictionary(dict)
        
    def SetClick(self, routine):
        self.routine = routine
        EVT_GRID_CELL_LEFT_DCLICK(self, self.OnDClick)
        EVT_LEFT_DOWN(self, self.OnDClick)
        
    def OnDClick(self, evt):
        self.routine( self.table.GetValue( evt.GetRow(),evt.GetCol() ) )
        evt.Skip()
        
#---------------------------------------------------------------------------

class GridDict:
    def __init__(self):
        pass

    def GetNumberEntries(self):
        return 100

    def GetEntry(self, num):
        return num

class TestFrame(wxFrame):
    def __init__(self, parent, log):
        wxFrame.__init__(self, parent, -1, "Huge (virtual) Table Demo", size=(640,480))
        grid = WordListGrid(self, size=wxSize(100,400), pos=wxPoint(0,0) )

        grid.SetReadOnly(5,5, true)
        self.dict = GridDict()
        grid.SetDictionary( self.dict)
        grid.SetClick( self.onclick)

    def onclick(self, value):
        print value
        
#---------------------------------------------------------------------------

if __name__ == '__main__':
    import sys
    app = wxPySimpleApp()
    frame = TestFrame(None, sys.stdout)
    frame.Show(true)
    app.MainLoop()

#------------------------END CODE---------------------------------------------------

--
Horst@freedict.de
Horst Eyermann
Germany

You need a dictionary? - visit http://www.freedict.de
for free (GPL) dictionaries (unix; windows work in progress)
For windows, visit http://www.freedict.de/wbuch

A article (in German) about dictionary efforts on the net
http://www.heise.de/tp/deutsch/inhalt/on/5927/1.html