python - 自定义网格单元编辑器。ComboBox 小部件的不正确行为
问题描述
我正在用 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()
解决方案
它解决了我的问题
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)
推荐阅读
- python - 当文件夹路径包含“#”时,绘图在非默认 .html 查看器中打开
- regex - 匹配两个单独文本的正则表达式,也忽略其间的所有内容
- vim - Vim 中丢失的语法高亮显示,但仅限于某个文件名
- ansible - 两个ansible sesu成为任务生成错误
- spotfire - Spotfire 数据表 - 当 [Column] != "Value" 时通过表达式限制按需数据
- php - 减少用于二进制比较的 UTF-8 字符串
- laravel-5 - Linode 停止作曲家更新
- python - 背景按钮颜色不会改变 on_press
- sql - 针对链接服务器运行 OPENQUERY 并使用本地表中的数据的问题
- c++ - C++:无法将向量传递给构造函数