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