1 from wxPython
.wx
import *
2 from wxPython
.grid
import *
6 #---------------------------------------------------------------------------
8 class CustomDataTable(wxPyGridTableBase
):
12 def __init__(self
, log
):
13 wxPyGridTableBase
.__init
__(self
)
16 self
.colLabels
= ['ID', 'Description', 'Severity', 'Priority', 'Platform',
17 'Opened?', 'Fixed?', 'Tested?']
19 self
.dataTypes
= [wxGRID_VALUE_NUMBER
,
21 wxGRID_VALUE_CHOICE
+ ':only in a million years!,wish list,minor,normal,major,critical',
22 wxGRID_VALUE_NUMBER
+ ':1,5',
23 wxGRID_VALUE_CHOICE
+ ':all,MSW,GTK,other',
29 [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1],
30 [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0],
31 [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0]
36 #--------------------------------------------------
37 # required methods for the wxPyGridTableBase interface
39 def GetNumberRows(self
):
40 return len(self
.data
) + 1
42 def GetNumberCols(self
):
43 return len(self
.data
[0])
45 def IsEmptyCell(self
, row
, col
):
46 return not self
.data
[row
][col
]
48 # Get/Set values in the table. The Python version of these
49 # methods can handle any data-type, (as long as the Editor and
50 # Renderer understands the type too,) not just strings as in the
52 def GetValue(self
, row
, col
):
54 return self
.data
[row
][col
]
58 def SetValue(self
, row
, col
, value
):
60 self
.data
[row
][col
] = value
63 self
.data
.append([''] * self
.GetNumberCols())
64 self
.SetValue(row
, col
, value
)
66 # tell the grid we've added a row
67 msg
= wxGridTableMessage(self
, # The table
68 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
, # what we did to it
71 self
.GetView().ProcessTableMessage(msg
)
74 #--------------------------------------------------
75 # Some optional methods
77 # Called when the grid needs to display labels
78 def GetColLabelValue(self
, col
):
79 return self
.colLabels
[col
]
81 # Called to determine the kind of editor/renderer to use by
82 # default, doesn't necessarily have to be the same type used
83 # nativly by the editor/renderer if they know how to convert.
84 def GetTypeName(self
, row
, col
):
85 return self
.dataTypes
[col
]
87 # Called to determine how the data can be fetched and stored by the
88 # editor and renderer. This allows you to enforce some type-safety
90 def CanGetValueAs(self
, row
, col
, typeName
):
91 colType
= string
.split(self
.dataTypes
[col
], ':')[0]
92 if typeName
== colType
:
97 def CanSetValueAs(self
, row
, col
, typeName
):
98 return self
.CanGetValueAs(row
, col
, typeName
)
104 #---------------------------------------------------------------------------
108 class CustTableGrid(wxGrid
):
109 def __init__(self
, parent
, log
):
110 wxGrid
.__init
__(self
, parent
, -1)
112 table
= CustomDataTable(log
)
114 # The second parameter means that the grid is to take ownership of the
115 # table and will destroy it when done. Otherwise you would need to keep
116 # a reference to it and call it's Destroy method later.
117 self
.SetTable(table
, true
)
119 self
.SetRowLabelSize(0)
121 self
.AutoSizeColumns(false
)
123 EVT_GRID_CELL_LEFT_DCLICK(self
, self
.OnLeftDClick
)
127 # I do this because I don't like the default behaviour of not starting the
128 # cell editor on double clicks, but only a second click.
129 def OnLeftDClick(self
, evt
):
130 if self
.CanEnableCellControl():
131 self
.EnableCellEditControl()
134 #---------------------------------------------------------------------------
136 class TestFrame(wxFrame
):
137 def __init__(self
, parent
, log
):
138 wxFrame
.__init
__(self
, parent
, -1, "Custom Table, data driven Grid Demo", size
=(640,480))
139 grid
= CustTableGrid(self
, log
)
143 #---------------------------------------------------------------------------
145 if __name__
== '__main__':
147 app
= wxPySimpleApp()
148 frame
= TestFrame(None, sys
.stdout
)
153 #---------------------------------------------------------------------------