Python 自定义网格单元编辑器。组合框小部件的行为不正确

Python 自定义网格单元编辑器。组合框小部件的行为不正确,python,wxpython,Python,Wxpython,我正在编写一个带有组合框的网格单元编辑器。当我打开(激活)编辑器时,我看到奇怪的行为 如果小部件中的文本未被选中,并且我用一个元素打开列表,那么该列表将立即关闭 如果小部件中的文本被选中,那么当您打开列表时,它将保持不变 有什么问题吗 #!/usr/bin/env python # -*- coding: utf-8 -*- import wx import wx.grid class GridCellComboBoxEditor(wx.grid.GridCellEditor):

我正在编写一个带有组合框的网格单元编辑器。当我打开(激活)编辑器时,我看到奇怪的行为

  • 如果小部件中的文本未被选中,并且我用一个元素打开列表,那么该列表将立即关闭
  • 如果小部件中的文本被选中,那么当您打开列表时,它将保持不变
有什么问题吗

#!/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()
它解决了我的问题

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)