'Button that delete itself
My idea its create a order list. This have to be posible of expand or reduce if the order needed. So I was trying how to do a button that can add a 'Box Label' to a grid, at the same time that this 'Box Label' have the property of delete by itself by a button. I tried a lot of things, but I am really new in this, so nothing work for me.
class OperatorWindow(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Window.size = (1366, 768)
def remove_item(self):
self.ids.parent.clear_widgets()
def add_more(self):
prod_addmore = self.ids.product_inputs
product = BoxLayout(size_hint_x=1, height=30, spacing=5)
prod = TextInput(size_hint_x=.2)
qty = TextInput(size_hint_x=.1)
price = TextInput(size_hint_x=.1)
delete = Button(text='-', size_hint_x=.05)
product.add_widget(prod)
product.add_widget(qty)
product.add_widget(price)
product.add_widget(delete)
prod_addmore.add_widget(product)
delete.bind(on_release=self.deleting(prod_addmore, product))
def deleting(self, prod_addmore, product):
prod_addmore.remove_widget(product)
class OperatorApp(App):
def build(self):
return OperatorWindow()
if __name__ == "__main__":
oa = OperatorApp()
oa.run()
Kivy File
...
BoxLayout:
id: products_labels
size_hint_y: None
size_hint_x: .45
height: 40
spacing: 5
FlatButton:
size_hint_x: .2
text: 'Producto'
canvas.before:
Color:
rgba: (.30,.30,.30,1)
Rectangle:
size: self.size
pos: self.pos
FlatButton:
size_hint_x: .1
text: 'Cantidad'
canvas.before:
Color:
rgba: (.30,.30,.30,1)
Rectangle:
size: self.size
pos: self.pos
FlatButton:
size_hint_x: .1
text: 'Precio'
canvas.before:
Color:
rgba: (.30,.30,.30,1)
Rectangle:
size: self.size
pos: self.pos
FlatButton:
text: '+'
size_hint_x: .05
on_release: root.add_more()
canvas.before:
Color:
rgba: (.30,.30,.30,1)
Rectangle:
size: self.size
pos: self.pos
ScrollView:
size_hint_y: .15
do_scroll_x: False
do_scroll_y: True
GridLayout:
id: product_inputs
size:(self.width, self.height)
size_hint_x: .45
spacing: 5
size_hint_y: None
cols: 1
height: self.minimum_height
row_default_height: 30
row_force_default: True
Solution 1:[1]
Please refer to this link! For some refreshers about widgets.
Your code is fine you just need some minor adjustments here and there.
Firstly change
product
To
self.product
It always makes it easier to call functions since the self indicates ownership okay.
Secondly change this line
delete.bind(on_release=self.deleting(prod_addmore, product))
to
delete.bind(on_release=self.deleting)
the reason for the error is because of the parenthesis. And lastly
Chang this
def deleting(self, prod_addmore, product):
prod_addmore.remove_widget(product)
to
def deleting(self, widget):
prod_addmore = self.ids.product_inputs
prod_addmore.remove_widget(self.product)
Hope this helps if you need more indept explano please let me know :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CodeSlice Kivy |
