KIVY:如何在多个屏幕上拥有相同的小部件组

时间:2017-10-31 03:12:58

标签: android python python-3.x kivy kivy-language

我想在我的所有屏幕上添加相同类型的小部件。当点击其中任何一个添加的小部件时,它将从所有屏幕中删除。这是当前的方法

def drinksSelect(self,value):  # creating a button by referring the id of the layout in which to create button
    drinkImagePath = {'pepsi': 'drinksPictures/pepsi.png','7up': 'drinksPictures/7up.png'}
    if self.root.a_s.l < self.root.a_s.limit: # You know what I mean
        st = 'number'
        img = myImage(source= drinkImagePath[value], size=(200,20), id=st)
        img2 = myImage(source=drinkImagePath[value], size=(200, 20), id='soo')
        self.root.a_s.ids.place_remaining.add_widget(img)
        self.root.m_s.ids.place.add_widget(img2)
        self.root.a_s.l += 1

我正在使用此

删除图像
class myImage (Image):
    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            self.parent.remove_widget(self)

问题在于它只删除被点击的图像而不删除其他屏幕上的图像。

1 个答案:

答案 0 :(得分:1)

试试这个:

def drinksSelect(self, value):  # creating a button by referring the id of the layout in which to create button
    drinkImagePath = {'pepsi': 'drinksPictures/pepsi.png', '7up': 'drinksPictures/7up.png'}
    if self.root.a_s.l < self.root.a_s.limit:  # You know what I mean
        st = 'number'
        img = myImage(source=drinkImagePath[value], size=(200, 20), id=st)
        img2 = myImage(source=drinkImagePath[value], size=(200, 20), id='soo')
        img.twin = img2
        img2.twin = img
        button.a_s = self.root.a_s
        button2.a_s = self.root.a_s
        self.root.a_s.ids.place_remaining.add_widget(img)
        self.root.m_s.ids.place.add_widget(img2)
        self.root.a_s.l += 1


class myImage(Image):
    twin = ObjectProperty()
    a_s = ObjectProperty()

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            self.parent.remove_widget(self)
            self.twin.parent.remove_widget(self.twin)
            self.a_s.l -= 1