0

I need some basic help with my code, I'm trying to create a new list with the value for the variable self.add_programs in each time when I use the variable program_controls to add a list of buttons to store in the arrays.

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 store the buttons in the arrays in each time when I add a list of buttons?

If that is possible, please let me know.

Thom Wiggers
  • 6,938
  • 1
  • 39
  • 65
  • I don't entirely understand what you're trying to do, but what if you didn't index the list at all? Like `self.add_programs.append(ProgramControls(program_controls, program))` – Kevin Jan 01 '15 at 17:28
  • `self.add_programs` is shorter than `self.rows`; for example, consider: `arr = [1]; arr[2]`... There is no `arr[2]`, so you get this error.... You may want to use `self.add_programs[self.rows - 1]`, but I didn't inspect your code too carefully... – Martin Tournoij Jan 01 '15 at 17:28
  • Try `self.add_programs.append(...)`. – Javier Jan 01 '15 at 17:28
  • @Javier i can use `self.add_programs.append` which is works but i'm trying to readd a list of buttons to store in the arrays but it won't let me because the controls are already used. How I can readd a list of buttons to store in the arrays? –  Jan 01 '15 at 17:37
  • Note that you've got some code which is not indented correctly and will probably not be executed at the time you're expecting it to be executed (`prog_button=`, `if self.programs` are likely executed when the class is first initiated). – Thom Wiggers Jan 01 '15 at 17:47
  • Thank you, what code I would need to make some of the changes in order to get the code to be executed? –  Jan 01 '15 at 17:49
  • That depends on what you are trying to do with that code. I have no idea how XBMC works. – Thom Wiggers Jan 01 '15 at 17:56

1 Answers1

3

If you do mylist[3].append() you're trying to append to a list that's the 4th item in your mylist. You could also write this as (mylist[3]).append() to make this more clear.

If you want to append to mylist, you need to just use mylist.append(). If you want to set it on a certain index, you can use list.insert(index, item); however, if the list is not as long as index, it'll just be appended at the end.

If you want to use specific keys, use a dict() instead:

mydict = {}
dict[3] = my_item

In your case, I'd just use self.add_programs.append() however.

Thom Wiggers
  • 6,938
  • 1
  • 39
  • 65
  • @ThornWiggers thank you very much for your help. I want to use `self.add_programs.append()` to append a list of buttons, but how I can readd a list of buttons to store in the arrays do i need to use something is like this `list.insert(index, item)`? –  Jan 01 '15 at 17:41
  • `list.insert()` is really for when you already have a list like `a = [0,1,2,4,5,6]` and would like to insert something in a certain position: `a.insert(3, 3)` gives `a = [0,1,2,3,4,5,6]`. I'm not entirely sure what you mean by 'readd a list of buttons to store in the arrays', but `insert` is very rarely needed. – Thom Wiggers Jan 01 '15 at 17:43
  • @ThornWiggers sorry I mean I want to insert the items in the arrays so I can then use `.append` to open a list of the arrays that I stored. Do I need to use `list.insert()`? can you please show me an example with my code of what i should use? sorry i'm confused how to use `list.insert(index, item)` with my code to make it to work. –  Jan 01 '15 at 17:48
  • Just don't use `insert`. – Thom Wiggers Jan 01 '15 at 17:49
  • what I need to use then? I want to re-add a list of buttons and store in the arrays. how i can do that? –  Jan 01 '15 at 17:52
  • The 'store in arrays' part is just 1) create an empty list, 2) append. I have no idea what you mean by 're-add'. – Thom Wiggers Jan 01 '15 at 17:55
  • Well, I'm using `program_controls` to create a list for the buttons to store in the append but when how i can use `program_controls` to create another list for the buttons when I press on the down arrow buttons of the keyboard twice? hope that is clear of what I'm asking for? –  Jan 01 '15 at 18:03