]>
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
)
67 def OnCellLeftClick(self
, evt
):
68 self
.log
.write("OnCellLeftClick: (%d,%d) %s\n" %
69 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
72 def OnCellRightClick(self
, evt
):
73 self
.log
.write("OnCellRightClick: (%d,%d) %s\n" %
74 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
77 def OnCellLeftDClick(self
, evt
):
78 self
.log
.write("OnCellLeftDClick: (%d,%d) %s\n" %
79 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
82 def OnCellRightDClick(self
, evt
):
83 self
.log
.write("OnCellRightDClick: (%d,%d) %s\n" %
84 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
87 def OnLabelLeftClick(self
, evt
):
88 self
.log
.write("OnLabelLeftClick: (%d,%d) %s\n" %
89 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
92 def OnLabelRightClick(self
, evt
):
93 self
.log
.write("OnLabelRightClick: (%d,%d) %s\n" %
94 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
97 def OnLabelLeftDClick(self
, evt
):
98 self
.log
.write("OnLabelLeftDClick: (%d,%d) %s\n" %
99 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
102 def OnLabelRightDClick(self
, evt
):
103 self
.log
.write("OnLabelRightDClick: (%d,%d) %s\n" %
104 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
108 def OnRowSize(self
, evt
):
109 self
.log
.write("OnRowSize: row %d, %s\n" %
110 (evt
.GetRowOrCol(), evt
.GetPosition()))
113 def OnColSize(self
, evt
):
114 self
.log
.write("OnColSize: col %d, %s\n" %
115 (evt
.GetRowOrCol(), evt
.GetPosition()))
118 def OnRangeSelect(self
, evt
):
120 self
.log
.write("OnRangeSelect: top-left %s, bottom-right %s\n" %
121 (evt
.GetTopLeftCoords(), evt
.GetBottomRightCoords()))
125 def OnCellChange(self
, evt
):
126 self
.log
.write("OnCellChange: (%d,%d) %s\n" %
127 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
129 # Show how to stay in a cell that has bad data. We can't just
130 # call SetGridCursor here since we are nested inside one so it
131 # won't have any effect. Instead, set coordinants to move to in
133 value
= self
.GetCellValue(evt
.GetRow(), evt
.GetCol())
134 if value
== 'no good':
135 self
.moveTo
= evt
.GetRow(), evt
.GetCol()
137 def OnIdle(self
, evt
):
138 if self
.moveTo
!= None:
139 self
.SetGridCursor(self
.moveTo
[0], self
.moveTo
[1])
144 def OnSelectCell(self
, evt
):
145 self
.log
.write("OnSelectCell: (%d,%d) %s\n" %
146 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
148 # Another way to stay in a cell that has a bad value...
149 row
= self
.GetGridCursorRow()
150 col
= self
.GetGridCursorCol()
151 if self
.IsCellEditControlEnabled():
152 self
.HideCellEditControl()
153 self
.DisableCellEditControl()
154 value
= self
.GetCellValue(row
, col
)
155 if value
== 'no good 2':
156 return # cancels the cell selection
162 def OnEditorShown(self
, evt
):
163 self
.log
.write("OnEditorShown: (%d,%d) %s\n" %
164 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
167 def OnEditorHidden(self
, evt
):
168 self
.log
.write("OnEditorHidden: (%d,%d) %s\n" %
169 (evt
.GetRow(), evt
.GetCol(), evt
.GetPosition()))
173 #---------------------------------------------------------------------------
175 class TestFrame(wxFrame
):
176 def __init__(self
, parent
, log
):
177 wxFrame
.__init
__(self
, parent
, -1, "Simple Grid Demo", size
=(640,480))
178 grid
= SimpleGrid(self
, log
)
182 #---------------------------------------------------------------------------
184 if __name__
== '__main__':
186 app
= wxPySimpleApp()
187 frame
= TestFrame(None, sys
.stdout
)
192 #---------------------------------------------------------------------------