Newbie Help: ListItem and OnAction
#1
Hello Kodi Devs,

I'm new to add-on development and python in general, hoping someone can help me understand what I'm doing wrong.
I'm attempting to define an action/function with arguments to call based on the listitem selected.

Here's my main add-on code at the moment:
Code:
import os, sys
import xbmc
import xbmcgui
import xbmcplugin

addon_handle = int(sys.argv[1])
ACTION_PREVIOUS_MENU = 10
ACTION_SELECT_ITEM = 7

# Main code
class Main(xbmcgui.Window):

    def __init__( self, *args, **kwargs ):
        items = list()
        print 'Plugin Started'
        item1= xbmcgui.ListItem('Test 1', '','')
        item1.setInfo( "games", { "title": 'Test1', "label": 'Test Label 1'} )
        items.append(('Test 1',item1, False))
        item2 = xbmcgui.ListItem('Test 2', '','')
        item2.setInfo( "games", { "title": 'Test2', "label": 'Test Label 2'} )
        items.append(('Test 2',item2, False))
        xbmcplugin.addDirectoryItems(addon_handle,items)
        xbmcplugin.endOfDirectory(addon_handle)
        

    def onInit(self):
        self.window = xbmcgui.Window(xbmcgui.getCurrentWindowId())
        self.window.setProperty('MyAddonIsRunning', 'true')

    def onAction(self, action):
        print 'Test Action'
        if action == ACTION_PREVIOUS_MENU:
            self.close()


window = xbmcgui.Window(xbmcgui.getCurrentWindowId())
if window.getProperty('MyAddonIsRunning') != 'true':
    mydisplay = Main()
    mydisplay .doModal()
    del mydisplay
    window.setProperty('MyAddonIsRunning', 'false')
    print 'Plugin Ended'

When I run this test script, I get my list, but I don't get 'Test Action' like I'm expecting. Is there an easier way to add actions to a list item?
Additionally, I never see 'Plugin Ended' when i close my add-on, so I'm not sure if that might also be implemented incorrectly.

Any help for a newbie would be greatly appreciated!
Reply
#2
That is a such weird mix of different Kodi APIs that I'm not even sure it will work.Huh
Kodi has 3 main API for creating addon UIs:
1) Python-based Controls (they are actually C++ based but that it does not matter for us). Here all UI elements are Python classes.
2) XML-based controls. Here all UI elements are defined in an XML-file with the same structure and syntax as Kodi skins. Basically, an addon has its own mini-skin,
3) Kodi plugin API - the simplest. You only need to create a list of elements and pass it to Kodi, and it will do the rest: displaying the list with text labels, images etc., processing user input, playing media files and so on.
So choose one of the above and stick to it. Yes, you can use different API in one addon, but the parts of your code using those APIs must be logically separated. E.g. you can use a Control-based login window in your plugin.
In your case, if you are using xbmcgui.Window class, then you need to add a ControlList to it, populate the list and process events when a user clicks it.
Reply
#3
For you code segment:

python:

def onAction(self, action):
        print 'Test Action'
        if action == ACTION_PREVIOUS_MENU:
            self.close()


You need to access the property getId in action
python:

def onAction(self, action):
        print 'Test Action'
        if action.getId() == ACTION_PREVIOUS_MENU:
            self.close()
Reply

Logout Mark Read Team Forum Stats Members Help
Newbie Help: ListItem and OnAction0