Python 2.7 Python-更改函数中的类变量值

Python 2.7 Python-更改函数中的类变量值,python-2.7,user-interface,kivy,Python 2.7,User Interface,Kivy,我正在构建一个python类,将下拉列表及其按钮封装在一个方便的小部件中,遇到了一个问题 class DropDownMenu(DropDown): def __init__(self, **kwargs): super(DropDownMenu, self).__init__(**kwargs) self.The_Menu = DropDown() self.The_Btns = [] self.Num_Btns = 0 def Set_Num_Btns(s

我正在构建一个python类,将下拉列表及其按钮封装在一个方便的小部件中,遇到了一个问题

class DropDownMenu(DropDown):

def __init__(self, **kwargs):
    super(DropDownMenu, self).__init__(**kwargs)
    self.The_Menu = DropDown()
    self.The_Btns = []
    self.Num_Btns = 0

def Set_Num_Btns(self):
    self.Num_Btns = len(self.The_Btns)

def Create_Menu(self, Btn_Names):

    # Populate List Size Property
    if (self.Num_Btns == 0):
        self.Set_Num_Btns()

    # Add Buttons to the Drop-Down
    for i in range(0, self.Num_Btns):
        self.The_Btns.append(Button(text = Btn_Names[i], size_hint_y = None, height = 20))
        self.The_Menu.add_widget(self.The_Btns[i])
它编译得很好,当我尝试创建下拉菜单时,我得到了我想要的:

self.File_Menu = DropDownMenu()
self.File_Menu.Create_Menu(self.File_Menu_Names)
self.add_widget(self.File_Menu)
但是,如果我试图将任何按钮绑定到任何东西上,比如:

self.File_Menu.The_Btns[0].bind(on_release = self.Insert_File_Menu.open)
编译器抛出异常,表示列表超出范围。进一步检查后,我意识到,尽管我调用了Create_菜单函数,但没有从空列表更改_Btns的值。所以,我的问题是:如何解决这个问题


任何帮助都将不胜感激。谢谢

首先,python没有您所指的编译意义,也没有编译器。还有,看一看

为了回答您的问题,您正在0到Num_Btns的范围内进行迭代。但是,在Set_Num_Btns中,您将变量设置为lenself.the_Btns,这是一个空列表,即您正在范围0,0上迭代。我猜你是想这样做的:

for name in Btn_Names:
    self.The_Btns.append(Button(text=name, ...))
    ....

谢谢你的帮助。我是python新手,所以一些约定还没有被接受。这应该可以解决我的问题