我是 wxPython 的新手,所以请温柔一点。
我正在尝试制作当用户按下鼠标右键时通过上下文菜单弹出来控制的虚拟列表控件。
根据我的一点点经验,虚拟列表控件似乎喜欢使用“项目”(阅读:“行”)和“列”数字进行操作。很公平。
当我收到一个右键单击事件时,我可以通过调用 event.GetIndex() 轻松获取行(项目)编号。但是如何获取被点击对象的列号呢?
import wx
import wx.lib.agw.ultimatelistctrl as ULC
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Right-click example")
self.list = MyListCtrl(parent=self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.list, 1, wx.EXPAND)
self.SetSizer(sizer)
class MyListCtrl(ULC.UltimateListCtrl):
def __init__(self, parent, *args, **kwargs):
ULC.UltimateListCtrl.__init__(self, parent, 1, agwStyle=wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES)
self.InsertColumn(0, "Column0")
self.InsertColumn(1, "Column1")
self.SetItemCount(5)
# Bindings
self.Bind(ULC.EVT_LIST_ITEM_RIGHT_CLICK, self.OnRightClick)
def OnGetItemText(self, item, column):
return "%d, %d" % (item, column)
def OnGetItemToolTip(self, item, column):
pass
def OnGetItemTextColour(self, item, column):
pass
def OnRightClick(self, event):
# Get the index (i.e. which row was clicked)
print("OnColRightClick: GetIndex = %r\n" %(event.GetIndex()))
# How can I get which column was clicked?
if __name__ == "__main__":
# Start the GUI
app = wx.App()
frame = MyFrame()
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()