python:右键单击列表菜单,不显示选中的项目

时间:2011-03-28 13:56:29

标签: python wxpython right-click

在我不断努力了解有关python的更多信息时,我正在尝试向我的mp3管理器程序添加一个右键单击事件。目前的工作原理是它显示菜单和所有选项。什么是无效的是从菜单中选择的功能没有像我认为的那样执行。这些代码大部分来自另一个网站的“如何”。

以下是右键菜单选项

menu_titles = ["Remove Selection from list",
               "Delete Selection from system",
               "Move Selection",
               "Copy Selection",
               "Print Selection"]

menu_title_by_id = {}
for title in menu_titles:
    menu_title_by_id[ wxNewId() ] = title

右键单击事件发生时运行的代码

def RightClickCb( self, event ):
    # record what was clicked
    self.list_item_clicked = right_click_context = event.GetText()

    ### 2. Launcher creates wxMenu. ###
    menu = wxMenu()
    for (id,title) in menu_title_by_id.items():
        ### 3. Launcher packs menu with Append. ###
        menu.Append( id, title )
        ### 4. Launcher registers menu handlers with EVT_MENU, on the menu. ###
        EVT_MENU( menu, id, self.MenuSelectionCb )

    ### 5. Launcher displays menu with call to PopupMenu, invoked on the source component, passing event's GetPoint. ###
    self.MainPanel.PopupMenu( menu, event.GetPoint() )
    menu.Destroy() # destroy to avoid mem leak

def MenuSelectionCb( self, event ):
    # do something
    operation = menu_title_by_id[ event.GetId() ]
    target    = self.list_item_clicked
    print 'Perform "%(operation)s" on "%(target)s."' % vars()

当我右键单击然后选择菜单中的一个选项时,我希望得到的是输出

Perform "Print Selection" on "<data about the selection here>"

我得到的是

Perform "Print Selection" on "."

如何从我在右键单击事件中选择的项目中获取数据?

2 个答案:

答案 0 :(得分:1)

也许您应该使用event.GetString()代替event.GetText()

请参阅here

你的代码似乎过时了,对事件的绑定应该像这样:

menu.Bind(wx.EVT_MENU, self.MenuSelectionCb, id=id)

此外,如果将所有ID绑定到同一个函数,则可以为所有ID绑定一次:

menu.Bind(wx.EVT_MENU, self.MenuSelectionCb)

答案 1 :(得分:0)

您可以在Python: Right click on objectlistview not showing item name selected下找到一个解决方案,其中建议使用objectlistview的GetSelectedObject()方法。

相关问题