2016-12-29 119 views
0

我無法取出部件與標籤一旦它加入 下面是相關的代碼: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() :聲明,我收到一個錯誤「本地變量引用之前引用」每次我沒有上述提及如果統計我測試時,我確保已添加小部件

回答

0

我假設你輸入此方法兩次(1st數據== []第二次數據= [...])。所以你應該讓你的變量在手邊(把它們放在實例 -

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) 

注意所有的地方我已經添加自我 ...