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 # For linux the first range means number keys in main keyboard, and
87 # the second range means numeric keypad keys, with num-lock on.
88 if key
in range(48,58) or key
in range(326,336):
94 def StartingKey(self
, evt
):
96 If the editor is enabled by pressing keys on the grid, this will be
97 called to let the editor do something about that first key if desired.
99 key
= evt
.GetKeyCode()
100 print "StartingKey", key
102 if key
in [WXK_NUMPAD0
, WXK_NUMPAD1
, WXK_NUMPAD2
, WXK_NUMPAD3
, WXK_NUMPAD4
,
103 WXK_NUMPAD5
, WXK_NUMPAD6
, WXK_NUMPAD7
, WXK_NUMPAD8
, WXK_NUMPAD9
]:
104 ch
= ch
= chr(ord('0') + key
- WXK_NUMPAD0
)
106 elif key
< 256 and key
>= 0 and chr(key
) in string
.printable
:
108 if not evt
.ShiftDown():
109 ch
= string
.lower(ch
)
112 # Replace the text. Other option would be to append it.
113 # self._tc.AppendText(ch)
114 self
._tc
.SetValue(ch
)
115 self
._tc
.SetInsertionPointEnd()
127 Create a new object which is the copy of this one
131 return MyCellEditor()
134 class MyGrid(wxGrid
):
135 def __init__(self
, parent
, cust
):
136 wxGrid
.__init
__(self
, parent
, -1)
137 self
.CreateGrid(10, 10)
138 for column
in xrange(10):
139 self
.SetColSize(column
, 40)
141 self
.SetDefaultCellAlignment(wxCENTRE
, wxCENTRE
)
145 for column
in xrange(10):
146 # Note, we create attr and cell editor for each column
147 # otherwise segfault at exit, probably tries to free those
148 # multiple times from each column.
149 attr
= wxGridCellAttr()
150 attr
.SetEditor(MyCellEditor())
151 self
.SetColAttr(column
, attr
)
154 #----------------------------------------------------------------------------
156 class CardNoteBook(wxNotebook
):
157 def __init__(self
, parent
, id):
158 wxNotebook
.__init
__(self
, parent
, id)
160 for title
, cust
in [("Default", 0), ("Custom Cell Editor", 1)]:
161 win
= MyGrid(self
, cust
)
162 self
.AddPage(win
, title
)
164 EVT_NOTEBOOK_PAGE_CHANGED(self
, self
.GetId(), self
.OnPageChanged
)
165 EVT_KEY_DOWN(self
, self
.OnKeyDown
)
166 EVT_NAVIGATION_KEY(self
, self
.OnNavKey
)
169 def OnKeyDown(self
, evt
):
170 print 'CardNoteBook.OnKeyDown: ', evt
.GetKeyCode()
174 def OnNavKey(self
, evt
):
175 print 'CardNoteBook.OnNavKey:', evt
178 def OnPageChanged(self
, event
):
182 #----------------------------------------------------------------------------
184 class ScoreKeeper(wxFrame
):
185 def __init__(self
, parent
, id, title
):
186 wxFrame
.__init
__(self
, parent
, -1, title
, size
= (500, 400),
187 style
=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE
)
188 EVT_CLOSE(self
, self
.OnCloseWindow
)
191 self
.nb
= CardNoteBook(self
, -1)
192 #win=MyGrid(self, "GTK")
195 def OnCloseWindow(self
, event
):
201 def OnFileExit(self
, event
):
204 #----------------------------------------------------------------------------
208 frame
= ScoreKeeper(None, -1, "ScoreKeeper: (A Demonstration)")
210 self
.SetTopWindow(frame
)
215 #---------------------------------------------------------------------------