在wx.ComboBox弹出窗口的悬停项的右侧添加“移除按钮”,可以通过自定义渲染器来实现。以下是一个实现的示例:
import wx
class MyComboPopup(wx.ComboPopup):
def __init__(self):
super().__init__()
def Create(self, parent):
self.list = wx.ListCtrl(parent, style=wx.LC_REPORT|wx.LC_SINGLE_SEL)
self.list.InsertColumn(0, 'Items')
self.list.InsertColumn(1, 'Remove')
self.list.SetColumnWidth(0, 150)
self.list.SetColumnWidth(1, 70)
self.list.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
self.list.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected)
self.list.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated)
self.list.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.list.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
self.list.Bind(wx.EVT_MOTION, self.OnMotion)
self.list.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindow)
self.list.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick)
self.list.Bind(wx.EVT_LIST_COL_RIGHT_CLICK, self.OnColRightClick)
self.list.Bind(wx.EVT_LIST_COL_BEGIN_DRAG, self.OnColBeginDrag)
self.list.Bind(wx.EVT_LIST_COL_DRAGGING, self.OnColDragging)
self.list.Bind(wx.EVT_LIST_COL_END_DRAG, self.OnColEndDrag)
self.list.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnBeginEdit)
self.list.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnEndEdit)
self.list.Bind(wx.EVT_LIST_DELETE_ITEM, self.OnDeleteItem)
self.list.Bind(wx.EVT_LIST_DELETE_ALL_ITEMS, self.OnDeleteAllItems)
self.list.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.OnRightClick)
self.list.Bind(wx.EVT_LIST_ITEM_MIDDLE_CLICK, self.OnMiddleClick)
self.list.Bind(wx.EVT_LIST_ITEM_FOCUSED, self.OnItemFocused)
self.list.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected)
self.list.Bind(wx.EVT_LIST_KEY_DOWN, self.OnKeyDown)
self.list.Bind(wx.EVT_LIST_INSERT_ITEM, self.OnInsertItem)
self.list.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.OnRightClick)
self.list.Bind(wx.EVT_LIST_ITEM_MIDDLE_CLICK, self.OnMiddleClick)
self.list.Bind(wx.EVT_LIST_ITEM_FOCUSED, self.OnItemFocused)
self.list.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected)
self.list.Bind(wx.EVT_LIST_KEY_DOWN, self.OnKeyDown)
self.list.Bind(wx.EVT_LIST_INSERT_ITEM, self.OnInsertItem)
def GetControl(self):
return self.list
def OnItemSelected(self, event):
item = event.GetItem()
index = item.GetId()
# 处理选中项的逻辑
pass
def OnItemDeselected(self, event):
item = event.GetItem()
index = item.GetId()
# 处理取消选中项的逻辑
pass
def OnItemActivated(self, event):
item = event.GetItem()
index = item.GetId()
# 处理双击选中项的逻辑
pass
def OnLeftDown(self, event):
# 处理鼠标左键按下的逻辑
pass
def OnLeftUp(self, event):
# 处理鼠标左键释放的逻辑
pass
def OnMotion(self, event):
# 处理鼠标移动的逻辑
pass
def OnLeaveWindow(self, event):
# 处理鼠标离开窗口的逻辑
pass
def OnColClick(self, event):
# 处理列点击的逻辑
pass
def OnColRightClick(self, event):
# 处理列右键点击的逻辑
pass
def OnColBeginDrag(self, event):
# 处理列开始拖拽的逻辑
pass
def OnColDragging(self, event):
# 处理列拖拽中的逻辑
pass
def OnColEndDrag(self, event):
# 处理列结束拖拽的逻辑
pass
def OnBeginEdit(self, event):
# 处理开始编辑的逻辑
pass
def OnEndEdit(self, event):
# 处理结束编辑的逻辑
pass
def OnDeleteItem(self, event):
# 处理删除项的逻辑
pass
def OnDeleteAllItems(self, event):
# 处理删除所有项的逻辑
pass
def OnRightClick(self, event):
# 处理右键点击的逻辑
pass
def OnMiddleClick(self, event):
# 处理中键点击的逻辑
pass
def OnItemFocused(self, event):
# 处理项获取焦点的逻辑
pass
def OnItemDeselected(self, event):
# 处理项取消选中的逻辑
pass
def OnKeyDown(self, event):
# 处理键盘按下的逻辑
pass
def OnInsertItem(self, event):
# 处理插入项的逻辑
pass
import wx
app = wx.App()
frame = wx.Frame(None, title='ComboBox Demo')
panel = wx.Panel(frame)
combo = wx.ComboBox(panel, style=wx.CB_DROPDOWN|wx.CB_READONLY)
popup = MyComboPopup()
combo.SetPopupControl(popup)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(combo, 0, wx.EXPAND|wx.ALL, 10)
panel.SetSizer(sizer)
frame.Show()
app.MainLoop()
通过以上步骤,你可以在wx.ComboBox弹出窗口的悬停项的右侧添加“移除按钮”。你可以根据需要在自定义渲染器的相关方法中实现具体的逻辑。
领取专属 10元无门槛券
手把手带您无忧上云