Kivy删除小部件

时间:2016-12-29 09:04:14

标签: python-2.7 widget kivy

添加完标签后,我无法删除带有标签的小部件 这是相关的代码:

    logi= True
    if data == []:

        logn =Label(text= "Incorrect Username",color=(190,0,0,1),
                     pos_hint={"right":1.035,"top":1.14})
        self.add_widget(logn)

        logu =Label(text= "Incorrect Password",color=(190,0,0,1),
                pos_hint={"right":1.035,"top":1.04})
        self.add_widget(logu)

        logi= False

    if logi == True:

        textinput.text=''
        textinput2.text=''

        if 'logn' in locals() and 'logu' in locals() :
            self.remove_widget(logn)   
            self.remove_widget(logu)

一旦添加了小部件,我似乎无法删除它们,如果我删除if 'logn' in locals() and 'logu' in locals() :语句,我会收到错误"在分配之前引用的局部变量"每次我测试这个没有上面提到的if语句我确保添加了小部件

1 个答案:

答案 0 :(得分:0)

我假设您输入此方法两次(第一次数据== []第二次数据= [...])。所以你应该把变量放在手边(把它们放在实例上 - self

logi= True
if data == []:

    self.logn =Label(text= "Incorrect Username",color=(190,0,0,1),
                 pos_hint={"right":1.035,"top":1.14})
    self.add_widget(self.logn)

    self.logu =Label(text= "Incorrect Password",color=(190,0,0,1),
            pos_hint={"right":1.035,"top":1.04})
    self.add_widget(self.logu)

    logi= False

if logi == True:

    textinput.text=''
    textinput2.text=''

    if hasattr(self, 'logn'): #check that we put something here before...
        self.remove_widget(self.logn)   
        self.remove_widget(self.logu)

请注意我添加的所有地点 self ...