]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GridSimple.py
1 from wxPython
.wx
import *
2 from wxPython
.grid
import *
3 from wxPython
.lib
.mixins
.grid
import wxGridAutoEditMixin
5 #---------------------------------------------------------------------------
7 class SimpleGrid(wxGrid
): ##, wxGridAutoEditMixin):
8 def __init__(self
, parent
, log
):
9 wxGrid
.__init
__(self
, parent
, -1)
10 ##wxGridAutoEditMixin.__init__(self)
14 EVT_IDLE(self
, self
.OnIdle
)
16 self
.CreateGrid(25, 25) #, wxGrid.wxGridSelectRows)
17 ##self.EnableEditing(False)
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)
31 self
.SetCellEditor(5, 0, wxGridCellNumberEditor(1,1000))
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())
37 self
.SetCellValue(6, 3, "You can veto editing this cell")
39 #self.SetRowLabelSize(0)
40 #self.SetColLabelSize(0)
42 # attribute objects let you keep a set of formatting values
43 # in one spot, and reuse them if needed
44 attr
= wxGridCellAttr()
45 attr
.SetTextColour(wxBLACK
)
46 attr
.SetBackgroundColour(wxRED
)
47 attr
.SetFont(wxFont(10, wxSWISS
, wxNORMAL
, wxBOLD
))
49 # you can set cell attributes for the whole row (or column)
50 self
.SetRowAttr(5, attr
)
52 self
.SetColLabelValue(0, "Custom")
53 self
.SetColLabelValue(1, "column")
54 self
.SetColLabelValue(2, "labels")
56 self
.SetColLabelAlignment(wxALIGN_LEFT
, wxALIGN_BOTTOM
)
58 #self.SetDefaultCellOverflow(False)
59 #r = wxGridCellAutoWrapStringRenderer()
60 #self.SetCellRenderer(9, 1, r)
63 self
.SetCellValue( 9, 1, "This default cell will overflow into neighboring cells, but not if you turn overflow off.");
64 self
.SetCellSize(11, 1, 3, 3);
65 self
.SetCellAlignment(11, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
66 self
.SetCellValue(11, 1, "This cell is set to span 3 rows and 3 columns");
69 editor
= wxGridCellTextEditor()
70 editor
.SetParameters('10')
71 self
.SetCellEditor(0, 4, editor
)
72 self
.SetCellValue(0, 4, "Limited text")
76 EVT_GRID_CELL_LEFT_CLICK(self
, self
.OnCellLeftClick
)
77 EVT_GRID_CELL_RIGHT_CLICK(self
, self
.OnCellRightClick
)
78 EVT_GRID_CELL_LEFT_DCLICK(self
, self
.OnCellLeftDClick
)
79 EVT_GRID_CELL_RIGHT_DCLICK(self
, self
.OnCellRightDClick
)
81 EVT_GRID_LABEL_LEFT_CLICK(self
, self
.OnLabelLeftClick
)
82 EVT_GRID_LABEL_RIGHT_CLICK(self
, self
.OnLabelRightClick
)
83 EVT_GRID_LABEL_LEFT_DCLICK(self
, self
.OnLabelLeftDClick
)
84 EVT_GRID_LABEL_RIGHT_DCLICK(self
, self
.OnLabelRightDClick
)
86 EVT_GRID_ROW_SIZE(self
, self
.OnRowSize
)
87 EVT_GRID_COL_SIZE(self
, self
.OnColSize
)
89 EVT_GRID_RANGE_SELECT(self
, self
.OnRangeSelect
)
90 EVT_GRID_CELL_CHANGE(self
, self
.OnCellChange
)
91 EVT_GRID_SELECT_CELL(self
, self
.OnSelectCell
)
93 EVT_GRID_EDITOR_SHOWN(self
, self
.OnEditorShown
)
94 EVT_GRID_EDITOR_HIDDEN(self
, self
.OnEditorHidden
)
95 EVT_GRID_EDITOR_CREATED(self
, self
.OnEditorCreated
)
99 def OnCellLeftClick(self
, evt
):
100 self
.log
.write("OnCellLeftClick: (%d,%d) %s\n" %
101 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
104 def OnCellRightClick(self
, evt
):
105 self
.log
.write("OnCellRightClick: (%d,%d) %s\n" %
106 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
109 def OnCellLeftDClick(self
, evt
):
110 self
.log
.write("OnCellLeftDClick: (%d,%d) %s\n" %
111 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
114 def OnCellRightDClick(self
, evt
):
115 self
.log
.write("OnCellRightDClick: (%d,%d) %s\n" %
116 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
119 def OnLabelLeftClick(self
, evt
):
120 self
.log
.write("OnLabelLeftClick: (%d,%d) %s\n" %
121 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
124 def OnLabelRightClick(self
, evt
):
125 self
.log
.write("OnLabelRightClick: (%d,%d) %s\n" %
126 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
129 def OnLabelLeftDClick(self
, evt
):
130 self
.log
.write("OnLabelLeftDClick: (%d,%d) %s\n" %
131 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
134 def OnLabelRightDClick(self
, evt
):
135 self
.log
.write("OnLabelRightDClick: (%d,%d) %s\n" %
136 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
140 def OnRowSize(self
, evt
):
141 self
.log
.write("OnRowSize: row %d, %s\n" %
142 (evt
.GetRowOrCol(), evt
.GetPosition()))
145 def OnColSize(self
, evt
):
146 self
.log
.write("OnColSize: col %d, %s\n" %
147 (evt
.GetRowOrCol(), evt
.GetPosition()))
150 def OnRangeSelect(self
, evt
):
152 self
.log
.write("OnRangeSelect: top-left %s, bottom-right %s\n" %
153 (evt
.GetTopLeftCoords(), evt
.GetBottomRightCoords()))
157 def OnCellChange(self
, evt
):
158 self
.log
.write("OnCellChange: (%d,%d) %s\n" %
159 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
161 # Show how to stay in a cell that has bad data. We can't just
162 # call SetGridCursor here since we are nested inside one so it
163 # won't have any effect. Instead, set coordinates to move to in
165 value
= self
.GetCellValue(evt
.GetRow(), evt
.GetCol())
166 if value
== 'no good':
167 self
.moveTo
= evt
.GetRow(), evt
.GetCol()
170 def OnIdle(self
, evt
):
171 if self
.moveTo
!= None:
172 self
.SetGridCursor(self
.moveTo
[0], self
.moveTo
[1])
177 def OnSelectCell(self
, evt
):
178 self
.log
.write("OnSelectCell: (%d,%d) %s\n" %
179 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
181 # Another way to stay in a cell that has a bad value...
182 row
= self
.GetGridCursorRow()
183 col
= self
.GetGridCursorCol()
184 if self
.IsCellEditControlEnabled():
185 self
.HideCellEditControl()
186 self
.DisableCellEditControl()
187 value
= self
.GetCellValue(row
, col
)
188 if value
== 'no good 2':
189 return # cancels the cell selection
193 def OnEditorShown(self
, evt
):
194 if evt
.GetRow() == 6 and evt
.GetCol() == 3 and \
195 wxMessageBox("Are you sure you wish to edit this cell?",
196 "Checking", wxYES_NO
) == wxNO
:
200 self
.log
.write("OnEditorShown: (%d,%d) %s\n" %
201 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
205 def OnEditorHidden(self
, evt
):
206 if evt
.GetRow() == 6 and evt
.GetCol() == 3 and \
207 wxMessageBox("Are you sure you wish to finish editing this cell?",
208 "Checking", wxYES_NO
) == wxNO
:
212 self
.log
.write("OnEditorHidden: (%d,%d) %s\n" %
213 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
217 def OnEditorCreated(self
, evt
):
218 self
.log
.write("OnEditorCreated: (%d, %d) %s\n" %
219 (evt
.GetRow(), evt
.GetCol(), evt
.GetControl()))
223 #---------------------------------------------------------------------------
225 class TestFrame(wxFrame
):
226 def __init__(self
, parent
, log
):
227 wxFrame
.__init
__(self
, parent
, -1, "Simple Grid Demo", size
=(640,480))
228 grid
= SimpleGrid(self
, log
)
232 #---------------------------------------------------------------------------
234 if __name__
== '__main__':
236 app
= wxPySimpleApp()
237 frame
= TestFrame(None, sys
.stdout
)
242 #---------------------------------------------------------------------------