]>
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
.SetCellFont(0, 0, wxFont(12, wxROMAN
, wxITALIC
, wxNORMAL
))
23 self
.SetCellTextColour(1, 1, wxRED
)
24 self
.SetCellBackgroundColour(2, 2, wxCYAN
)
26 # attribute objects let you keep a set of formatting values
27 # in one spot, and reuse them if needed
28 attr
= wxGridCellAttr()
29 attr
.SetTextColour(wxBLACK
)
30 attr
.SetBackgroundColour(wxRED
)
31 attr
.SetFont(wxFont(10, wxSWISS
, wxNORMAL
, wxBOLD
))
33 # you can set cell attributes for the whole row (or column)
34 self
.SetRowAttr(5, attr
)
36 self
.SetColLabelValue(0, "Custom")
37 self
.SetColLabelValue(1, "column")
38 self
.SetColLabelValue(2, "labels")
40 self
.SetColLabelAlignment(wxALIGN_LEFT
, wxALIGN_BOTTOM
)
43 EVT_GRID_CELL_LEFT_CLICK(self
, self
.OnCellLeftClick
)
44 EVT_GRID_CELL_RIGHT_CLICK(self
, self
.OnCellRightClick
)
45 EVT_GRID_CELL_LEFT_DCLICK(self
, self
.OnCellLeftDClick
)
46 EVT_GRID_CELL_RIGHT_DCLICK(self
, self
.OnCellRightDClick
)
48 EVT_GRID_LABEL_LEFT_CLICK(self
, self
.OnLabelLeftClick
)
49 EVT_GRID_LABEL_RIGHT_CLICK(self
, self
.OnLabelRightClick
)
50 EVT_GRID_LABEL_LEFT_DCLICK(self
, self
.OnLabelLeftDClick
)
51 EVT_GRID_LABEL_RIGHT_DCLICK(self
, self
.OnLabelRightDClick
)
53 EVT_GRID_ROW_SIZE(self
, self
.OnRowSize
)
54 EVT_GRID_COL_SIZE(self
, self
.OnColSize
)
56 EVT_GRID_RANGE_SELECT(self
, self
.OnRangeSelect
)
57 EVT_GRID_CELL_CHANGE(self
, self
.OnCellChange
)
58 EVT_GRID_SELECT_CELL(self
, self
.OnSelectCell
)
60 EVT_GRID_EDITOR_SHOWN(self
, self
.OnEditorShown
)
61 EVT_GRID_EDITOR_HIDDEN(self
, self
.OnEditorHidden
)
64 def OnCellLeftClick(self
, evt
):
65 self
.log
.write("OnCellLeftClick: (%d,%d) %s\n" %
66 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
69 def OnCellRightClick(self
, evt
):
70 self
.log
.write("OnCellRightClick: (%d,%d) %s\n" %
71 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
74 def OnCellLeftDClick(self
, evt
):
75 self
.log
.write("OnCellLeftDClick: (%d,%d) %s\n" %
76 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
79 def OnCellRightDClick(self
, evt
):
80 self
.log
.write("OnCellRightDClick: (%d,%d) %s\n" %
81 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
84 def OnLabelLeftClick(self
, evt
):
85 self
.log
.write("OnLabelLeftClick: (%d,%d) %s\n" %
86 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
89 def OnLabelRightClick(self
, evt
):
90 self
.log
.write("OnLabelRightClick: (%d,%d) %s\n" %
91 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
94 def OnLabelLeftDClick(self
, evt
):
95 self
.log
.write("OnLabelLeftDClick: (%d,%d) %s\n" %
96 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
99 def OnLabelRightDClick(self
, evt
):
100 self
.log
.write("OnLabelRightDClick: (%d,%d) %s\n" %
101 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
105 def OnRowSize(self
, evt
):
106 self
.log
.write("OnRowSize: row %d, %s\n" %
107 (evt
.GetRowOrCol(), evt
.GetPosition()))
110 def OnColSize(self
, evt
):
111 self
.log
.write("OnColSize: col %d, %s\n" %
112 (evt
.GetRowOrCol(), evt
.GetPosition()))
115 def OnRangeSelect(self
, evt
):
117 self
.log
.write("OnRangeSelect: top-left %s, bottom-right %s\n" %
118 (evt
.GetTopLeftCoords(), evt
.GetBottomRightCoords()))
122 def OnCellChange(self
, evt
):
123 self
.log
.write("OnCellChange: (%d,%d) %s\n" %
124 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
126 # Show how to stay in a cell that has bad data. We can't just
127 # call SetGridCursor here since we are nested inside one so it
128 # won't have any effect. Instead, set coordinants to move to in
130 value
= self
.GetCellValue(evt
.GetRow(), evt
.GetCol())
131 if value
== 'no good':
132 self
.moveTo
= evt
.GetRow(), evt
.GetCol()
134 def OnIdle(self
, evt
):
135 if self
.moveTo
!= None:
136 self
.SetGridCursor(self
.moveTo
[0], self
.moveTo
[1])
141 def OnSelectCell(self
, evt
):
142 self
.log
.write("OnSelectCell: (%d,%d) %s\n" %
143 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
145 # Another way to stay in a cell that has a bad value...
146 row
= self
.GetGridCursorRow()
147 col
= self
.GetGridCursorCol()
148 if self
.IsCellEditControlEnabled():
149 self
.HideCellEditControl()
150 self
.DisableCellEditControl()
151 value
= self
.GetCellValue(row
, col
)
152 if value
== 'no good 2':
153 return # cancels the cell selection
159 def OnEditorShown(self
, evt
):
160 self
.log
.write("OnEditorShown: (%d,%d) %s\n" %
161 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
164 def OnEditorHidden(self
, evt
):
165 self
.log
.write("OnEditorHidden: (%d,%d) %s\n" %
166 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
170 #---------------------------------------------------------------------------
172 class TestFrame(wxFrame
):
173 def __init__(self
, parent
, log
):
174 wxFrame
.__init
__(self
, parent
, -1, "Simple Grid Demo", size
=(640,480))
175 grid
= SimpleGrid(self
, log
)
179 #---------------------------------------------------------------------------
181 if __name__
== '__main__':
183 app
= wxPySimpleApp()
184 frame
= TestFrame(None, sys
.stdout
)
189 #---------------------------------------------------------------------------