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?', 'TestFloat']
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',
27 wxGRID_VALUE_FLOAT
+ ':6,2',
31 [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1, 1.12],
32 [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0, 1.50],
33 [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0, 1.56]
38 #--------------------------------------------------
39 # required methods for the wxPyGridTableBase interface
41 def GetNumberRows(self
):
42 return len(self
.data
) + 1
44 def GetNumberCols(self
):
45 return len(self
.data
[0])
47 def IsEmptyCell(self
, row
, col
):
48 return not self
.data
[row
][col
]
50 # Get/Set values in the table. The Python version of these
51 # methods can handle any data-type, (as long as the Editor and
52 # Renderer understands the type too,) not just strings as in the
54 def GetValue(self
, row
, col
):
56 return self
.data
[row
][col
]
60 def SetValue(self
, row
, col
, value
):
62 self
.data
[row
][col
] = value
65 self
.data
.append([''] * self
.GetNumberCols())
66 self
.SetValue(row
, col
, value
)
68 # tell the grid we've added a row
69 msg
= wxGridTableMessage(self
, # The table
70 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
, # what we did to it
73 self
.GetView().ProcessTableMessage(msg
)
76 #--------------------------------------------------
77 # Some optional methods
79 # Called when the grid needs to display labels
80 def GetColLabelValue(self
, col
):
81 return self
.colLabels
[col
]
83 # Called to determine the kind of editor/renderer to use by
84 # default, doesn't necessarily have to be the same type used
85 # nativly by the editor/renderer if they know how to convert.
86 def GetTypeName(self
, row
, col
):
87 return self
.dataTypes
[col
]
89 # Called to determine how the data can be fetched and stored by the
90 # editor and renderer. This allows you to enforce some type-safety
92 def CanGetValueAs(self
, row
, col
, typeName
):
93 colType
= string
.split(self
.dataTypes
[col
], ':')[0]
94 if typeName
== colType
:
99 def CanSetValueAs(self
, row
, col
, typeName
):
100 return self
.CanGetValueAs(row
, col
, typeName
)
106 #---------------------------------------------------------------------------
110 class CustTableGrid(wxGrid
):
111 def __init__(self
, parent
, log
):
112 wxGrid
.__init
__(self
, parent
, -1)
114 table
= CustomDataTable(log
)
116 # The second parameter means that the grid is to take ownership of the
117 # table and will destroy it when done. Otherwise you would need to keep
118 # a reference to it and call it's Destroy method later.
119 self
.SetTable(table
, true
)
121 self
.SetRowLabelSize(0)
123 self
.AutoSizeColumns(false
)
125 EVT_GRID_CELL_LEFT_DCLICK(self
, self
.OnLeftDClick
)
129 # I do this because I don't like the default behaviour of not starting the
130 # cell editor on double clicks, but only a second click.
131 def OnLeftDClick(self
, evt
):
132 if self
.CanEnableCellControl():
133 self
.EnableCellEditControl()
136 #---------------------------------------------------------------------------
138 class TestFrame(wxFrame
):
139 def __init__(self
, parent
, log
):
140 wxFrame
.__init
__(self
, parent
, -1, "Custom Table, data driven Grid Demo", size
=(640,480))
141 grid
= CustTableGrid(self
, log
)
145 #---------------------------------------------------------------------------
147 if __name__
== '__main__':
149 app
= wxPySimpleApp()
150 frame
= TestFrame(None, sys
.stdout
)
155 #---------------------------------------------------------------------------