python:右键单击列表菜单未显示所选项目 [英] python: Right Click on list menu not showing item selected

查看:30
本文介绍了python:右键单击列表菜单未显示所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我不断努力学习有关 Python 的更多信息的过程中,我正在尝试向我的 mp3 管理器程序添加一个右键单击事件.当前有效的是它显示菜单和所有选项.不起作用的是从菜单中选择的功能没有按照我认为的那样执行.大部分代码取自另一个网站上的操作方法".

这里是右键菜单选项

menu_titles = ["从列表中删除选择","从系统中删除选择",移动选择","复制选择",打印选择"]menu_title_by_id = {}对于 menu_titles 中的标题:menu_title_by_id[ wxNewId() ] = 标题

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

def RightClickCb( self, event ):# 记录被点击的内容self.list_item_clicked = right_click_context = event.GetText()### 2. Launcher 创建 wxMenu.###菜单 = wxMenu()对于 menu_title_by_id.items() 中的 (id,title):### 3. 带有附加的启动包菜单.###menu.Append( id, title )### 4. Launcher 在菜单上使用 EVT_MENU 注册菜单处理程序.###EVT_MENU(菜单,id,self.MenuSelectionCb)### 5. Launcher 显示带有调用 PopupMenu 的菜单,在源组件上调用,传递事件的 GetPoint.###self.MainPanel.PopupMenu( menu, event.GetPoint() )menu.Destroy() # 销毁以避免内存泄漏def MenuSelectionCb( self, event ):# 做点什么operation = menu_title_by_id[ event.GetId() ]目标 = self.list_item_clicked打印 '在 "%(target)s." 上执行 "%(operation)s".' % vars()

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

在<这里选择的数据>"上执行打印选择"

我得到的是

在."上执行打印选择"

如何从我选择作为右键单击事件的一部分的项目中获取数据?

解决方案

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

请参阅此处

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

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

此外,如果您将所有 id 绑定到同一个函数,您只需为所有 id 绑定一次:

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

In my ongoing effort to learn more about python, I am trying to add a right click event to my mp3 manager program. What currently works is that it shows the menu and all of the options. What is not working is the functions selected from the menu are not executing as I think they should be. Much of this code was taken from a 'how to' on another site.

Here are the right click menu options

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

The code that is run when the right click event happens

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()

What I expect to get when I do a right-click and then select one of the options in the menu is the output

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

What I am getting is

Perform "Print Selection" on "."

How do I get the data from the item I have selected as part of my right click event?

解决方案

Maybe you should use event.GetString() in place of event.GetText()

See here

Your code seems outdated tho, binding to events should be done like this:

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

moreover if you bind all ids to the same function you can just bind once for all ids:

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

这篇关于python:右键单击列表菜单未显示所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆