Inherit from xbmc.ListItem
#1
Hello,

I would like to inherit the class listitem, and i have some problem with python
python:

class User(xbmcgui.ListItem):
    def __init__(self, name, label2="", iconImage="", thumbnailImage="", path="", window= None, userdata=None):
        super(User, self).__init__(label=name, label2="", iconImage="", thumbnailImage="", path="")
        self.Base = window
        self.Name = name
        self.UserData = userdata

us = User(msg.username, window = self.Base, userdata= msg.userdata)
Error Message :
python:
TypeError: 'userdata' is an invalid keyword argument for this function
Can help me please ?
Reply
#2
Overriding xbmc* classes is a bit tricky because you have to ovverride both __init__ and __new__ methods:
python:
class User(xbmcgui.ListItem):
def __new__(cls, name, label2="", iconImage="", thumbnailImage="", path="", window=None, userdata=None):
    return super(User, cls).__new__(cls, name, label2="", iconImage="", thumbnailImage="", path="")

def __init__(self, name, label2="", iconImage="", thumbnailImage="", path="", window=None, userdata=None):
    super(User, self).__init__(label=name, label2="", iconImage="", thumbnailImage="", path="")
    self.Base = window
    self.Name = name
    self.UserData = userdata
Reply
#3
Thank you Roman, its work fine. and have you the solution for convert ListItem to User ?

python:

class User(xbmcgui.ListItem):
    def __new__(cls, name, status =0,  files = 0):
        return super(User, cls).__new__(cls, name)

    def __init__(self, name, status =0,  files = 0):
        super(User, self).__init__(label=name)
        self.Name = name
        self.files = files

    def openSession(self):
       xbmcgui.Dialog().notification('User', self.Name)
       ....
       .....
python:

self.getControl(120).getSelectedItem().openSession()
Error :
python:

AttributeError: 'xbmcgui.ListItem' object has no attribute 'onClick'
Reply
#4
If you are not instantiating your ListItem-derived classes by yourslef but receiving ListItem instances from Kodi, then you should use composition instead of inheritance, meaning that you User class should not inherit from ListItem but have a ListItem instance as one of its properties, e.g.:

python:
 class User(object):
  def __init__(self, list_item):
        self._list_item = list_item
       # Access list_item properties via its accessor methods

list_item = window.getControl(120).getSelectedItem()
user = User(list_item)
Reply

Logout Mark Read Team Forum Stats Members Help
Inherit from xbmc.ListItem0