| 1 | from wxPython.wx import * |
| 2 | from wxPython.grid import * |
| 3 | from wxPython.lib.mixins.grid import wxGridAutoEditMixin |
| 4 | |
| 5 | #--------------------------------------------------------------------------- |
| 6 | |
| 7 | class SimpleGrid(wxGrid): ##, wxGridAutoEditMixin): |
| 8 | def __init__(self, parent, log): |
| 9 | wxGrid.__init__(self, parent, -1) |
| 10 | ##wxGridAutoEditMixin.__init__(self) |
| 11 | self.log = log |
| 12 | self.moveTo = None |
| 13 | |
| 14 | EVT_IDLE(self, self.OnIdle) |
| 15 | |
| 16 | self.CreateGrid(25, 25) |
| 17 | ##self.EnableEditing(false) |
| 18 | |
| 19 | # simple cell formatting |
| 20 | self.SetColSize(3, 200) |
| 21 | self.SetRowSize(4, 45) |
| 22 | self.SetCellValue(0, 0, "First cell") |
| 23 | self.SetCellValue(1, 1, "Another cell") |
| 24 | self.SetCellValue(2, 2, "Yet another cell") |
| 25 | self.SetCellValue(3, 3, "This cell is read-only") |
| 26 | self.SetCellFont(0, 0, wxFont(12, wxROMAN, wxITALIC, wxNORMAL)) |
| 27 | self.SetCellTextColour(1, 1, wxRED) |
| 28 | self.SetCellBackgroundColour(2, 2, wxCYAN) |
| 29 | self.SetReadOnly(3, 3, true) |
| 30 | |
| 31 | self.SetCellEditor(5, 0, wxGridCellNumberEditor()) |
| 32 | self.SetCellValue(5, 0, "123") |
| 33 | self.SetCellEditor(6, 0, wxGridCellFloatEditor()) |
| 34 | self.SetCellValue(6, 0, "123.34") |
| 35 | self.SetCellEditor(7, 0, wxGridCellNumberEditor()) |
| 36 | |
| 37 | self.SetCellValue(6, 3, "You can veto editing this cell") |
| 38 | |
| 39 | |
| 40 | # attribute objects let you keep a set of formatting values |
| 41 | # in one spot, and reuse them if needed |
| 42 | attr = wxGridCellAttr() |
| 43 | attr.SetTextColour(wxBLACK) |
| 44 | attr.SetBackgroundColour(wxRED) |
| 45 | attr.SetFont(wxFont(10, wxSWISS, wxNORMAL, wxBOLD)) |
| 46 | |
| 47 | # you can set cell attributes for the whole row (or column) |
| 48 | self.SetRowAttr(5, attr) |
| 49 | |
| 50 | self.SetColLabelValue(0, "Custom") |
| 51 | self.SetColLabelValue(1, "column") |
| 52 | self.SetColLabelValue(2, "labels") |
| 53 | |
| 54 | self.SetColLabelAlignment(wxALIGN_LEFT, wxALIGN_BOTTOM) |
| 55 | |
| 56 | # test all the events |
| 57 | EVT_GRID_CELL_LEFT_CLICK(self, self.OnCellLeftClick) |
| 58 | EVT_GRID_CELL_RIGHT_CLICK(self, self.OnCellRightClick) |
| 59 | EVT_GRID_CELL_LEFT_DCLICK(self, self.OnCellLeftDClick) |
| 60 | EVT_GRID_CELL_RIGHT_DCLICK(self, self.OnCellRightDClick) |
| 61 | |
| 62 | EVT_GRID_LABEL_LEFT_CLICK(self, self.OnLabelLeftClick) |
| 63 | EVT_GRID_LABEL_RIGHT_CLICK(self, self.OnLabelRightClick) |
| 64 | EVT_GRID_LABEL_LEFT_DCLICK(self, self.OnLabelLeftDClick) |
| 65 | EVT_GRID_LABEL_RIGHT_DCLICK(self, self.OnLabelRightDClick) |
| 66 | |
| 67 | EVT_GRID_ROW_SIZE(self, self.OnRowSize) |
| 68 | EVT_GRID_COL_SIZE(self, self.OnColSize) |
| 69 | |
| 70 | EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect) |
| 71 | EVT_GRID_CELL_CHANGE(self, self.OnCellChange) |
| 72 | EVT_GRID_SELECT_CELL(self, self.OnSelectCell) |
| 73 | |
| 74 | EVT_GRID_EDITOR_SHOWN(self, self.OnEditorShown) |
| 75 | EVT_GRID_EDITOR_HIDDEN(self, self.OnEditorHidden) |
| 76 | EVT_GRID_EDITOR_CREATED(self, self.OnEditorCreated) |
| 77 | |
| 78 | |
| 79 | |
| 80 | def OnCellLeftClick(self, evt): |
| 81 | self.log.write("OnCellLeftClick: (%d,%d) %s\n" % |
| 82 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) |
| 83 | evt.Skip() |
| 84 | |
| 85 | def OnCellRightClick(self, evt): |
| 86 | self.log.write("OnCellRightClick: (%d,%d) %s\n" % |
| 87 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) |
| 88 | evt.Skip() |
| 89 | |
| 90 | def OnCellLeftDClick(self, evt): |
| 91 | self.log.write("OnCellLeftDClick: (%d,%d) %s\n" % |
| 92 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) |
| 93 | evt.Skip() |
| 94 | |
| 95 | def OnCellRightDClick(self, evt): |
| 96 | self.log.write("OnCellRightDClick: (%d,%d) %s\n" % |
| 97 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) |
| 98 | evt.Skip() |
| 99 | |
| 100 | def OnLabelLeftClick(self, evt): |
| 101 | self.log.write("OnLabelLeftClick: (%d,%d) %s\n" % |
| 102 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) |
| 103 | evt.Skip() |
| 104 | |
| 105 | def OnLabelRightClick(self, evt): |
| 106 | self.log.write("OnLabelRightClick: (%d,%d) %s\n" % |
| 107 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) |
| 108 | evt.Skip() |
| 109 | |
| 110 | def OnLabelLeftDClick(self, evt): |
| 111 | self.log.write("OnLabelLeftDClick: (%d,%d) %s\n" % |
| 112 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) |
| 113 | evt.Skip() |
| 114 | |
| 115 | def OnLabelRightDClick(self, evt): |
| 116 | self.log.write("OnLabelRightDClick: (%d,%d) %s\n" % |
| 117 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) |
| 118 | evt.Skip() |
| 119 | |
| 120 | |
| 121 | def OnRowSize(self, evt): |
| 122 | self.log.write("OnRowSize: row %d, %s\n" % |
| 123 | (evt.GetRowOrCol(), evt.GetPosition())) |
| 124 | evt.Skip() |
| 125 | |
| 126 | def OnColSize(self, evt): |
| 127 | self.log.write("OnColSize: col %d, %s\n" % |
| 128 | (evt.GetRowOrCol(), evt.GetPosition())) |
| 129 | evt.Skip() |
| 130 | |
| 131 | def OnRangeSelect(self, evt): |
| 132 | if evt.Selecting(): |
| 133 | self.log.write("OnRangeSelect: top-left %s, bottom-right %s\n" % |
| 134 | (evt.GetTopLeftCoords(), evt.GetBottomRightCoords())) |
| 135 | evt.Skip() |
| 136 | |
| 137 | |
| 138 | def OnCellChange(self, evt): |
| 139 | self.log.write("OnCellChange: (%d,%d) %s\n" % |
| 140 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) |
| 141 | |
| 142 | # Show how to stay in a cell that has bad data. We can't just |
| 143 | # call SetGridCursor here since we are nested inside one so it |
| 144 | # won't have any effect. Instead, set coordinants to move to in |
| 145 | # idle time. |
| 146 | value = self.GetCellValue(evt.GetRow(), evt.GetCol()) |
| 147 | if value == 'no good': |
| 148 | self.moveTo = evt.GetRow(), evt.GetCol() |
| 149 | |
| 150 | |
| 151 | def OnIdle(self, evt): |
| 152 | if self.moveTo != None: |
| 153 | self.SetGridCursor(self.moveTo[0], self.moveTo[1]) |
| 154 | self.moveTo = None |
| 155 | evt.Skip() |
| 156 | |
| 157 | |
| 158 | def OnSelectCell(self, evt): |
| 159 | self.log.write("OnSelectCell: (%d,%d) %s\n" % |
| 160 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) |
| 161 | |
| 162 | # Another way to stay in a cell that has a bad value... |
| 163 | row = self.GetGridCursorRow() |
| 164 | col = self.GetGridCursorCol() |
| 165 | if self.IsCellEditControlEnabled(): |
| 166 | self.HideCellEditControl() |
| 167 | self.DisableCellEditControl() |
| 168 | value = self.GetCellValue(row, col) |
| 169 | if value == 'no good 2': |
| 170 | return # cancels the cell selection |
| 171 | evt.Skip() |
| 172 | |
| 173 | |
| 174 | def OnEditorShown(self, evt): |
| 175 | if evt.GetRow() == 6 and evt.GetCol() == 3 and \ |
| 176 | wxMessageBox("Are you sure you wish to edit this cell?", |
| 177 | "Checking", wxYES_NO) == wxNO: |
| 178 | evt.Veto() |
| 179 | return |
| 180 | |
| 181 | self.log.write("OnEditorShown: (%d,%d) %s\n" % |
| 182 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) |
| 183 | evt.Skip() |
| 184 | |
| 185 | |
| 186 | def OnEditorHidden(self, evt): |
| 187 | if evt.GetRow() == 6 and evt.GetCol() == 3 and \ |
| 188 | wxMessageBox("Are you sure you wish to finish editing this cell?", |
| 189 | "Checking", wxYES_NO) == wxNO: |
| 190 | evt.Veto() |
| 191 | return |
| 192 | |
| 193 | self.log.write("OnEditorHidden: (%d,%d) %s\n" % |
| 194 | (evt.GetRow(), evt.GetCol(), evt.GetPosition())) |
| 195 | evt.Skip() |
| 196 | |
| 197 | |
| 198 | def OnEditorCreated(self, evt): |
| 199 | self.log.write("OnEditorCreated: (%d, %d) %s\n" % |
| 200 | (evt.GetRow(), evt.GetCol(), evt.GetControl())) |
| 201 | |
| 202 | |
| 203 | |
| 204 | #--------------------------------------------------------------------------- |
| 205 | |
| 206 | class TestFrame(wxFrame): |
| 207 | def __init__(self, parent, log): |
| 208 | wxFrame.__init__(self, parent, -1, "Simple Grid Demo", size=(640,480)) |
| 209 | grid = SimpleGrid(self, log) |
| 210 | |
| 211 | |
| 212 | |
| 213 | #--------------------------------------------------------------------------- |
| 214 | |
| 215 | if __name__ == '__main__': |
| 216 | import sys |
| 217 | app = wxPySimpleApp() |
| 218 | frame = TestFrame(None, sys.stdout) |
| 219 | frame.Show(true) |
| 220 | app.MainLoop() |
| 221 | |
| 222 | |
| 223 | #--------------------------------------------------------------------------- |
| 224 | |
| 225 | |