开始编辑时自定义 GridCellEditor 的焦点组合框
Focus Combobox of custom GridCellEditor on Begin Edit
我正在创建自定义 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()
这在网格中看起来像这样:
现在我想要 ComboBox 的文本字段,用户可以在其中键入,在编辑开始时自动聚焦。所以我添加了 self.combobox.SetFocus()
,但这只会造成麻烦。 ComboBox 的下拉列表将显示几毫秒,然后再次关闭,然后编辑过程自动结束。意味着我将不得不再次单击单元格以再次开始编辑过程,尽管由于 SetFocus() 的缘故,这将再次自行结束。
有谁知道如何让用户输入的文本在开始网格单元格的编辑过程时自动被选中,而组合框不会以奇怪的方式运行?
我在这里找到了问题的解决方案: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()
我正在创建自定义 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()
这在网格中看起来像这样:
现在我想要 ComboBox 的文本字段,用户可以在其中键入,在编辑开始时自动聚焦。所以我添加了 self.combobox.SetFocus()
,但这只会造成麻烦。 ComboBox 的下拉列表将显示几毫秒,然后再次关闭,然后编辑过程自动结束。意味着我将不得不再次单击单元格以再次开始编辑过程,尽管由于 SetFocus() 的缘故,这将再次自行结束。
有谁知道如何让用户输入的文本在开始网格单元格的编辑过程时自动被选中,而组合框不会以奇怪的方式运行?
我在这里找到了问题的解决方案: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()