繁体   English   中英

python错误:IndexError:列表索引超出范围

[英]python error: IndexError: list index out of range

我需要一些基本的代码帮助,每次使用变量program_controls添加按钮列表以存储在数组中时,我都尝试使用变量self.add_programs的值创建一个新列表。

当我尝试这个:

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))

它给我错误:IndexError:列表索引超出范围

错误在此行上跳跃:

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

这是代码:

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)

您能帮我每次添加按钮列表时如何将按钮存储在数组中吗?

如果可以的话,请告诉我。

如果您执行mylist[3].append() ,则尝试追加到mylist 您也可以将其写为(mylist[3]).append()以使其更加清楚。

如果要追加到mylist ,则只需使用mylist.append() 如果要在某个索引上设置它,可以使用list.insert(index, item) ; 但是,如果列表的长度不如index ,则将其附加在末尾。

如果要使用特定键,请改用dict()

mydict = {}
dict[3] = my_item

在您的情况下,我只是使用self.add_programs.append()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM