]>
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")
41 EVT_GRID_CELL_LEFT_CLICK(self
, self
.OnCellLeftClick
)
42 EVT_GRID_CELL_RIGHT_CLICK(self
, self
.OnCellRightClick
)
43 EVT_GRID_CELL_LEFT_DCLICK(self
, self
.OnCellLeftDClick
)
44 EVT_GRID_CELL_RIGHT_DCLICK(self
, self
.OnCellRightDClick
)
46 EVT_GRID_LABEL_LEFT_CLICK(self
, self
.OnLabelLeftClick
)
47 EVT_GRID_LABEL_RIGHT_CLICK(self
, self
.OnLabelRightClick
)
48 EVT_GRID_LABEL_LEFT_DCLICK(self
, self
.OnLabelLeftDClick
)
49 EVT_GRID_LABEL_RIGHT_DCLICK(self
, self
.OnLabelRightDClick
)
51 EVT_GRID_ROW_SIZE(self
, self
.OnRowSize
)
52 EVT_GRID_COL_SIZE(self
, self
.OnColSize
)
54 EVT_GRID_RANGE_SELECT(self
, self
.OnRangeSelect
)
55 EVT_GRID_CELL_CHANGE(self
, self
.OnCellChange
)
56 EVT_GRID_SELECT_CELL(self
, self
.OnSelectCell
)
58 EVT_GRID_EDITOR_SHOWN(self
, self
.OnEditorShown
)
59 EVT_GRID_EDITOR_HIDDEN(self
, self
.OnEditorHidden
)
62 def OnCellLeftClick(self
, evt
):
63 self
.log
.write("OnCellLeftClick: (%d,%d) %s\n" %
64 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
67 def OnCellRightClick(self
, evt
):
68 self
.log
.write("OnCellRightClick: (%d,%d) %s\n" %
69 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
72 def OnCellLeftDClick(self
, evt
):
73 self
.log
.write("OnCellLeftDClick: (%d,%d) %s\n" %
74 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
77 def OnCellRightDClick(self
, evt
):
78 self
.log
.write("OnCellRightDClick: (%d,%d) %s\n" %
79 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
82 def OnLabelLeftClick(self
, evt
):
83 self
.log
.write("OnLabelLeftClick: (%d,%d) %s\n" %
84 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
87 def OnLabelRightClick(self
, evt
):
88 self
.log
.write("OnLabelRightClick: (%d,%d) %s\n" %
89 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
92 def OnLabelLeftDClick(self
, evt
):
93 self
.log
.write("OnLabelLeftDClick: (%d,%d) %s\n" %
94 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
97 def OnLabelRightDClick(self
, evt
):
98 self
.log
.write("OnLabelRightDClick: (%d,%d) %s\n" %
99 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
103 def OnRowSize(self
, evt
):
104 self
.log
.write("OnRowSize: row %d, %s\n" %
105 (evt
.GetRowOrCol(), evt
.GetPosition()))
108 def OnColSize(self
, evt
):
109 self
.log
.write("OnColSize: col %d, %s\n" %
110 (evt
.GetRowOrCol(), evt
.GetPosition()))
113 def OnRangeSelect(self
, evt
):
115 self
.log
.write("OnRangeSelect: top-left %s, bottom-right %s\n" %
116 (evt
.GetTopLeftCoords(), evt
.GetBottomRightCoords()))
120 def OnCellChange(self
, evt
):
121 self
.log
.write("OnCellChange: (%d,%d) %s\n" %
122 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
124 # Show how to stay in a cell that has bad data. We can't just
125 # call SetGridCursor here since we are nested inside one so it
126 # won't have any effect. Instead, set coordinants to move to in
128 value
= self
.GetCellValue(evt
.GetRow(), evt
.GetCol())
129 if value
== 'no good':
130 self
.moveTo
= evt
.GetRow(), evt
.GetCol()
132 def OnIdle(self
, evt
):
133 if self
.moveTo
!= None:
134 self
.SetGridCursor(self
.moveTo
[0], self
.moveTo
[1])
139 def OnSelectCell(self
, evt
):
140 self
.log
.write("OnSelectCell: (%d,%d) %s\n" %
141 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
143 # Another way to stay in a cell that has a bad value...
144 row
= self
.GetGridCursorRow()
145 col
= self
.GetGridCursorCol()
146 if self
.IsCellEditControlEnabled():
147 self
.HideCellEditControl()
148 self
.DisableCellEditControl()
149 value
= self
.GetCellValue(row
, col
)
150 if value
== 'no good 2':
151 return # cancels the cell selection
157 def OnEditorShown(self
, evt
):
158 self
.log
.write("OnEditorShown: (%d,%d) %s\n" %
159 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
162 def OnEditorHidden(self
, evt
):
163 self
.log
.write("OnEditorHidden: (%d,%d) %s\n" %
164 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
168 #---------------------------------------------------------------------------
170 class TestFrame(wxFrame
):
171 def __init__(self
, parent
, log
):
172 wxFrame
.__init
__(self
, parent
, -1, "Simple Grid Demo", size
=(640,480))
173 grid
= SimpleGrid(self
, log
)
177 #---------------------------------------------------------------------------
179 if __name__
== '__main__':
181 app
= wxPySimpleApp()
182 frame
= TestFrame(None, sys
.stdout
)
187 #---------------------------------------------------------------------------