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