为了更好地学习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 "."
如何从我在右键单击事件中选择的项目中获取数据?