1 from wxPython
.wx
import *
2 from wxPython
.grid
import *
4 #---------------------------------------------------------------------------
6 class CustomDataTable(wxPyGridTableBase
):
10 def __init__(self
, log
):
11 wxPyGridTableBase
.__init
__(self
)
14 self
.colLabels
= ['ID', 'Description', 'Severity', 'Priority', 'Platform',
15 'Opened?', 'Fixed?', 'Tested?', 'TestFloat']
17 self
.dataTypes
= [wxGRID_VALUE_NUMBER
,
19 wxGRID_VALUE_CHOICE
+ ':only in a million years!,wish list,minor,normal,major,critical',
20 wxGRID_VALUE_NUMBER
+ ':1,5',
21 wxGRID_VALUE_CHOICE
+ ':all,MSW,GTK,other',
25 wxGRID_VALUE_FLOAT
+ ':6,2',
29 [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1, 1.12],
30 [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0, 1.50],
31 [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0, 1.56]
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
):
47 return not self
.data
[row
][col
]
51 # Get/Set values in the table. The Python version of these
52 # methods can handle any data-type, (as long as the Editor and
53 # Renderer understands the type too,) not just strings as in the
55 def GetValue(self
, row
, col
):
57 return self
.data
[row
][col
]
61 def SetValue(self
, row
, col
, value
):
63 self
.data
[row
][col
] = value
66 self
.data
.append([''] * self
.GetNumberCols())
67 self
.SetValue(row
, col
, value
)
69 # tell the grid we've added a row
70 msg
= wxGridTableMessage(self
, # The table
71 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
, # what we did to it
74 self
.GetView().ProcessTableMessage(msg
)
77 #--------------------------------------------------
78 # Some optional methods
80 # Called when the grid needs to display labels
81 def GetColLabelValue(self
, col
):
82 return self
.colLabels
[col
]
84 # Called to determine the kind of editor/renderer to use by
85 # default, doesn't necessarily have to be the same type used
86 # nativly by the editor/renderer if they know how to convert.
87 def GetTypeName(self
, row
, col
):
88 return self
.dataTypes
[col
]
90 # Called to determine how the data can be fetched and stored by the
91 # editor and renderer. This allows you to enforce some type-safety
93 def CanGetValueAs(self
, row
, col
, typeName
):
94 colType
= self
.dataTypes
[col
].split(':')[0]
95 if typeName
== colType
:
100 def CanSetValueAs(self
, row
, col
, typeName
):
101 return self
.CanGetValueAs(row
, col
, typeName
)
107 #---------------------------------------------------------------------------
111 class CustTableGrid(wxGrid
):
112 def __init__(self
, parent
, log
):
113 wxGrid
.__init
__(self
, parent
, -1)
115 table
= CustomDataTable(log
)
117 # The second parameter means that the grid is to take ownership of the
118 # table and will destroy it when done. Otherwise you would need to keep
119 # a reference to it and call it's Destroy method later.
120 self
.SetTable(table
, true
)
122 self
.SetRowLabelSize(0)
124 self
.AutoSizeColumns(False)
126 EVT_GRID_CELL_LEFT_DCLICK(self
, self
.OnLeftDClick
)
130 # I do this because I don't like the default behaviour of not starting the
131 # cell editor on double clicks, but only a second click.
132 def OnLeftDClick(self
, evt
):
133 if self
.CanEnableCellControl():
134 self
.EnableCellEditControl()
137 #---------------------------------------------------------------------------
139 class TestFrame(wxFrame
):
140 def __init__(self
, parent
, log
):
141 wxFrame
.__init
__(self
, parent
, -1, "Custom Table, data driven Grid Demo", size
=(640,480))
142 p
= wxPanel(self
, -1, style
=0)
143 grid
= CustTableGrid(p
, log
)
144 b
= wxButton(p
, -1, "Another Control...")
146 EVT_BUTTON(self
, b
.GetId(), self
.OnButton
)
147 EVT_SET_FOCUS(b
, self
.OnButtonFocus
)
148 bs
= wxBoxSizer(wxVERTICAL
)
149 bs
.Add(grid
, 1, wxGROW|wxALL
, 5)
153 def OnButton(self
, evt
):
154 print "button selected"
156 def OnButtonFocus(self
, evt
):
160 #---------------------------------------------------------------------------
162 if __name__
== '__main__':
164 app
= wxPySimpleApp()
165 frame
= TestFrame(None, sys
.stdout
)
170 #---------------------------------------------------------------------------