从另一个班级删除小部件

时间:2019-02-14 12:30:31

标签: python kivy kivy-language

从创建的类中添加我创建的类的窗口小部件,直到可以,但是要进行相反的操作,我非常困难,因为当某个变量(qtd)达到0并保持时,我需要将其删除动态的,必须在创建的类中删除 我认为这有点令人困惑,但是有了代码,它变得更容易

[main.py]

class Carrinho(BoxLayout):

    def click1(self):
        global compras, x, total

        total = 0
        compras = [] 
       boxin = Carrinho(orientation='vertical')
        cont = BoxLayout(orientation='horizontal')

        finalizar = Button(text="Finalizar Compra", size_hint=(None, None), width=150, height=50,
                           pos_hint={'x': .0, 'y': .0})
        cancelar = Button(text="Cancelar", size_hint=(None, None), width=150, height=50, pos_hint={'x': .0, 'y': 0})

        for x in compras:
            boxin.add_widget(Linha(text=x, id=x))
            total += qtd[compras.index(x)] * value[compras.index(x)]

        tot = Label(text="Total:                                                               R$" + str(total))

        boxin.add_widget(tot)
        cont.add_widget(finalizar)
        cont.add_widget(cancelar)

        boxin.add_widget(cont)

        # finalizar.on_press=self.click_fim

        pop = Popup(title='Carrinho', content=boxin, size_hint=(None, None), size=(350, 500))

        pop.open()


class Linha(BoxLayout):

    def __init__(self, text='', **kwargs):
        super().__init__(**kwargs)
        self.ids.label1.text = text
        self.ids.label2.text = str(qtd[compras.index(text)])
        self.ids.label3.text = "R$" + str(qtd[compras.index(text)] * value[compras.index(text)])

    def click_menos(self):
        qtd[compras.index(x)] -= 1
        self.ids.label2.text = str(qtd[compras.index(x)])
        self.ids.label3.text = "R$" + str(qtd[compras.index(x)] * value[compras.index(x)])
        if qtd[compras.index(x)]<=0:
            ##self.parent.ids.boxinha.remove_widget(Linha) 
    |##
It's this function above that I'm stuck, days kkkk
Can someone save me? --- searching in google i got self.parent and i think i am closing, but its wrong yet....

[。KV]

<Carrinho>:
    id:boxinha

<Linha>:
    orientation: 'horizontal'
    id: lin
    Button:
        id: menos
        size_hint: None , None
        pos_hint: {'center_x':0.47, 'center_y': 0.47}
        width: 20
        height: 20
        on_press: root.click_menos()
    Label:
        id: label1 #descrição do item
        font_size: 15
    Label:
        id: label2 #quantidade
        font_size: 15

    Label:
        id: label3 #valor
        font_size: 15
    Button:
        id: mais
        size_hint: None, None
        pos_hint: {'center_x':0.47, 'center_y': 0.47}
        width: 20
        height: 20
        on_press: root.click_mais()

0 个答案:

没有答案