删除 kivy 中的标签

removing labels in kivy

     temp_label = Label(text= str(temp) + 'C', pos=(-300, -50))
             temp_label.bind(on_touch_down = self.disappear)
             self.add_widget(temp_label)
    
    
   def disappear(self, label_instance, label_choice):
                self.remove_widget(self.temp_label)

我想使用 'on_touch_down' 删除标签,但每次我这样做都会收到此错误

AttributeError: 'UK_Weather' object has no attribute 'temp_label'

以上只是一小段代码,如果可能的话可以用python语言

回答

尝试更改;

temp_label = Label(text= str(temp) + 'C', pos=(-300, -50))
             temp_label.bind(on_touch_down = self.disappear)
             self.add_widget(temp_label)

至:;

self.temp_label = Label(text= str(temp) + 'C', pos=(-300, -50))
             self.temp_label.bind(on_touch_down = self.disappear)
             self.add_widget(self.temp_label)

否则 self.temp_label 未定义(如错误消息所述)。