首页 > 解决方案 > 去除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语言吗

标签: pythonkivylabel

解决方案


尝试改变;

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未定义(如错误消息所述)。


推荐阅读