]>
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");
67 editor
= wxGridCellTextEditor()
68 editor
.SetParameters('10')
69 self
.SetCellEditor(0, 4, editor
)
70 self
.SetCellValue(0, 4, "Limited text")
74 EVT_GRID_CELL_LEFT_CLICK(self
, self
.OnCellLeftClick
)
75 EVT_GRID_CELL_RIGHT_CLICK(self
, self
.OnCellRightClick
)
76 EVT_GRID_CELL_LEFT_DCLICK(self
, self
.OnCellLeftDClick
)
77 EVT_GRID_CELL_RIGHT_DCLICK(self
, self
.OnCellRightDClick
)
79 EVT_GRID_LABEL_LEFT_CLICK(self
, self
.OnLabelLeftClick
)
80 EVT_GRID_LABEL_RIGHT_CLICK(self
, self
.OnLabelRightClick
)
81 EVT_GRID_LABEL_LEFT_DCLICK(self
, self
.OnLabelLeftDClick
)
82 EVT_GRID_LABEL_RIGHT_DCLICK(self
, self
.OnLabelRightDClick
)
84 EVT_GRID_ROW_SIZE(self
, self
.OnRowSize
)
85 EVT_GRID_COL_SIZE(self
, self
.OnColSize
)
87 EVT_GRID_RANGE_SELECT(self
, self
.OnRangeSelect
)
88 EVT_GRID_CELL_CHANGE(self
, self
.OnCellChange
)
89 EVT_GRID_SELECT_CELL(self
, self
.OnSelectCell
)
91 EVT_GRID_EDITOR_SHOWN(self
, self
.OnEditorShown
)
92 EVT_GRID_EDITOR_HIDDEN(self
, self
.OnEditorHidden
)
93 EVT_GRID_EDITOR_CREATED(self
, self
.OnEditorCreated
)
97 def OnCellLeftClick(self
, evt
):
98 self
.log
.write("OnCellLeftClick: (%d,%d) %s\n" %
99 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
102 def OnCellRightClick(self
, evt
):
103 self
.log
.write("OnCellRightClick: (%d,%d) %s\n" %
104 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
107 def OnCellLeftDClick(self
, evt
):
108 self
.log
.write("OnCellLeftDClick: (%d,%d) %s\n" %
109 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
112 def OnCellRightDClick(self
, evt
):
113 self
.log
.write("OnCellRightDClick: (%d,%d) %s\n" %
114 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
117 def OnLabelLeftClick(self
, evt
):
118 self
.log
.write("OnLabelLeftClick: (%d,%d) %s\n" %
119 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
122 def OnLabelRightClick(self
, evt
):
123 self
.log
.write("OnLabelRightClick: (%d,%d) %s\n" %
124 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
127 def OnLabelLeftDClick(self
, evt
):
128 self
.log
.write("OnLabelLeftDClick: (%d,%d) %s\n" %
129 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
132 def OnLabelRightDClick(self
, evt
):
133 self
.log
.write("OnLabelRightDClick: (%d,%d) %s\n" %
134 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
138 def OnRowSize(self
, evt
):
139 self
.log
.write("OnRowSize: row %d, %s\n" %
140 (evt
.GetRowOrCol(), evt
.GetPosition()))
143 def OnColSize(self
, evt
):
144 self
.log
.write("OnColSize: col %d, %s\n" %
145 (evt
.GetRowOrCol(), evt
.GetPosition()))
148 def OnRangeSelect(self
, evt
):
150 self
.log
.write("OnRangeSelect: top-left %s, bottom-right %s\n" %
151 (evt
.GetTopLeftCoords(), evt
.GetBottomRightCoords()))
155 def OnCellChange(self
, evt
):
156 self
.log
.write("OnCellChange: (%d,%d) %s\n" %
157 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
159 # Show how to stay in a cell that has bad data. We can't just
160 # call SetGridCursor here since we are nested inside one so it
161 # won't have any effect. Instead, set coordinants to move to in
163 value
= self
.GetCellValue(evt
.GetRow(), evt
.GetCol())
164 if value
== 'no good':
165 self
.moveTo
= evt
.GetRow(), evt
.GetCol()
168 def OnIdle(self
, evt
):
169 if self
.moveTo
!= None:
170 self
.SetGridCursor(self
.moveTo
[0], self
.moveTo
[1])
175 def OnSelectCell(self
, evt
):
176 self
.log
.write("OnSelectCell: (%d,%d) %s\n" %
177 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
179 # Another way to stay in a cell that has a bad value...
180 row
= self
.GetGridCursorRow()
181 col
= self
.GetGridCursorCol()
182 if self
.IsCellEditControlEnabled():
183 self
.HideCellEditControl()
184 self
.DisableCellEditControl()
185 value
= self
.GetCellValue(row
, col
)
186 if value
== 'no good 2':
187 return # cancels the cell selection
191 def OnEditorShown(self
, evt
):
192 if evt
.GetRow() == 6 and evt
.GetCol() == 3 and \
193 wxMessageBox("Are you sure you wish to edit this cell?",
194 "Checking", wxYES_NO
) == wxNO
:
198 self
.log
.write("OnEditorShown: (%d,%d) %s\n" %
199 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
203 def OnEditorHidden(self
, evt
):
204 if evt
.GetRow() == 6 and evt
.GetCol() == 3 and \
205 wxMessageBox("Are you sure you wish to finish editing this cell?",
206 "Checking", wxYES_NO
) == wxNO
:
210 self
.log
.write("OnEditorHidden: (%d,%d) %s\n" %
211 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
215 def OnEditorCreated(self
, evt
):
216 self
.log
.write("OnEditorCreated: (%d, %d) %s\n" %
217 (evt
.GetRow(), evt
.GetCol(), evt
.GetControl()))
221 #---------------------------------------------------------------------------
223 class TestFrame(wxFrame
):
224 def __init__(self
, parent
, log
):
225 wxFrame
.__init
__(self
, parent
, -1, "Simple Grid Demo", size
=(640,480))
226 grid
= SimpleGrid(self
, log
)
230 #---------------------------------------------------------------------------
232 if __name__
== '__main__':
234 app
= wxPySimpleApp()
235 frame
= TestFrame(None, sys
.stdout
)
240 #---------------------------------------------------------------------------