]>
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)
18 # simple cell formatting
19 self
.SetColSize(3, 200)
20 self
.SetRowSize(4, 45)
21 self
.SetCellValue(0, 0, "First cell")
22 self
.SetCellValue(1, 1, "Another cell")
23 self
.SetCellValue(2, 2, "Yet another cell")
24 self
.SetCellValue(3, 3, "This cell is read-only")
25 self
.SetCellFont(0, 0, wxFont(12, wxROMAN
, wxITALIC
, wxNORMAL
))
26 self
.SetCellTextColour(1, 1, wxRED
)
27 self
.SetCellBackgroundColour(2, 2, wxCYAN
)
28 self
.SetReadOnly(3, 3, true
)
30 self
.SetCellEditor(5, 0, wxGridCellNumberEditor())
31 self
.SetCellValue(5, 0, "123")
32 self
.SetCellEditor(6, 0, wxGridCellFloatEditor())
33 self
.SetCellValue(6, 0, "123.34")
35 # attribute objects let you keep a set of formatting values
36 # in one spot, and reuse them if needed
37 attr
= wxGridCellAttr()
38 attr
.SetTextColour(wxBLACK
)
39 attr
.SetBackgroundColour(wxRED
)
40 attr
.SetFont(wxFont(10, wxSWISS
, wxNORMAL
, wxBOLD
))
42 # you can set cell attributes for the whole row (or column)
43 self
.SetRowAttr(5, attr
)
45 self
.SetColLabelValue(0, "Custom")
46 self
.SetColLabelValue(1, "column")
47 self
.SetColLabelValue(2, "labels")
49 self
.SetColLabelAlignment(wxALIGN_LEFT
, wxALIGN_BOTTOM
)
52 EVT_GRID_CELL_LEFT_CLICK(self
, self
.OnCellLeftClick
)
53 EVT_GRID_CELL_RIGHT_CLICK(self
, self
.OnCellRightClick
)
54 EVT_GRID_CELL_LEFT_DCLICK(self
, self
.OnCellLeftDClick
)
55 EVT_GRID_CELL_RIGHT_DCLICK(self
, self
.OnCellRightDClick
)
57 EVT_GRID_LABEL_LEFT_CLICK(self
, self
.OnLabelLeftClick
)
58 EVT_GRID_LABEL_RIGHT_CLICK(self
, self
.OnLabelRightClick
)
59 EVT_GRID_LABEL_LEFT_DCLICK(self
, self
.OnLabelLeftDClick
)
60 EVT_GRID_LABEL_RIGHT_DCLICK(self
, self
.OnLabelRightDClick
)
62 EVT_GRID_ROW_SIZE(self
, self
.OnRowSize
)
63 EVT_GRID_COL_SIZE(self
, self
.OnColSize
)
65 EVT_GRID_RANGE_SELECT(self
, self
.OnRangeSelect
)
66 EVT_GRID_CELL_CHANGE(self
, self
.OnCellChange
)
67 EVT_GRID_SELECT_CELL(self
, self
.OnSelectCell
)
69 EVT_GRID_EDITOR_SHOWN(self
, self
.OnEditorShown
)
70 EVT_GRID_EDITOR_HIDDEN(self
, self
.OnEditorHidden
)
71 EVT_GRID_EDITOR_CREATED(self
, self
.OnEditorCreated
)
74 def OnCellLeftClick(self
, evt
):
75 self
.log
.write("OnCellLeftClick: (%d,%d) %s\n" %
76 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
79 def OnCellRightClick(self
, evt
):
80 self
.log
.write("OnCellRightClick: (%d,%d) %s\n" %
81 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
84 def OnCellLeftDClick(self
, evt
):
85 self
.log
.write("OnCellLeftDClick: (%d,%d) %s\n" %
86 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
89 def OnCellRightDClick(self
, evt
):
90 self
.log
.write("OnCellRightDClick: (%d,%d) %s\n" %
91 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
94 def OnLabelLeftClick(self
, evt
):
95 self
.log
.write("OnLabelLeftClick: (%d,%d) %s\n" %
96 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
99 def OnLabelRightClick(self
, evt
):
100 self
.log
.write("OnLabelRightClick: (%d,%d) %s\n" %
101 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
104 def OnLabelLeftDClick(self
, evt
):
105 self
.log
.write("OnLabelLeftDClick: (%d,%d) %s\n" %
106 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
109 def OnLabelRightDClick(self
, evt
):
110 self
.log
.write("OnLabelRightDClick: (%d,%d) %s\n" %
111 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
115 def OnRowSize(self
, evt
):
116 self
.log
.write("OnRowSize: row %d, %s\n" %
117 (evt
.GetRowOrCol(), evt
.GetPosition()))
120 def OnColSize(self
, evt
):
121 self
.log
.write("OnColSize: col %d, %s\n" %
122 (evt
.GetRowOrCol(), evt
.GetPosition()))
125 def OnRangeSelect(self
, evt
):
127 self
.log
.write("OnRangeSelect: top-left %s, bottom-right %s\n" %
128 (evt
.GetTopLeftCoords(), evt
.GetBottomRightCoords()))
132 def OnCellChange(self
, evt
):
133 self
.log
.write("OnCellChange: (%d,%d) %s\n" %
134 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
136 # Show how to stay in a cell that has bad data. We can't just
137 # call SetGridCursor here since we are nested inside one so it
138 # won't have any effect. Instead, set coordinants to move to in
140 value
= self
.GetCellValue(evt
.GetRow(), evt
.GetCol())
141 if value
== 'no good':
142 self
.moveTo
= evt
.GetRow(), evt
.GetCol()
145 def OnIdle(self
, evt
):
146 if self
.moveTo
!= None:
147 self
.SetGridCursor(self
.moveTo
[0], self
.moveTo
[1])
152 def OnSelectCell(self
, evt
):
153 self
.log
.write("OnSelectCell: (%d,%d) %s\n" %
154 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
156 # Another way to stay in a cell that has a bad value...
157 row
= self
.GetGridCursorRow()
158 col
= self
.GetGridCursorCol()
159 if self
.IsCellEditControlEnabled():
160 self
.HideCellEditControl()
161 self
.DisableCellEditControl()
162 value
= self
.GetCellValue(row
, col
)
163 if value
== 'no good 2':
164 return # cancels the cell selection
168 def OnEditorShown(self
, evt
):
169 self
.log
.write("OnEditorShown: (%d,%d) %s\n" %
170 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
173 def OnEditorHidden(self
, evt
):
174 self
.log
.write("OnEditorHidden: (%d,%d) %s\n" %
175 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
178 def OnEditorCreated(self
, evt
):
179 self
.log
.write("OnEditorCreated: (%d, %d) %s\n" %
180 (evt
.GetRow(), evt
.GetCol(), evt
.GetControl()))
184 #---------------------------------------------------------------------------
186 class TestFrame(wxFrame
):
187 def __init__(self
, parent
, log
):
188 wxFrame
.__init
__(self
, parent
, -1, "Simple Grid Demo", size
=(640,480))
189 grid
= SimpleGrid(self
, log
)
193 #---------------------------------------------------------------------------
195 if __name__
== '__main__':
197 app
= wxPySimpleApp()
198 frame
= TestFrame(None, sys
.stdout
)
203 #---------------------------------------------------------------------------