]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GridSimple.py
c073d48932d4783ca10368c23229e7b912e42e83
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
)
75 def OnCellLeftClick(self
, evt
):
76 self
.log
.write("OnCellLeftClick: (%d,%d) %s\n" %
77 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
80 def OnCellRightClick(self
, evt
):
81 self
.log
.write("OnCellRightClick: (%d,%d) %s\n" %
82 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
85 def OnCellLeftDClick(self
, evt
):
86 self
.log
.write("OnCellLeftDClick: (%d,%d) %s\n" %
87 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
90 def OnCellRightDClick(self
, evt
):
91 self
.log
.write("OnCellRightDClick: (%d,%d) %s\n" %
92 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
95 def OnLabelLeftClick(self
, evt
):
96 self
.log
.write("OnLabelLeftClick: (%d,%d) %s\n" %
97 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
100 def OnLabelRightClick(self
, evt
):
101 self
.log
.write("OnLabelRightClick: (%d,%d) %s\n" %
102 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
105 def OnLabelLeftDClick(self
, evt
):
106 self
.log
.write("OnLabelLeftDClick: (%d,%d) %s\n" %
107 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
110 def OnLabelRightDClick(self
, evt
):
111 self
.log
.write("OnLabelRightDClick: (%d,%d) %s\n" %
112 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
116 def OnRowSize(self
, evt
):
117 self
.log
.write("OnRowSize: row %d, %s\n" %
118 (evt
.GetRowOrCol(), evt
.GetPosition()))
121 def OnColSize(self
, evt
):
122 self
.log
.write("OnColSize: col %d, %s\n" %
123 (evt
.GetRowOrCol(), evt
.GetPosition()))
126 def OnRangeSelect(self
, evt
):
128 self
.log
.write("OnRangeSelect: top-left %s, bottom-right %s\n" %
129 (evt
.GetTopLeftCoords(), evt
.GetBottomRightCoords()))
133 def OnCellChange(self
, evt
):
134 self
.log
.write("OnCellChange: (%d,%d) %s\n" %
135 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
137 # Show how to stay in a cell that has bad data. We can't just
138 # call SetGridCursor here since we are nested inside one so it
139 # won't have any effect. Instead, set coordinants to move to in
141 value
= self
.GetCellValue(evt
.GetRow(), evt
.GetCol())
142 if value
== 'no good':
143 self
.moveTo
= evt
.GetRow(), evt
.GetCol()
146 def OnIdle(self
, evt
):
147 if self
.moveTo
!= None:
148 self
.SetGridCursor(self
.moveTo
[0], self
.moveTo
[1])
153 def OnSelectCell(self
, evt
):
154 self
.log
.write("OnSelectCell: (%d,%d) %s\n" %
155 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
157 # Another way to stay in a cell that has a bad value...
158 row
= self
.GetGridCursorRow()
159 col
= self
.GetGridCursorCol()
160 if self
.IsCellEditControlEnabled():
161 self
.HideCellEditControl()
162 self
.DisableCellEditControl()
163 value
= self
.GetCellValue(row
, col
)
164 if value
== 'no good 2':
165 return # cancels the cell selection
169 def OnEditorShown(self
, evt
):
170 self
.log
.write("OnEditorShown: (%d,%d) %s\n" %
171 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
174 def OnEditorHidden(self
, evt
):
175 self
.log
.write("OnEditorHidden: (%d,%d) %s\n" %
176 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
179 def OnEditorCreated(self
, evt
):
180 self
.log
.write("OnEditorCreated: (%d, %d) %s\n" %
181 (evt
.GetRow(), evt
.GetCol(), evt
.GetControl()))
185 #---------------------------------------------------------------------------
187 class TestFrame(wxFrame
):
188 def __init__(self
, parent
, log
):
189 wxFrame
.__init
__(self
, parent
, -1, "Simple Grid Demo", size
=(640,480))
190 grid
= SimpleGrid(self
, log
)
194 #---------------------------------------------------------------------------
196 if __name__
== '__main__':
198 app
= wxPySimpleApp()
199 frame
= TestFrame(None, sys
.stdout
)
204 #---------------------------------------------------------------------------