自定义网格单元编辑器。 ComboBox小部件的错误行为

问题描述 投票:1回答:1

我正在使用ComboBox编写网格单元编辑器。当我打开(激活)编辑器时,我看到奇怪的行为。

  • 如果未选择小部件中的文本,而我打开一个带有元素的列表,则此列表将立即关闭。
  • 如果选择了小部件中的文本,那么当您打开列表时,它将保留。

可能是什么问题?

#!/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()
python wxpython wxpython-phoenix
1个回答
1
投票

它解决了我的问题

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)
© www.soinside.com 2019 - 2024. All rights reserved.