5 #----------------------------------------------------------------------------
6 from wxPython
.wx
import *
7 from wxPython
.grid
import *
9 class MyCellEditor(wxPyGridCellEditor
):
11 A custom GridCellEditor that only accepts numbers.
12 Also tries to work around wxGTK anomaly that key cannot start edit.
15 def Create(self
, parent
, id, evtHandler
):
17 Called to create the control, which must derive from wxControl.
22 if wxPlatform
== '__WXMSW__':
23 theStyle
= wxTE_PROCESS_TAB | wxTE_MULTILINE | wxTE_NO_VSCROLL | wxTE_AUTO_SCROLL
24 #theStyle = wxTE_PROCESS_TAB | wxTE_PROCESS_ENTER
26 self
._tc
= wxTextCtrl(parent
, id, "", style
=theStyle
)
27 self
._tc
.SetInsertionPoint(0)
28 self
.SetControl(self
._tc
)
31 self
._tc
.PushEventHandler(evtHandler
)
34 def SetSize(self
, rect
):
36 Called to position/size the edit control within the cell rectangle.
37 If you don't fill the cell (the rect) then be sure to override
38 PaintBackground and do something meaningful there.
40 self
._tc
.SetDimensions(rect
.x
-1, rect
.y
-1, rect
.width
+4, rect
.height
+4)
42 def BeginEdit(self
, row
, col
, grid
):
44 Fetch the value from the table and prepare the edit control
45 to begin editing. Set the focus to the edit control.
49 self
.startValue
= grid
.GetTable().GetValue(row
, col
)
50 self
._tc
.SetValue(self
.startValue
)
53 def EndEdit(self
, row
, col
, grid
):
55 Complete the editing of the current cell. Returns true if the value
56 has changed. If necessary, the control may be destroyed.
60 val
= self
._tc
.GetValue()
61 if val
!= self
.startValue
:
63 grid
.GetTable().SetValue(row
, col
, val
) # update the table
72 Reset the value in the control back to its starting value.
75 self
._tc
.SetValue(self
.startValue
)
76 self
._tc
.SetInsertionPointEnd()
79 def IsAcceptedKey(self
, evt
):
81 Return TRUE to allow the given key to start editing: the base class
82 version only checks that the event has no modifiers.
84 key
= evt
.GetKeyCode()
86 if (key
in range(ord('0'),ord('9')+1) or
87 key
in range(WXK_NUMPAD0
, WXK_NUMPAD9
+1)):
93 def StartingKey(self
, evt
):
95 If the editor is enabled by pressing keys on the grid, this will be
96 called to let the editor do something about that first key if desired.
98 key
= evt
.GetKeyCode()
99 print "StartingKey", key
101 if key
in range(WXK_NUMPAD0
, WXK_NUMPAD9
+1):
102 ch
= ch
= chr(ord('0') + key
- WXK_NUMPAD0
)
104 elif key
< 256 and key
>= 0 and chr(key
) in string
.printable
:
106 if not evt
.ShiftDown():
110 # Replace the text. Other option would be to append it.
111 # self._tc.AppendText(ch)
112 self
._tc
.SetValue(ch
)
113 self
._tc
.SetInsertionPointEnd()
125 Create a new object which is the copy of this one
129 return MyCellEditor()
132 class MyGrid(wxGrid
):
133 def __init__(self
, parent
, cust
):
134 wxGrid
.__init
__(self
, parent
, -1)
135 self
.CreateGrid(10, 10)
136 for column
in xrange(10):
137 self
.SetColSize(column
, 40)
139 self
.SetDefaultCellAlignment(wxCENTRE
, wxCENTRE
)
143 for column
in xrange(10):
144 # Note, we create attr and cell editor for each column
145 # otherwise segfault at exit, probably tries to free those
146 # multiple times from each column.
147 attr
= wxGridCellAttr()
148 attr
.SetEditor(MyCellEditor())
149 self
.SetColAttr(column
, attr
)
152 #----------------------------------------------------------------------------
154 class CardNoteBook(wxNotebook
):
155 def __init__(self
, parent
, id):
156 wxNotebook
.__init
__(self
, parent
, id)
158 for title
, cust
in [("Default", 0), ("Custom Cell Editor", 1)]:
159 win
= MyGrid(self
, cust
)
160 self
.AddPage(win
, title
)
162 EVT_NOTEBOOK_PAGE_CHANGED(self
, self
.GetId(), self
.OnPageChanged
)
163 EVT_KEY_DOWN(self
, self
.OnKeyDown
)
164 EVT_NAVIGATION_KEY(self
, self
.OnNavKey
)
167 def OnKeyDown(self
, evt
):
168 print 'CardNoteBook.OnKeyDown: ', evt
.GetKeyCode()
172 def OnNavKey(self
, evt
):
173 print 'CardNoteBook.OnNavKey:', evt
176 def OnPageChanged(self
, event
):
180 #----------------------------------------------------------------------------
182 class ScoreKeeper(wxFrame
):
183 def __init__(self
, parent
, id, title
):
184 wxFrame
.__init
__(self
, parent
, -1, title
, size
= (500, 400),
185 style
=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE
)
186 EVT_CLOSE(self
, self
.OnCloseWindow
)
189 self
.nb
= CardNoteBook(self
, -1)
190 #win=MyGrid(self, "GTK")
193 def OnCloseWindow(self
, event
):
199 def OnFileExit(self
, event
):
202 #----------------------------------------------------------------------------
206 frame
= ScoreKeeper(None, -1, "ScoreKeeper: (A Demonstration)")
208 self
.SetTopWindow(frame
)
213 #---------------------------------------------------------------------------