[wxPython] Can't use keyboard of "enter" to input value on cell

Hi everyone
I’ve been referring to some online resources and attempting to create an application with editable cells using WxPython. The program runs smoothly when I use older versions of WxPython (WxPython 2.8) and Python (Python 2.6). However, when I tried upgrading to WxPython 4.0.4 and Python 3.8, the program no longer executes.

Here is my code, and I had changed old command in my code.

I would appreciate any advice or assistance you can provide:
`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)

    print(f'Create')

    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,

                           wx.SIZE_ALLOW_MINUS_ONE)

    print(f'SetSize')

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())

    print(f'BeginEdit')

def EndEdit(self, row, col, grid):

    changed = False

    val = self._tc.GetValue()

    if val != self.startValue:

        changed = True

        grid.GetTable().SetValue(row, col, val) # update the table

    self.startValue = ''

    self._tc.SetValue('')

    print(f'EndEdit')

    return changed

def ApplyEdit(self, row, col, grid):

    val = self._tc.GetValue()

    grid.GetTable().SetValue(row, col, val) # update the table

    # self.startValue = ''

    # self._tc.SetValue('')

    # print(f'ApplyEdit')

def Reset(self):

    self._tc.SetValue(self.startValue)

    self._tc.SetInsertionPointEnd()

    print(f'Reset')

def Clone(self):

    print(f'Clone')

    return UpCaseCellEditor()

def StartingKey(self, evt):

    self.OnChar(evt)

    if evt.GetSkipped():

        self._tc.EmulateKeyPress(evt)

    print(f'StartingKey')

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(5,5)

    grid.SetDefaultEditor(UpCaseCellEditor())

app = wx.App()

frame = TestFrame()

frame.Show()

app.MainLoop()`

Hi and welcome to Discuss wxPython,

I noticed that the signature you are using for the EndEdit() method is not correct for wxPython 4.

Below is the code for the example in the wxPython demo for wxPython 4.2.1 - see if it works in your example.

    def EndEdit(self, row, col, grid, oldVal):
        """
        End editing the cell.  This function must check if the current
        value of the editing control is valid and different from the
        original value (available as oldval in its string form.)  If
        it has not changed then simply return None, otherwise return
        the value in its string form.
        *Must Override*
        """
        val = self._tc.GetValue()
        if val != oldVal:   #self.startValue:
            return val
        else:
            return None
1 Like

Hi RichardT,
First of all, thank you very much for your response. I tried using the “Enter” key to save the value in the cell and exit the edit mode, but it seems that it still doesn’t work correctly after applying the code in your providing code. Could this error be related to a different version? Or do you have any other suggestions?

It’s possible that the version of wxPython could be causing other problems. When I replaced the EndEdit() code in your program with the code I posted above, I was able to complete the editing of a cell and move to the next cell below by pressing the Enter key (and also by using the Tab key). I am using Python 3.10.12 + wxPython 4.2.1 gtk3 (phoenix) wxWidgets 3.2.2.1 on Linux Mint 21.2.

I would certainly recommend upgrading to wxPython 4.2.1 if you are able, as it contains many improvements and bug fixes compared to 4.0.4.

EDIT: I came across the following thread:

In that thread @Robin said

It looks like several virtual methods are missing there. I’ve created an issue at GitHub to track work on this fix.

He raised an issue for it on GitHub:

That issue was fixed by:

The fix was merged on March 13, 2020. Therefore the earliest wxPython version it could have appeared in would be 4.1.0 (released April 24, 2020). Perhaps these missing virtual methods are causing your problem?

Good find, @RichardT. The above changes could very likely explain @WayneL issues with 4.0.4.

Hi everyone,
Thank you all for your responses.
I think it might be a wxPython version issue,it works ok after updating to wx 4.2.1.