]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GridSimple.py
2f26955502a065180300ee951a2a3c5eb2da3a68
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 # attribute objects let you keep a set of formatting values
37 # in one spot, and reuse them if needed
38 attr
= wxGridCellAttr()
39 attr
.SetTextColour(wxBLACK
)
40 attr
.SetBackgroundColour(wxRED
)
41 attr
.SetFont(wxFont(10, wxSWISS
, wxNORMAL
, wxBOLD
))
43 # you can set cell attributes for the whole row (or column)
44 self
.SetRowAttr(5, attr
)
46 self
.SetColLabelValue(0, "Custom")
47 self
.SetColLabelValue(1, "column")
48 self
.SetColLabelValue(2, "labels")
50 self
.SetColLabelAlignment(wxALIGN_LEFT
, wxALIGN_BOTTOM
)
53 EVT_GRID_CELL_LEFT_CLICK(self
, self
.OnCellLeftClick
)
54 EVT_GRID_CELL_RIGHT_CLICK(self
, self
.OnCellRightClick
)
55 EVT_GRID_CELL_LEFT_DCLICK(self
, self
.OnCellLeftDClick
)
56 EVT_GRID_CELL_RIGHT_DCLICK(self
, self
.OnCellRightDClick
)
58 EVT_GRID_LABEL_LEFT_CLICK(self
, self
.OnLabelLeftClick
)
59 EVT_GRID_LABEL_RIGHT_CLICK(self
, self
.OnLabelRightClick
)
60 EVT_GRID_LABEL_LEFT_DCLICK(self
, self
.OnLabelLeftDClick
)
61 EVT_GRID_LABEL_RIGHT_DCLICK(self
, self
.OnLabelRightDClick
)
63 EVT_GRID_ROW_SIZE(self
, self
.OnRowSize
)
64 EVT_GRID_COL_SIZE(self
, self
.OnColSize
)
66 EVT_GRID_RANGE_SELECT(self
, self
.OnRangeSelect
)
67 EVT_GRID_CELL_CHANGE(self
, self
.OnCellChange
)
68 EVT_GRID_SELECT_CELL(self
, self
.OnSelectCell
)
70 EVT_GRID_EDITOR_SHOWN(self
, self
.OnEditorShown
)
71 EVT_GRID_EDITOR_HIDDEN(self
, self
.OnEditorHidden
)
72 EVT_GRID_EDITOR_CREATED(self
, self
.OnEditorCreated
)
76 def OnCellLeftClick(self
, evt
):
77 self
.log
.write("OnCellLeftClick: (%d,%d) %s\n" %
78 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
81 def OnCellRightClick(self
, evt
):
82 self
.log
.write("OnCellRightClick: (%d,%d) %s\n" %
83 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
86 def OnCellLeftDClick(self
, evt
):
87 self
.log
.write("OnCellLeftDClick: (%d,%d) %s\n" %
88 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
91 def OnCellRightDClick(self
, evt
):
92 self
.log
.write("OnCellRightDClick: (%d,%d) %s\n" %
93 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
96 def OnLabelLeftClick(self
, evt
):
97 self
.log
.write("OnLabelLeftClick: (%d,%d) %s\n" %
98 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
101 def OnLabelRightClick(self
, evt
):
102 self
.log
.write("OnLabelRightClick: (%d,%d) %s\n" %
103 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
106 def OnLabelLeftDClick(self
, evt
):
107 self
.log
.write("OnLabelLeftDClick: (%d,%d) %s\n" %
108 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
111 def OnLabelRightDClick(self
, evt
):
112 self
.log
.write("OnLabelRightDClick: (%d,%d) %s\n" %
113 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
117 def OnRowSize(self
, evt
):
118 self
.log
.write("OnRowSize: row %d, %s\n" %
119 (evt
.GetRowOrCol(), evt
.GetPosition()))
122 def OnColSize(self
, evt
):
123 self
.log
.write("OnColSize: col %d, %s\n" %
124 (evt
.GetRowOrCol(), evt
.GetPosition()))
127 def OnRangeSelect(self
, evt
):
129 self
.log
.write("OnRangeSelect: top-left %s, bottom-right %s\n" %
130 (evt
.GetTopLeftCoords(), evt
.GetBottomRightCoords()))
134 def OnCellChange(self
, evt
):
135 self
.log
.write("OnCellChange: (%d,%d) %s\n" %
136 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
138 # Show how to stay in a cell that has bad data. We can't just
139 # call SetGridCursor here since we are nested inside one so it
140 # won't have any effect. Instead, set coordinants to move to in
142 value
= self
.GetCellValue(evt
.GetRow(), evt
.GetCol())
143 if value
== 'no good':
144 self
.moveTo
= evt
.GetRow(), evt
.GetCol()
147 def OnIdle(self
, evt
):
148 if self
.moveTo
!= None:
149 self
.SetGridCursor(self
.moveTo
[0], self
.moveTo
[1])
154 def OnSelectCell(self
, evt
):
155 self
.log
.write("OnSelectCell: (%d,%d) %s\n" %
156 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
158 # Another way to stay in a cell that has a bad value...
159 row
= self
.GetGridCursorRow()
160 col
= self
.GetGridCursorCol()
161 if self
.IsCellEditControlEnabled():
162 self
.HideCellEditControl()
163 self
.DisableCellEditControl()
164 value
= self
.GetCellValue(row
, col
)
165 if value
== 'no good 2':
166 return # cancels the cell selection
170 def OnEditorShown(self
, evt
):
171 self
.log
.write("OnEditorShown: (%d,%d) %s\n" %
172 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
175 def OnEditorHidden(self
, evt
):
176 self
.log
.write("OnEditorHidden: (%d,%d) %s\n" %
177 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
180 def OnEditorCreated(self
, evt
):
181 self
.log
.write("OnEditorCreated: (%d, %d) %s\n" %
182 (evt
.GetRow(), evt
.GetCol(), evt
.GetControl()))
186 #---------------------------------------------------------------------------
188 class TestFrame(wxFrame
):
189 def __init__(self
, parent
, log
):
190 wxFrame
.__init
__(self
, parent
, -1, "Simple Grid Demo", size
=(640,480))
191 grid
= SimpleGrid(self
, log
)
195 #---------------------------------------------------------------------------
197 if __name__
== '__main__':
199 app
= wxPySimpleApp()
200 frame
= TestFrame(None, sys
.stdout
)
205 #---------------------------------------------------------------------------