]>
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")
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
)
56 #self.SetDefaultCellOverflow(False)
57 #r = wxGridCellAutoWrapStringRenderer()
58 #self.SetCellRenderer(9, 1, r)
61 self
.SetCellValue( 9, 1, "This default cell will overflow into neighboring cells, but not if you turn overflow off.");
62 self
.SetCellSize(11, 1, 3, 3);
63 self
.SetCellAlignment(11, 1, wxALIGN_CENTRE
, wxALIGN_CENTRE
);
64 self
.SetCellValue(11, 1, "This cell is set to span 3 rows and 3 columns");
69 EVT_GRID_CELL_LEFT_CLICK(self
, self
.OnCellLeftClick
)
70 EVT_GRID_CELL_RIGHT_CLICK(self
, self
.OnCellRightClick
)
71 EVT_GRID_CELL_LEFT_DCLICK(self
, self
.OnCellLeftDClick
)
72 EVT_GRID_CELL_RIGHT_DCLICK(self
, self
.OnCellRightDClick
)
74 EVT_GRID_LABEL_LEFT_CLICK(self
, self
.OnLabelLeftClick
)
75 EVT_GRID_LABEL_RIGHT_CLICK(self
, self
.OnLabelRightClick
)
76 EVT_GRID_LABEL_LEFT_DCLICK(self
, self
.OnLabelLeftDClick
)
77 EVT_GRID_LABEL_RIGHT_DCLICK(self
, self
.OnLabelRightDClick
)
79 EVT_GRID_ROW_SIZE(self
, self
.OnRowSize
)
80 EVT_GRID_COL_SIZE(self
, self
.OnColSize
)
82 EVT_GRID_RANGE_SELECT(self
, self
.OnRangeSelect
)
83 EVT_GRID_CELL_CHANGE(self
, self
.OnCellChange
)
84 EVT_GRID_SELECT_CELL(self
, self
.OnSelectCell
)
86 EVT_GRID_EDITOR_SHOWN(self
, self
.OnEditorShown
)
87 EVT_GRID_EDITOR_HIDDEN(self
, self
.OnEditorHidden
)
88 EVT_GRID_EDITOR_CREATED(self
, self
.OnEditorCreated
)
92 def OnCellLeftClick(self
, evt
):
93 self
.log
.write("OnCellLeftClick: (%d,%d) %s\n" %
94 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
97 def OnCellRightClick(self
, evt
):
98 self
.log
.write("OnCellRightClick: (%d,%d) %s\n" %
99 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
102 def OnCellLeftDClick(self
, evt
):
103 self
.log
.write("OnCellLeftDClick: (%d,%d) %s\n" %
104 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
107 def OnCellRightDClick(self
, evt
):
108 self
.log
.write("OnCellRightDClick: (%d,%d) %s\n" %
109 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
112 def OnLabelLeftClick(self
, evt
):
113 self
.log
.write("OnLabelLeftClick: (%d,%d) %s\n" %
114 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
117 def OnLabelRightClick(self
, evt
):
118 self
.log
.write("OnLabelRightClick: (%d,%d) %s\n" %
119 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
122 def OnLabelLeftDClick(self
, evt
):
123 self
.log
.write("OnLabelLeftDClick: (%d,%d) %s\n" %
124 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
127 def OnLabelRightDClick(self
, evt
):
128 self
.log
.write("OnLabelRightDClick: (%d,%d) %s\n" %
129 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
133 def OnRowSize(self
, evt
):
134 self
.log
.write("OnRowSize: row %d, %s\n" %
135 (evt
.GetRowOrCol(), evt
.GetPosition()))
138 def OnColSize(self
, evt
):
139 self
.log
.write("OnColSize: col %d, %s\n" %
140 (evt
.GetRowOrCol(), evt
.GetPosition()))
143 def OnRangeSelect(self
, evt
):
145 self
.log
.write("OnRangeSelect: top-left %s, bottom-right %s\n" %
146 (evt
.GetTopLeftCoords(), evt
.GetBottomRightCoords()))
150 def OnCellChange(self
, evt
):
151 self
.log
.write("OnCellChange: (%d,%d) %s\n" %
152 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
154 # Show how to stay in a cell that has bad data. We can't just
155 # call SetGridCursor here since we are nested inside one so it
156 # won't have any effect. Instead, set coordinants to move to in
158 value
= self
.GetCellValue(evt
.GetRow(), evt
.GetCol())
159 if value
== 'no good':
160 self
.moveTo
= evt
.GetRow(), evt
.GetCol()
163 def OnIdle(self
, evt
):
164 if self
.moveTo
!= None:
165 self
.SetGridCursor(self
.moveTo
[0], self
.moveTo
[1])
170 def OnSelectCell(self
, evt
):
171 self
.log
.write("OnSelectCell: (%d,%d) %s\n" %
172 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
174 # Another way to stay in a cell that has a bad value...
175 row
= self
.GetGridCursorRow()
176 col
= self
.GetGridCursorCol()
177 if self
.IsCellEditControlEnabled():
178 self
.HideCellEditControl()
179 self
.DisableCellEditControl()
180 value
= self
.GetCellValue(row
, col
)
181 if value
== 'no good 2':
182 return # cancels the cell selection
186 def OnEditorShown(self
, evt
):
187 if evt
.GetRow() == 6 and evt
.GetCol() == 3 and \
188 wxMessageBox("Are you sure you wish to edit this cell?",
189 "Checking", wxYES_NO
) == wxNO
:
193 self
.log
.write("OnEditorShown: (%d,%d) %s\n" %
194 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
198 def OnEditorHidden(self
, evt
):
199 if evt
.GetRow() == 6 and evt
.GetCol() == 3 and \
200 wxMessageBox("Are you sure you wish to finish editing this cell?",
201 "Checking", wxYES_NO
) == wxNO
:
205 self
.log
.write("OnEditorHidden: (%d,%d) %s\n" %
206 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
210 def OnEditorCreated(self
, evt
):
211 self
.log
.write("OnEditorCreated: (%d, %d) %s\n" %
212 (evt
.GetRow(), evt
.GetCol(), evt
.GetControl()))
216 #---------------------------------------------------------------------------
218 class TestFrame(wxFrame
):
219 def __init__(self
, parent
, log
):
220 wxFrame
.__init
__(self
, parent
, -1, "Simple Grid Demo", size
=(640,480))
221 grid
= SimpleGrid(self
, log
)
225 #---------------------------------------------------------------------------
227 if __name__
== '__main__':
229 app
= wxPySimpleApp()
230 frame
= TestFrame(None, sys
.stdout
)
235 #---------------------------------------------------------------------------