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_MULTILINE | wxTE_NO_VSCROLL | wxTE_AUTO_SCROLL
25 self
._tc
= wxTextCtrl(parent
, id, "", style
=theStyle
)
26 self
._tc
.SetInsertionPoint(0)
27 self
.SetControl(self
._tc
)
30 self
._tc
.PushEventHandler(evtHandler
)
33 def SetSize(self
, rect
):
35 Called to position/size the edit control within the cell rectangle.
36 If you don't fill the cell (the rect) then be sure to override
37 PaintBackground and do something meaningful there.
39 self
._tc
.SetDimensions(rect
.x
-1, rect
.y
-1, rect
.width
+4, rect
.height
+4)
41 def BeginEdit(self
, row
, col
, grid
):
43 Fetch the value from the table and prepare the edit control
44 to begin editing. Set the focus to the edit control.
48 self
.startValue
= grid
.GetTable().GetValue(row
, col
)
49 self
._tc
.SetValue(self
.startValue
)
52 def EndEdit(self
, row
, col
, grid
):
54 Complete the editing of the current cell. Returns true if the value
55 has changed. If necessary, the control may be destroyed.
59 val
= self
._tc
.GetValue()
60 if val
!= self
.startValue
:
62 grid
.GetTable().SetValue(row
, col
, val
) # update the table
71 Reset the value in the control back to its starting value.
74 self
._tc
.SetValue(self
.startValue
)
75 self
._tc
.SetInsertionPointEnd()
78 ## def IsAcceptedKey(self, evt):
80 ## Return TRUE to allow the given key to start editing: the base class
81 ## version only checks that the event has no modifiers.
83 ## key = evt.GetKeyCode()
84 ## print "KeyCode:", key
85 ## # For linux the first range means number keys in main keyboard, and
86 ## # the second range means numeric keypad keys, with num-lock on.
87 ## if key in range(48,58) or key in range(326,336):
92 ## def StartingKey(self, evt):
94 ## If the editor is enabled by pressing keys on the grid, this will be
95 ## called to let the editor do something about that first key if desired.
97 ## key = evt.GetKeyCode()
98 ## print "StartingKey", key
100 ## if key in [WXK_NUMPAD0, WXK_NUMPAD1, WXK_NUMPAD2, WXK_NUMPAD3, WXK_NUMPAD4,
101 ## WXK_NUMPAD5, WXK_NUMPAD6, WXK_NUMPAD7, WXK_NUMPAD8, WXK_NUMPAD9]:
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():
107 ## ch = string.lower(ch)
109 ## if ch is not None:
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()
118 ## def Destroy(self):
119 ## """final cleanup"""
120 ## self.base_Destroy()
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 #---------------------------------------------------------------------------