Adding context menu to listitems in an addon?
#1
I cannot figure out how to add context menu items to my list items. At least I have been unable to open said context menu after creating them.

What I'm doing is basically something like this:
Code:
listitem = xbmcgui.ListItem()

#Add some properties to listitem here...

listitem.addContextMenuItems([('Some label here', 'XBMC.RunScript(SomeScriptHere')])

# Do some stuff here...

list.addItem(listitem)

This code does not seem to throw any errors as far as I can see. However, when pressing right-clicking on the list item or pressing 'C' while the item is focused does not bring up a context menu. Indeed, nothing at all happens.

Am I missing something here?
Reply
#2
post a working code to see better
Reply
#3
ppic Wrote:post a working code to see better

As you wish; I have made a minimum working example that displays the issue I'm having.

http://quak.es/tmp/script.example.example.zip

As you can see, I here create two list items in a list, and add a context menu item to each. However, I can not manage to make them actually display the context menu.
Reply
#4
I could really use some help here, guys.
Reply
#5
from another post

commands = []
commands.append(( 'runme', 'XBMC.RunPlugin(plugin://video/myplugin)', ))
commands.append(( 'runother', 'XBMC.RunPlugin(plugin://video/otherplugin)', ))
listitem = xbmcgui.ListItem()
listitem.addContextMenuItems( commands )
Reply
#6
fekker Wrote:from another post

commands = []
commands.append(( 'runme', 'XBMC.RunPlugin(plugin://video/myplugin)', ))
commands.append(( 'runother', 'XBMC.RunPlugin(plugin://video/otherplugin)', ))
listitem = xbmcgui.ListItem()
listitem.addContextMenuItems( commands )

This is more or less exactly what I'm doing (see referenced example). No dice.
Reply
#7
Actually you're doing something different. In the example posted by fekker, commands is a python list that contains two tuples, each tuple containing two strings. In your example you created a python list containing two strings.

So I assume when addContextMenuItems loops over the supplied list, it assumes that each element in the list is a tuple containing:

1. The label to use in the context menu
2. The path to the callable script

That being said, I'm still early in my own plugin development learning curve, so I can't confirm if either example works.
Reply
#8
Ok - just downloaded the sample code and I see you changed it to be a tuple within a list, so ignore my earlier comments.

However, python has some quirky behavior with single element data structures, so does sample work like this?

Code:
        list = self.getControl(5010)
        listitem = xbmcgui.ListItem("One item")
        listitem.addContextMenuItems([('Context', 'XBMC.RunScript()',),])
        list.addItem(listitem)
        
        listitem = xbmcgui.ListItem("Another item")
        listitem.addContextMenuItems([('Context', 'XBMC.RunScript()',),])
        list.addItem(listitem)
Reply
#9
ackbarr Wrote:Actually you're doing something different. In the example posted by fekker, commands is a python list that contains two tuples, each tuple containing two strings. In your example you created a python list containing two strings.

So I assume when addContextMenuItems loops over the supplied list, it assumes that each element in the list is a tuple containing:

1. The label to use in the context menu
2. The path to the callable script

That being said, I'm still early in my own plugin development learning curve, so I can't confirm if either example works.

Ah, indeed, I see that in my short example in the original post I made that mistake. However, if you downloaded the script.example.example you'll see that I have in fact made a list of tuples.

Code:
listitem.addContextMenuItems([('Context', 'XBMC.RunScript()')])

Still did not work. I will correct my original post, at least.

EDIT: Just saw that you had posted again... I will try your suggestions.

EDIT 2: No dice. I think the quirky behavior you're referring to is trying to make a tuple with only one element ('foo'), in which case you need to add a comma ('foo',). Doesn't apply in this case though.
Reply
#10
Code:
commands = []
argsToPass = "id=406777id2=605784"
scriptToRun = "special://home/addons/plugin.video.xbmcflicks/resources/lib/expandTvShows.py"
runner = "XBMC.RunScript(" + scriptToRun + ", " + argsToPass + ")"
name = "Name you want to display in menu"
commands.append(( name, runner, ))
listitem = xbmcgui.ListItem()
listitem.addContextMenuItems( commands )

Note: it will fail if the .py file does not exist

argsToPass -- Any arguments you want to send to the script
scriptToRun -- The script you want the context menu item to run
runner -- What is handling it, in this case it's XBMC.RunScript
name -- The name you want to display in the menu

this should work too, untested as I can't run XBMC on this machine
Code:
def AddContextItem(name,script,arg):
    commands = []
    runner = "XBMC.RunScript(" + str(script)+ ", " + str(arg) + ")"
    commands.append(( str(name), runner, ))
    listitem = xbmcgui.ListItem()
    listitem.addContextMenuItems( commands )
    

AddContextItem("Hello World","special://home/addons/plugin.video.xbmcflicks/resources/lib/expandTvShows.py", "id=909722")
Reply
#11
fekker Wrote:Note: it will fail if the .py file does not exist

Does this mean that the context menu does not show up if the .py file does not exist (which is what happens for me), or just that it fails to run when you click the item in the context menu?

I am beginning to think that adding context menu items only works for plugins and not scripts. I would be delighted for someone to prove me wrong, however. If someone can manage to modify the example that I posted earlier so that it works, I would be grateful.
Reply
#12
yes, adding context menu entries only works for plugins. you can't add a general context menu entry pointing to a script.
Reply
#13
spiff Wrote:yes, adding context menu entries only works for plugins. you can't add a general context menu entry pointing to a script.

Is there a particular reason for this? I assume I will have to create my own context menu implementation then.
Reply
#14
(2010-12-08, 15:46)spiff Wrote: yes, adding context menu entries only works for plugins. you can't add a general context menu entry pointing to a script.


Hi Spiff,

I'd like to be able to be able to add to this context menu from python also:

http://forum.xbmc.org/showthread.php?tid=136804

and from a site search I see others would also. Do you know what would be the best way to add this?

Thanks,
Losty

Reply

Logout Mark Read Team Forum Stats Members Help
Adding context menu to listitems in an addon?0