I am writing a grid cell editor with a ComboBox. When I open (activate) the editor, I see strange behavior.
What could be the problem?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx
import wx.grid
class GridCellComboBoxEditor(wx.grid.GridCellEditor):
def __init__(self, choices):
super().__init__()
self.choices = choices
def Create(self, parent, id, evtHandler):
self.control = wx.ComboBox(parent, id, choices=self.choices)
self.SetControl(self.control)
if evtHandler:
self.control.PushEventHandler(evtHandler)
def Clone(self):
return GridCellComboBoxEditor(self.choices)
def BeginEdit(self, row, col, grid):
self.startValue = grid.GetTable().GetValue(row, col)
pos = self.control.FindString(self.startValue)
if pos == wx.NOT_FOUND:
pos = 0
self.control.SetSelection(pos)
def EndEdit(self, row, col, grid, oldval):
self.endValue = self.control.GetValue()
if self.endValue != oldval:
return self.endValue
else:
return None
def ApplyEdit(self, row, col, grid):
grid.GetTable().SetValue(row, col, self.endValue)
def Reset(self):
self.control.SetStringSelection(self.startValue)
self.control.SetInsertionPointEnd()
if __name__ == '__main__':
class Frame(wx.Frame):
def __init__(self, parent):
super().__init__(parent, title='Test GridCellComboBoxEditor')
vbox = wx.BoxSizer(wx.VERTICAL)
grid = wx.grid.Grid(self, size=(256, 128))
vbox.Add(grid, flag=wx.ALL, border=5)
self.SetSizer(vbox)
self.Layout()
grid.CreateGrid(4, 2)
table = grid.GetTable() # type: wx.grid.GridTableBase
table.SetValue(0, 0, "Choice1")
table.SetValue(1, 0, "Choice2")
choices = ['Choice1', 'Choice2', 'Choice3', 'Choice4', 'Choice5']
grid.SetCellEditor(0, 0, GridCellComboBoxEditor(choices))
grid.SetCellEditor(1, 0, GridCellComboBoxEditor(choices))
grid.SetCellEditor(2, 0, GridCellComboBoxEditor(choices))
grid.SetCellEditor(3, 0, GridCellComboBoxEditor(choices))
app = wx.App()
frame = Frame(None)
frame.Show()
app.MainLoop()
It solved my problem
def Create(self, parent, id, evtHandler):
self.control = wx.ComboBox(parent, id, choices=self.choices)
self.SetControl(self.control)
newEventHandler = wx.EvtHandler()
if evtHandler:
self.control.PushEventHandler(newEventHandler)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.