Hi guys,
I need some basic help with my code, I've created a empty list to appending the controls to a list to add the controls when I pressed on the down arrow button of the keyboard. I'm trying to appending another controls to a list to add the controls when I press on the down arrow button again but i get an error: IndexError: list index out of range

When I try this:

self.add_programs = list()
self.rows += 1

program_controls = xbmcgui.ControlButton(
    int(position_start), 
    int(position_top), 
    int(program_width), 
    int(program_height), 
    program_title, 
    focusTexture = self.path + self.button_focus, 
    noFocusTexture = self.path + self.button_nofocus,
    textColor ='0xFFFFFFFF',
    focusedColor ='0xFF000000'
)
self.add_programs[self.rows].append(ProgramControls(program_controls, program))

It give me the error: IndexError: list index out of range

The error are jumping on this line:

self.add_programs[self.rows].append(ProgramControls(program_controls, program))

Here is the code:

class ProgramControls(object):
     def __init__(self, control, program):
         self.control = control
         self.program = program


class MyClass(xbmcgui.WindowXML):

    def __init__(self):
        self.add_programs = list()
        self.rows = 0

    def GoDown(self):
        self.add_programs = list()
        self.rows += 1

        program_controls = xbmcgui.ControlButton(
            int(position_start), 
            int(position_top), 
            int(program_width), 
            int(program_height), 
            program_title, 
            focusTexture = self.path + self.button_focus, 
            noFocusTexture = self.path + self.button_nofocus,
            textColor ='0xFFFFFFFF',
            focusedColor ='0xFF000000'
        )
        self.add_programs[self.rows].append(ProgramControls(program_controls, program))
    prog_button = [elem.control for elem in self.add_programs]


    if self.programs == False:
       self.addControls(prog_button)

Can you please help me how I can appending the controls to a list in each time when I press on the down arrow button of the keyboard to allow me to add the controls in each time?

If that is possible, please let me know.

I'm not too good with Python myself but I think you'll getting the index wrong because Python uses zero based indexes.
You're setting the number of rows the list has with
self.rows += 1
but then using that same amount to locate the element in the list:
self.add_programs[self.rows].append(ProgramControls(program_controls, program))

So, if you set self.rows = 4, that means the elements 0,1,2,3 exist but 4 does not. Try [self.rows-1] for line 28.

You may want to add a temporay print(self.rows, len(self.add_programs)) just before line 28 to see if self.rows exceeds length - 1

It alsmost looks like self.add_programs is an empty list at that point.

Edited 1 Year Ago by sneekula

@sneekula thank you for your help. Here is the list of elements when I press on the down arrow button of the keyboard.

01:14:08 T:4196  NOTICE: (1, 0)
01:14:08 T:4196  NOTICE: (2, 1)
01:14:08 T:4196  NOTICE: (3, 2)
01:14:08 T:4196  NOTICE: (4, 3)
01:14:08 T:4196  NOTICE: (5, 4)
01:14:08 T:4196  NOTICE: (6, 5)
01:14:08 T:4196  NOTICE: (7, 6)
01:14:08 T:4196  NOTICE: (8, 7)
01:14:08 T:4196  NOTICE: (9, 8)
01:14:08 T:4196  NOTICE: (10, 9)
01:14:08 T:4196  NOTICE: (11, 10)
01:14:08 T:4196  NOTICE: (12, 11)
01:14:08 T:4196  NOTICE: (13, 12)
01:14:08 T:4196  NOTICE: (14, 13)
01:14:08 T:4196  NOTICE: (15, 14)
01:14:08 T:4196  NOTICE: (16, 15)
01:14:08 T:4196  NOTICE: (17, 16)
01:14:08 T:4196  NOTICE: (18, 17)
01:14:08 T:4196  NOTICE: (19, 18)
01:14:08 T:4196  NOTICE: (20, 19)
01:14:08 T:4196  NOTICE: (21, 20)
01:14:08 T:4196  NOTICE: (22, 21)
01:14:08 T:4196  NOTICE: (23, 22)
01:14:08 T:4196  NOTICE: (24, 23)
01:14:08 T:4196  NOTICE: (25, 24)
01:14:08 T:4196  NOTICE: (26, 25)
01:14:08 T:4196  NOTICE: (27, 26)
01:14:08 T:4196  NOTICE: (28, 27)
01:14:08 T:4196  NOTICE: (29, 28)
01:14:08 T:4196  NOTICE: (30, 29)
01:14:08 T:4196  NOTICE: (31, 30)
01:14:08 T:4196  NOTICE: (32, 31)
01:14:08 T:4196  NOTICE: (33, 32)
01:14:08 T:4196  NOTICE: (34, 33)
01:14:08 T:4196  NOTICE: (35, 34)
01:14:08 T:4196  NOTICE: (36, 35)
01:14:08 T:4196  NOTICE: (37, 36)
01:14:08 T:4196  NOTICE: (38, 37)
01:14:08 T:4196  NOTICE: (39, 38)
01:14:08 T:4196  NOTICE: (40, 39)
01:14:08 T:4196  NOTICE: (41, 40)
01:14:08 T:4196  NOTICE: (42, 41)
01:14:08 T:4196  NOTICE: (43, 42)
01:14:08 T:4196  NOTICE: (44, 43)
01:14:08 T:4196  NOTICE: (45, 44)
01:14:08 T:4196  NOTICE: (46, 45)
01:14:08 T:4196  NOTICE: (47, 46)
01:14:08 T:4196  NOTICE: (48, 47)
01:14:08 T:4196  NOTICE: (49, 48)
01:14:08 T:4196  NOTICE: (50, 49)
01:14:08 T:4196  NOTICE: (51, 50)
01:14:08 T:4196  NOTICE: (52, 51)
01:14:08 T:4196  NOTICE: (53, 52)
01:14:08 T:4196  NOTICE: (54, 53)
01:14:08 T:4196  NOTICE: (55, 54)
01:14:08 T:4196  NOTICE: (56, 55)
01:14:08 T:4196  NOTICE: (57, 56)
01:14:08 T:4196  NOTICE: (58, 57)
01:14:08 T:4196  NOTICE: (59, 58)
01:14:08 T:4196  NOTICE: (60, 59)
01:14:08 T:4196  NOTICE: (61, 60)
01:14:08 T:4196  NOTICE: (62, 61)
01:14:08 T:4196  NOTICE: (63, 62)
01:14:08 T:4196  NOTICE: (64, 63)
01:14:08 T:4196  NOTICE: (65, 64)
01:14:08 T:4196  NOTICE: (66, 65)
01:14:08 T:4196  NOTICE: (67, 66)
01:14:08 T:4196  NOTICE: (68, 67)
01:14:08 T:4196  NOTICE: (69, 68)

So when I press on the down arrow button again, I get another list of elements which is included the error:

01:14:08 T:4196  NOTICE: (1, 0)
01:14:08 T:4196  NOTICE: (2, 1)
01:14:08 T:4196  NOTICE: (3, 2)
01:14:08 T:4196  NOTICE: (4, 3)
01:14:08 T:4196  NOTICE: (5, 4)
01:14:08 T:4196  NOTICE: (6, 5)
01:14:08 T:4196  NOTICE: (7, 6)
01:14:08 T:4196  NOTICE: (8, 7)
01:14:08 T:4196  NOTICE: (9, 8)
01:14:08 T:4196  NOTICE: (10, 9)
01:14:08 T:4196  NOTICE: (11, 10)
01:14:08 T:4196  NOTICE: (12, 11)
01:14:08 T:4196  NOTICE: (13, 12)
01:14:08 T:4196  NOTICE: (14, 13)
01:14:08 T:4196  NOTICE: (15, 14)
01:14:08 T:4196  NOTICE: (16, 15)
01:14:08 T:4196  NOTICE: (17, 16)
01:14:08 T:4196  NOTICE: (18, 17)
01:14:08 T:4196  NOTICE: (19, 18)
01:14:08 T:4196  NOTICE: (20, 19)
01:14:08 T:4196  NOTICE: (21, 20)
01:14:08 T:4196  NOTICE: (22, 21)
01:14:08 T:4196  NOTICE: (23, 22)
01:14:08 T:4196  NOTICE: (24, 23)
01:14:08 T:4196  NOTICE: (25, 24)
01:14:08 T:4196  NOTICE: (26, 25)
01:14:08 T:4196  NOTICE: (27, 26)
01:14:08 T:4196  NOTICE: (28, 27)
01:14:08 T:4196  NOTICE: (29, 28)
01:14:08 T:4196  NOTICE: (30, 29)
01:14:08 T:4196  NOTICE: (31, 30)
01:14:08 T:4196  NOTICE: (32, 31)
01:14:08 T:4196  NOTICE: (33, 32)
01:14:08 T:4196  NOTICE: (34, 33)
01:14:08 T:4196  NOTICE: (35, 34)
01:14:08 T:4196  NOTICE: (36, 35)
01:14:08 T:4196  NOTICE: (37, 36)
01:14:08 T:4196  NOTICE: (38, 37)
01:14:08 T:4196  NOTICE: (39, 38)
01:14:08 T:4196  NOTICE: (40, 39)
01:14:08 T:4196  NOTICE: (41, 40)
01:14:08 T:4196  NOTICE: (42, 41)
01:14:08 T:4196  NOTICE: (43, 42)
01:14:08 T:4196  NOTICE: (44, 43)
01:14:08 T:4196  NOTICE: (45, 44)
01:14:08 T:4196  NOTICE: (46, 45)
01:14:08 T:4196  NOTICE: (47, 46)
01:14:08 T:4196  NOTICE: (48, 47)
01:14:08 T:4196  NOTICE: (49, 48)
01:14:08 T:4196  NOTICE: (50, 49)
01:14:08 T:4196  NOTICE: (51, 50)
01:14:08 T:4196  NOTICE: (52, 51)
01:14:08 T:4196  NOTICE: (53, 52)
01:14:08 T:4196  NOTICE: (54, 53)
01:14:08 T:4196  NOTICE: (55, 54)
01:14:08 T:4196  NOTICE: (56, 55)
01:14:08 T:4196  NOTICE: (57, 56)
01:14:08 T:4196  NOTICE: (58, 57)
01:14:08 T:4196  NOTICE: (59, 58)
01:14:08 T:4196  NOTICE: (60, 59)
01:14:08 T:4196  NOTICE: (61, 60)
01:14:08 T:4196  NOTICE: (62, 61)
01:14:08 T:4196  NOTICE: (63, 62)
01:14:08 T:4196  NOTICE: (64, 63)
01:14:08 T:4196  NOTICE: (65, 64)
01:14:08 T:4196  NOTICE: (66, 65)
01:14:08 T:4196  NOTICE: (67, 66)
01:14:08 T:4196  NOTICE: (68, 67)
01:14:08 T:4196  NOTICE: (69, 68)
01:14:25 T:4196  NOTICE: (70, 69)
01:14:25 T:4196  NOTICE: (71, 70)
01:14:25 T:4196  NOTICE: (72, 71)
01:14:25 T:4196  NOTICE: (73, 72)
01:14:25 T:4196  NOTICE: (74, 73)
01:14:25 T:4196  NOTICE: (75, 74)
01:14:25 T:4196  NOTICE: (76, 75)
01:14:25 T:4196  NOTICE: (77, 76)
01:14:25 T:4196  NOTICE: (78, 77)
01:14:25 T:4196  NOTICE: (79, 78)
01:14:25 T:4196  NOTICE: (80, 79)
01:14:25 T:4196  NOTICE: (81, 80)
01:14:25 T:4196  NOTICE: (82, 81)
01:14:25 T:4196  NOTICE: (83, 82)
01:14:25 T:4196  NOTICE: (84, 83)
01:14:25 T:4196  NOTICE: (85, 84)
01:14:25 T:4196  NOTICE: (86, 85)
01:14:25 T:4196  NOTICE: (87, 86)
01:14:25 T:4196  NOTICE: (88, 87)
01:14:25 T:4196  NOTICE: (89, 88)
01:14:25 T:4196  NOTICE: (90, 89)
01:14:25 T:4196  NOTICE: (91, 90)
01:14:25 T:4196  NOTICE: (92, 91)
01:14:25 T:4196  NOTICE: (93, 92)
01:14:25 T:4196  NOTICE: (94, 93)
01:14:25 T:4196  NOTICE: (95, 94)
01:14:25 T:4196  NOTICE: (96, 95)
01:14:25 T:4196  NOTICE: (97, 96)
01:14:25 T:4196  NOTICE: (98, 97)
01:14:25 T:4196  NOTICE: (99, 98)
01:14:25 T:4196  NOTICE: (100, 99)
01:14:25 T:4196  NOTICE: (101, 100)
01:14:25 T:4196  NOTICE: (102, 101)
01:14:25 T:4196  NOTICE: (103, 102)
01:14:25 T:4196  NOTICE: (104, 103)
01:14:25 T:4196  NOTICE: (105, 104)
01:14:25 T:4196  NOTICE: (106, 105)
01:14:25 T:4196  NOTICE: (107, 106)
01:14:25 T:4196  NOTICE: (108, 107)
01:14:25 T:4196  NOTICE: (109, 108)
01:14:25 T:4196  NOTICE: (110, 109)
01:14:25 T:4196  NOTICE: (111, 110)
01:14:25 T:4196  NOTICE: (112, 111)
01:14:25 T:4196  NOTICE: (113, 112)
01:14:25 T:4196  NOTICE: (114, 113)
01:14:25 T:4196  NOTICE: (115, 114)
01:14:25 T:4196  NOTICE: (116, 115)
01:14:25 T:4196  NOTICE: (117, 116)
01:14:25 T:4196  NOTICE: (118, 117)
01:14:25 T:4196  NOTICE: (119, 118)
01:14:25 T:4196  NOTICE: (120, 119)
01:14:25 T:4196  NOTICE: (121, 120)
01:14:25 T:4196  NOTICE: (122, 121)
01:14:25 T:4196  NOTICE: (123, 122)
01:14:25 T:4196  NOTICE: (124, 123)
01:14:25 T:4196  NOTICE: (125, 124)
01:14:25 T:4196  NOTICE: (126, 125)
01:14:25 T:4196  NOTICE: (127, 126)
01:14:25 T:4196  NOTICE: (128, 127)
01:14:25 T:4196  NOTICE: (129, 128)
01:14:25 T:4196  NOTICE: (130, 129)
01:14:25 T:4196  NOTICE: (131, 130)
01:14:25 T:4196  NOTICE: (132, 131)
01:14:25 T:4196  NOTICE: (133, 132)
01:14:25 T:4196  NOTICE: (134, 133)
01:14:25 T:4196  NOTICE: (135, 134)
01:14:25 T:4196  NOTICE: (136, 135)
01:14:25 T:4196  NOTICE: (137, 136)
01:14:25 T:4196  NOTICE: (138, 137)

01:14:26 T:4196   ERROR: EXCEPTION: Control is already used
01:14:26 T:4196   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <type 'exceptions.RuntimeError'>
Error Contents: Control is already used
Traceback (most recent call last):
File "C:\Users\user\AppData\Roaming\XBMC\addons\script.tvguide\test.py", line 5792, in onAction
self.GoDown()
File "C:\Users\user\AppData\Roaming\XBMC\addons\script.tvguide\test.py", line 2503, in GoDown
self.addControls(prog_button)`
RuntimeError: Control is already used
-->End of     Python script error report<--                    

Do you know what I need to do?

Edited 1 Year Ago by mark103

This article has been dead for over six months. Start a new discussion instead.