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