自定义GridCellEditor的Focus Combobox开始编辑

时间:2015-12-23 12:05:40

标签: python python-2.7 wxpython

我正在创建一个自定义GridCellEditor,以便能够使用levenshtein算法并根据输入的用户值建议字符串。

class GridCellLevenshteinEditor(wx.grid.PyGridCellEditor):

    """A Grid Cell Editor with a combobox that offers choices sorted
    with the levenshtein algorithm."""

    def __init__(self, choices, allow_new_entries=False):
        wx.grid.PyGridCellEditor.__init__(self)
        # this holds all the possible strings, that can be selected
        self.choices = choices
        self.allow_new_entries = allow_new_entries

        self.start_value = None
        self.combobox = None

    def Create(self, parent, id, evt_handler):
        """Creates the actual edit control."""
        self.combobox = ComboBox(parent, None, self.choices, doSort=False,
                                 style=wx.CB_SIMPLE)
        self.SetControl(self.combobox)
        if evt_handler:
            self.combobox.PushEventHandler(evt_handler)

    def BeginEdit(self, row, col, grid):
        """Fetch the value from the table and prepare the edit control
        to begin editing.
        This function should save the original value of the grid cell at the
        given row and col and show the control allowing the user to
        change it."""
        self.start_value = grid.GetTable().GetValue(row, col)
        if self.start_value in ("", None):
            self.start_value = "test"
        self.combobox.ChangeValue(self.start_value)
        self.combobox.SetFocus()  # <-- this causes an issue

    def set_new_choices(self, new_choices):
        """Empties the Combobox Control and fills it with the new given
        choices. Can be used as well for example for updating the choices."""
        self.choices = new_choices
        self.combobox.refresh_combo_box(new_choices, False)

没有self.combobox.SetFocus(),这在Grid中看起来像这样:

LevenshteinEditor without Focus

现在我希望用户可以输入的ComboBox的文本字段在编辑开始时自动聚焦。所以我添加了self.combobox.SetFocus(),但这只会造成麻烦。 ComboBox的下拉列表将显示几毫秒,然后再次关闭,然后编辑过程自动结束。意味着我必须再次单击单元格以再次开始编辑过程,但由于SetFocus(),它将立即再次自动结束。

有没有人知道如何在开始网格单元的编辑过程时自动选择用户输入文本,而组合框不会以奇怪的方式运行?

1 个答案:

答案 0 :(得分:0)

我在这里找到了解决问题的方法:http://wxpython-users.1045709.n5.nabble.com/Combo-box-as-grid-cell-editor-td2360176.html

当我更改焦点时,默认事件处理程序认为我已完成编辑并调用了EndEdit()。因此,我必须通过添加PushEventHandler(wx.EvtHandler())来覆盖组合框编辑器的事件处理。

所以我不得不改变以下方法。

def Create(self, parent, dummy_id, dummy_evt_handler):
"""Creates the actual edit control."""
    self.combobox = ComboBox(parent, None, self.choices, doSort=False,
                             style=wx.CB_DROPDOWN | wx.TE_PROCESS_ENTER)
    self.SetControl(self.combobox)
    self.combobox.PushEventHandler(wx.EvtHandler())
    self.combobox.Bind(wx.EVT_TEXT_ENTER, self.process_enter,
                       self.combobox)

def process_enter(self, dummy_event):
    """This gets called, when the enter key was pressed in the combobox."""
    # end edit and select next cell
    self.grid.DisableCellEditControl()
    self.grid.MoveCursorRight(False)

def BeginEdit(self, row, col, grid):
    """Fetch the value from the table and prepare the edit control
    to begin editing.
    This function should save the original value of the grid cell at the
    given row and col and show the control allowing the user to
    change it."""
    self.grid = grid
    self.start_value = grid.GetTable().GetValue(row, col)
    self.combobox.ChangeValue(self.start_value)
    self.combobox.SetFocus()