]>
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")
35 self
.SetCellEditor(7, 0, wxGridCellNumberEditor())
37 self
.SetCellValue(6, 3, "You can veto editing this cell")
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
))
47 # you can set cell attributes for the whole row (or column)
48 self
.SetRowAttr(5, attr
)
50 self
.SetColLabelValue(0, "Custom")
51 self
.SetColLabelValue(1, "column")
52 self
.SetColLabelValue(2, "labels")
54 self
.SetColLabelAlignment(wxALIGN_LEFT
, wxALIGN_BOTTOM
)
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
)
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
)
67 EVT_GRID_ROW_SIZE(self
, self
.OnRowSize
)
68 EVT_GRID_COL_SIZE(self
, self
.OnColSize
)
70 EVT_GRID_RANGE_SELECT(self
, self
.OnRangeSelect
)
71 EVT_GRID_CELL_CHANGE(self
, self
.OnCellChange
)
72 EVT_GRID_SELECT_CELL(self
, self
.OnSelectCell
)
74 EVT_GRID_EDITOR_SHOWN(self
, self
.OnEditorShown
)
75 EVT_GRID_EDITOR_HIDDEN(self
, self
.OnEditorHidden
)
76 EVT_GRID_EDITOR_CREATED(self
, self
.OnEditorCreated
)
80 def OnCellLeftClick(self
, evt
):
81 self
.log
.write("OnCellLeftClick: (%d,%d) %s\n" %
82 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
85 def OnCellRightClick(self
, evt
):
86 self
.log
.write("OnCellRightClick: (%d,%d) %s\n" %
87 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
90 def OnCellLeftDClick(self
, evt
):
91 self
.log
.write("OnCellLeftDClick: (%d,%d) %s\n" %
92 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
95 def OnCellRightDClick(self
, evt
):
96 self
.log
.write("OnCellRightDClick: (%d,%d) %s\n" %
97 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
100 def OnLabelLeftClick(self
, evt
):
101 self
.log
.write("OnLabelLeftClick: (%d,%d) %s\n" %
102 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
105 def OnLabelRightClick(self
, evt
):
106 self
.log
.write("OnLabelRightClick: (%d,%d) %s\n" %
107 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
110 def OnLabelLeftDClick(self
, evt
):
111 self
.log
.write("OnLabelLeftDClick: (%d,%d) %s\n" %
112 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
115 def OnLabelRightDClick(self
, evt
):
116 self
.log
.write("OnLabelRightDClick: (%d,%d) %s\n" %
117 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
121 def OnRowSize(self
, evt
):
122 self
.log
.write("OnRowSize: row %d, %s\n" %
123 (evt
.GetRowOrCol(), evt
.GetPosition()))
126 def OnColSize(self
, evt
):
127 self
.log
.write("OnColSize: col %d, %s\n" %
128 (evt
.GetRowOrCol(), evt
.GetPosition()))
131 def OnRangeSelect(self
, evt
):
133 self
.log
.write("OnRangeSelect: top-left %s, bottom-right %s\n" %
134 (evt
.GetTopLeftCoords(), evt
.GetBottomRightCoords()))
138 def OnCellChange(self
, evt
):
139 self
.log
.write("OnCellChange: (%d,%d) %s\n" %
140 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
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
146 value
= self
.GetCellValue(evt
.GetRow(), evt
.GetCol())
147 if value
== 'no good':
148 self
.moveTo
= evt
.GetRow(), evt
.GetCol()
151 def OnIdle(self
, evt
):
152 if self
.moveTo
!= None:
153 self
.SetGridCursor(self
.moveTo
[0], self
.moveTo
[1])
158 def OnSelectCell(self
, evt
):
159 self
.log
.write("OnSelectCell: (%d,%d) %s\n" %
160 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
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
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
:
181 self
.log
.write("OnEditorShown: (%d,%d) %s\n" %
182 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
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
:
193 self
.log
.write("OnEditorHidden: (%d,%d) %s\n" %
194 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
198 def OnEditorCreated(self
, evt
):
199 self
.log
.write("OnEditorCreated: (%d, %d) %s\n" %
200 (evt
.GetRow(), evt
.GetCol(), evt
.GetControl()))
204 #---------------------------------------------------------------------------
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
)
213 #---------------------------------------------------------------------------
215 if __name__
== '__main__':
217 app
= wxPySimpleApp()
218 frame
= TestFrame(None, sys
.stdout
)
223 #---------------------------------------------------------------------------