]>
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)
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())
32 self
.SetCellValue(5, 0, "123")
33 self
.SetCellEditor(6, 0, wxGridCellFloatEditor())
34 self
.SetCellValue(6, 0, "123.34")
36 self
.SetCellValue(6, 3, "You can veto editing this cell")
39 # attribute objects let you keep a set of formatting values
40 # in one spot, and reuse them if needed
41 attr
= wxGridCellAttr()
42 attr
.SetTextColour(wxBLACK
)
43 attr
.SetBackgroundColour(wxRED
)
44 attr
.SetFont(wxFont(10, wxSWISS
, wxNORMAL
, wxBOLD
))
46 # you can set cell attributes for the whole row (or column)
47 self
.SetRowAttr(5, attr
)
49 self
.SetColLabelValue(0, "Custom")
50 self
.SetColLabelValue(1, "column")
51 self
.SetColLabelValue(2, "labels")
53 self
.SetColLabelAlignment(wxALIGN_LEFT
, wxALIGN_BOTTOM
)
56 EVT_GRID_CELL_LEFT_CLICK(self
, self
.OnCellLeftClick
)
57 EVT_GRID_CELL_RIGHT_CLICK(self
, self
.OnCellRightClick
)
58 EVT_GRID_CELL_LEFT_DCLICK(self
, self
.OnCellLeftDClick
)
59 EVT_GRID_CELL_RIGHT_DCLICK(self
, self
.OnCellRightDClick
)
61 EVT_GRID_LABEL_LEFT_CLICK(self
, self
.OnLabelLeftClick
)
62 EVT_GRID_LABEL_RIGHT_CLICK(self
, self
.OnLabelRightClick
)
63 EVT_GRID_LABEL_LEFT_DCLICK(self
, self
.OnLabelLeftDClick
)
64 EVT_GRID_LABEL_RIGHT_DCLICK(self
, self
.OnLabelRightDClick
)
66 EVT_GRID_ROW_SIZE(self
, self
.OnRowSize
)
67 EVT_GRID_COL_SIZE(self
, self
.OnColSize
)
69 EVT_GRID_RANGE_SELECT(self
, self
.OnRangeSelect
)
70 EVT_GRID_CELL_CHANGE(self
, self
.OnCellChange
)
71 EVT_GRID_SELECT_CELL(self
, self
.OnSelectCell
)
73 EVT_GRID_EDITOR_SHOWN(self
, self
.OnEditorShown
)
74 EVT_GRID_EDITOR_HIDDEN(self
, self
.OnEditorHidden
)
75 EVT_GRID_EDITOR_CREATED(self
, self
.OnEditorCreated
)
79 def OnCellLeftClick(self
, evt
):
80 self
.log
.write("OnCellLeftClick: (%d,%d) %s\n" %
81 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
84 def OnCellRightClick(self
, evt
):
85 self
.log
.write("OnCellRightClick: (%d,%d) %s\n" %
86 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
89 def OnCellLeftDClick(self
, evt
):
90 self
.log
.write("OnCellLeftDClick: (%d,%d) %s\n" %
91 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
94 def OnCellRightDClick(self
, evt
):
95 self
.log
.write("OnCellRightDClick: (%d,%d) %s\n" %
96 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
99 def OnLabelLeftClick(self
, evt
):
100 self
.log
.write("OnLabelLeftClick: (%d,%d) %s\n" %
101 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
104 def OnLabelRightClick(self
, evt
):
105 self
.log
.write("OnLabelRightClick: (%d,%d) %s\n" %
106 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
109 def OnLabelLeftDClick(self
, evt
):
110 self
.log
.write("OnLabelLeftDClick: (%d,%d) %s\n" %
111 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
114 def OnLabelRightDClick(self
, evt
):
115 self
.log
.write("OnLabelRightDClick: (%d,%d) %s\n" %
116 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
120 def OnRowSize(self
, evt
):
121 self
.log
.write("OnRowSize: row %d, %s\n" %
122 (evt
.GetRowOrCol(), evt
.GetPosition()))
125 def OnColSize(self
, evt
):
126 self
.log
.write("OnColSize: col %d, %s\n" %
127 (evt
.GetRowOrCol(), evt
.GetPosition()))
130 def OnRangeSelect(self
, evt
):
132 self
.log
.write("OnRangeSelect: top-left %s, bottom-right %s\n" %
133 (evt
.GetTopLeftCoords(), evt
.GetBottomRightCoords()))
137 def OnCellChange(self
, evt
):
138 self
.log
.write("OnCellChange: (%d,%d) %s\n" %
139 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
141 # Show how to stay in a cell that has bad data. We can't just
142 # call SetGridCursor here since we are nested inside one so it
143 # won't have any effect. Instead, set coordinants to move to in
145 value
= self
.GetCellValue(evt
.GetRow(), evt
.GetCol())
146 if value
== 'no good':
147 self
.moveTo
= evt
.GetRow(), evt
.GetCol()
150 def OnIdle(self
, evt
):
151 if self
.moveTo
!= None:
152 self
.SetGridCursor(self
.moveTo
[0], self
.moveTo
[1])
157 def OnSelectCell(self
, evt
):
158 self
.log
.write("OnSelectCell: (%d,%d) %s\n" %
159 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
161 # Another way to stay in a cell that has a bad value...
162 row
= self
.GetGridCursorRow()
163 col
= self
.GetGridCursorCol()
164 if self
.IsCellEditControlEnabled():
165 self
.HideCellEditControl()
166 self
.DisableCellEditControl()
167 value
= self
.GetCellValue(row
, col
)
168 if value
== 'no good 2':
169 return # cancels the cell selection
173 def OnEditorShown(self
, evt
):
174 if evt
.GetRow() == 6 and evt
.GetCol() == 3 and \
175 wxMessageBox("Are you sure you wish to edit this cell?",
176 "Checking", wxYES_NO
) == wxNO
:
180 self
.log
.write("OnEditorShown: (%d,%d) %s\n" %
181 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
185 def OnEditorHidden(self
, evt
):
186 if evt
.GetRow() == 6 and evt
.GetCol() == 3 and \
187 wxMessageBox("Are you sure you wish to finish editing this cell?",
188 "Checking", wxYES_NO
) == wxNO
:
192 self
.log
.write("OnEditorHidden: (%d,%d) %s\n" %
193 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
197 def OnEditorCreated(self
, evt
):
198 self
.log
.write("OnEditorCreated: (%d, %d) %s\n" %
199 (evt
.GetRow(), evt
.GetCol(), evt
.GetControl()))
203 #---------------------------------------------------------------------------
205 class TestFrame(wxFrame
):
206 def __init__(self
, parent
, log
):
207 wxFrame
.__init
__(self
, parent
, -1, "Simple Grid Demo", size
=(640,480))
208 grid
= SimpleGrid(self
, log
)
212 #---------------------------------------------------------------------------
214 if __name__
== '__main__':
216 app
= wxPySimpleApp()
217 frame
= TestFrame(None, sys
.stdout
)
222 #---------------------------------------------------------------------------