自定义 GridCellEditor 崩溃 python

Custom GridCellEditor crash python

我正在尝试设置自定义 GridCellEditor 我的配置是 Windows 7-64 位,Python3.4.3-32 位,wxPython-Phoenix 3.0.3

我从本书 wxpython 中获取并改编了示例代码。

CellEditor 工作正常,但是当我尝试验证内容(使用 TAB,或单击网格上的其他位置)时,Python 崩溃...

从 Internet 借用的一些其他代码会造成相同的崩溃(例如 here

有人可以帮我解决这个问题吗?


import wx
import wx.grid
import string

class UpCaseCellEditor(wx.grid.GridCellEditor):
    def __init__(self):
        wx.grid.GridCellEditor.__init__(self)

    def Create(self, parent, id, evtHandler):
        self._tc = wx.TextCtrl(parent, id, "")
        self._tc.SetInsertionPoint(0)
        self.SetControl(self._tc)

        if evtHandler:
            self._tc.PushEventHandler(evtHandler)

        self._tc.Bind(wx.EVT_CHAR, self.OnChar)

    def SetSize(self, rect):
        self._tc.SetSize(rect.x, rect.y, rect.width+2, rect.height+2)

    def BeginEdit(self, row, col, grid):
        self.startValue = grid.GetTable().GetValue(row, col)
        self._tc.SetValue(self.startValue)
        self._tc.SetInsertionPointEnd()
        self._tc.SetFocus()
        self._tc.SetSelection(0, self._tc.GetLastPosition())

    def EndEdit(self, row, col, grid):
        changed = False
        val = self._tc.GetValue()
        if val != self.startValue:
            changed = True
            grid.GetTable().SetValue(row, col, val)
        self.startValue = ''
        self._tc.SetValue('')
        return changed

    def Reset(self):
        self._tc.SetValue(self.startValue)
        self._tc.SetInsertionPointEnd()

    def Clone(self):
        return UpCaseCellEditor()

    def StartingKey(self, evt):
        self.OnChar(evt)
        if evt.GetSkipped():
            self._tc.EmulateKeyPress(evt)

    def OnChar(self, evt):
        key = evt.GetKeyCode()
        if key > 255:
            evt.Skip()
            return
        char = chr(key)
        if char in string.ascii_letters:
            char = char.upper()
            self._tc.WriteText(char)
        else:
            evt.Skip()


class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Grid Editor",
                             size=(640,480))
        grid = wx.grid.Grid(self)
        grid.CreateGrid(50,50)
        grid.SetCellEditor(0, 0, UpCaseCellEditor())

app = wx.App()
frame = TestFrame()
frame.Show()
app.MainLoop()      

我试过 PyGridCellEditor(据说已弃用?)


>>> import wx
>>> wx.grid.PyGridCellEditor
class 'wx.core.deprecated.locals.DeprecatedClassProxy'

顺便说一句,我在使用 PyGridCellEditor 时遇到了同样的问题。由于 python 崩溃,我没有从闲置中得到任何回溯。我唯一的信息来自 Windows:

<pre>Signature du problème : Nom d’événement de problème: APPCRASH Nom de l’application: pythonw.exe Version de l’application: 0.0.0.0 Horodatage de l’application: 54ecf082 Nom du module par défaut: _core.pyd Version du module par défaut: 0.0.0.0 Horodateur du module par défaut: 55cb1a46 Code de l’exception: c0000005 Décalage de l’exception: 00012fa5 Version du système: 6.1.7601.2.1.0.256.48 Identificateur de paramètres régionaux: 2060 Information supplémentaire n° 1: 0a9e Information supplémentaire n° 2: 0a9e372d3b4ad19135b953a78882e789 Information supplémentaire n° 3: 0a9e Information supplémentaire n° 4: 0a9e372d3b4ad19135b953a78882e789 如果我可以做些什么来获得更多信息,请告诉我:)

好吧,我终于找到了解决方案: EndEdit 实际上需要一个位置参数 oldval 所以,更换

def EndEdit(self, row, col, grid):
</pre>
def EndEdit(self, row, col, grid, oldval):
</pre>
做的伎俩。我认为,也可以通过这个论点改进代码。
此外, ApplyEdit(self, row, col, grid) 应该被覆盖,这是对 table 进行实际更改的地方......