]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GridSimple.py
1 from wxPython
.wx
import *
2 from wxPython
.grid
import *
4 #---------------------------------------------------------------------------
6 class SimpleGrid(wxGrid
):
7 def __init__(self
, parent
, log
):
8 wxGrid
.__init
__(self
, parent
, -1)
12 EVT_IDLE(self
, self
.OnIdle
)
14 self
.CreateGrid(25, 25)
16 # simple cell formatting
17 self
.SetColSize(3, 200)
18 self
.SetRowSize(4, 45)
19 self
.SetCellValue(0, 0, "First cell")
20 self
.SetCellValue(1, 1, "Another cell")
21 self
.SetCellValue(2, 2, "Yet another cell")
22 self
.SetCellValue(3, 3, "This cell is read-only")
23 self
.SetCellFont(0, 0, wxFont(12, wxROMAN
, wxITALIC
, wxNORMAL
))
24 self
.SetCellTextColour(1, 1, wxRED
)
25 self
.SetCellBackgroundColour(2, 2, wxCYAN
)
26 self
.SetReadOnly(3, 3, true
)
29 # attribute objects let you keep a set of formatting values
30 # in one spot, and reuse them if needed
31 attr
= wxGridCellAttr()
32 attr
.SetTextColour(wxBLACK
)
33 attr
.SetBackgroundColour(wxRED
)
34 attr
.SetFont(wxFont(10, wxSWISS
, wxNORMAL
, wxBOLD
))
36 # you can set cell attributes for the whole row (or column)
37 self
.SetRowAttr(5, attr
)
39 self
.SetColLabelValue(0, "Custom")
40 self
.SetColLabelValue(1, "column")
41 self
.SetColLabelValue(2, "labels")
43 self
.SetColLabelAlignment(wxALIGN_LEFT
, wxALIGN_BOTTOM
)
46 EVT_GRID_CELL_LEFT_CLICK(self
, self
.OnCellLeftClick
)
47 EVT_GRID_CELL_RIGHT_CLICK(self
, self
.OnCellRightClick
)
48 EVT_GRID_CELL_LEFT_DCLICK(self
, self
.OnCellLeftDClick
)
49 EVT_GRID_CELL_RIGHT_DCLICK(self
, self
.OnCellRightDClick
)
51 EVT_GRID_LABEL_LEFT_CLICK(self
, self
.OnLabelLeftClick
)
52 EVT_GRID_LABEL_RIGHT_CLICK(self
, self
.OnLabelRightClick
)
53 EVT_GRID_LABEL_LEFT_DCLICK(self
, self
.OnLabelLeftDClick
)
54 EVT_GRID_LABEL_RIGHT_DCLICK(self
, self
.OnLabelRightDClick
)
56 EVT_GRID_ROW_SIZE(self
, self
.OnRowSize
)
57 EVT_GRID_COL_SIZE(self
, self
.OnColSize
)
59 EVT_GRID_RANGE_SELECT(self
, self
.OnRangeSelect
)
60 EVT_GRID_CELL_CHANGE(self
, self
.OnCellChange
)
61 EVT_GRID_SELECT_CELL(self
, self
.OnSelectCell
)
63 EVT_GRID_EDITOR_SHOWN(self
, self
.OnEditorShown
)
64 EVT_GRID_EDITOR_HIDDEN(self
, self
.OnEditorHidden
)
65 EVT_GRID_EDITOR_CREATED(self
, self
.OnEditorCreated
)
68 def OnCellLeftClick(self
, evt
):
69 self
.log
.write("OnCellLeftClick: (%d,%d) %s\n" %
70 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
73 def OnCellRightClick(self
, evt
):
74 self
.log
.write("OnCellRightClick: (%d,%d) %s\n" %
75 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
78 def OnCellLeftDClick(self
, evt
):
79 self
.log
.write("OnCellLeftDClick: (%d,%d) %s\n" %
80 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
83 def OnCellRightDClick(self
, evt
):
84 self
.log
.write("OnCellRightDClick: (%d,%d) %s\n" %
85 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
88 def OnLabelLeftClick(self
, evt
):
89 self
.log
.write("OnLabelLeftClick: (%d,%d) %s\n" %
90 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
93 def OnLabelRightClick(self
, evt
):
94 self
.log
.write("OnLabelRightClick: (%d,%d) %s\n" %
95 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
98 def OnLabelLeftDClick(self
, evt
):
99 self
.log
.write("OnLabelLeftDClick: (%d,%d) %s\n" %
100 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
103 def OnLabelRightDClick(self
, evt
):
104 self
.log
.write("OnLabelRightDClick: (%d,%d) %s\n" %
105 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
109 def OnRowSize(self
, evt
):
110 self
.log
.write("OnRowSize: row %d, %s\n" %
111 (evt
.GetRowOrCol(), evt
.GetPosition()))
114 def OnColSize(self
, evt
):
115 self
.log
.write("OnColSize: col %d, %s\n" %
116 (evt
.GetRowOrCol(), evt
.GetPosition()))
119 def OnRangeSelect(self
, evt
):
121 self
.log
.write("OnRangeSelect: top-left %s, bottom-right %s\n" %
122 (evt
.GetTopLeftCoords(), evt
.GetBottomRightCoords()))
126 def OnCellChange(self
, evt
):
127 self
.log
.write("OnCellChange: (%d,%d) %s\n" %
128 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
130 # Show how to stay in a cell that has bad data. We can't just
131 # call SetGridCursor here since we are nested inside one so it
132 # won't have any effect. Instead, set coordinants to move to in
134 value
= self
.GetCellValue(evt
.GetRow(), evt
.GetCol())
135 if value
== 'no good':
136 self
.moveTo
= evt
.GetRow(), evt
.GetCol()
138 def OnIdle(self
, evt
):
139 if self
.moveTo
!= None:
140 self
.SetGridCursor(self
.moveTo
[0], self
.moveTo
[1])
145 def OnSelectCell(self
, evt
):
146 self
.log
.write("OnSelectCell: (%d,%d) %s\n" %
147 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
149 # Another way to stay in a cell that has a bad value...
150 row
= self
.GetGridCursorRow()
151 col
= self
.GetGridCursorCol()
152 if self
.IsCellEditControlEnabled():
153 self
.HideCellEditControl()
154 self
.DisableCellEditControl()
155 value
= self
.GetCellValue(row
, col
)
156 if value
== 'no good 2':
157 return # cancels the cell selection
163 def OnEditorShown(self
, evt
):
164 self
.log
.write("OnEditorShown: (%d,%d) %s\n" %
165 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
168 def OnEditorHidden(self
, evt
):
169 self
.log
.write("OnEditorHidden: (%d,%d) %s\n" %
170 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
173 def OnEditorCreated(self
, evt
):
174 self
.log
.write("OnEditorCreated: (%d, %d) %s\n" %
175 (evt
.GetRow(), evt
.GetCol(), evt
.GetControl()))
179 #---------------------------------------------------------------------------
181 class TestFrame(wxFrame
):
182 def __init__(self
, parent
, log
):
183 wxFrame
.__init
__(self
, parent
, -1, "Simple Grid Demo", size
=(640,480))
184 grid
= SimpleGrid(self
, log
)
188 #---------------------------------------------------------------------------
190 if __name__
== '__main__':
192 app
= wxPySimpleApp()
193 frame
= TestFrame(None, sys
.stdout
)
198 #---------------------------------------------------------------------------