2 回答
TA贡献1802条经验 获得超5个赞
我不确定在 Python 中设置 a 是否重要,GridRow但如果不重要,您也可以在 KV 语言中解决您的问题。您只需要更改 GridRow 类的代码:
<Label>:
font_size: 35
<GridRow>:
id: row
cols: 4
size_hint_y: None
Label:
size_hint_y: None
text: '00:00:00'
Label:
size_hint_y: None
text: '00:00:00'
Label:
size_hint_y: None
text: '00:00:00'
Button:
background_color: 1, 0, 0, 1
size_hint_y: None
text: 'Delete this'
font_size: 25
on_press: row.parent.remove_widget(row)
<RootWidget>:
orientation: 'vertical'
Button:
size_hint: 1, .25
font_size: 50
pos_hint: {'x': 0, 'top': 1}
text: 'append row'
on_press: rootgrid.append_row()
ScrollviewLayout:
scroll_type: ['bars', 'content']
bar_width: 10
pos_hint: {'x': 0, 'y': .2}
RootGridLayout:
id: rootgrid
cols: 1
size_hint_y: None
height: self.minimum_height
有了这个,就不再需要delete_btn类和函数了。GridRow.__init__
TA贡献1851条经验 获得超3个赞
您可以在创建GridRow和时传递删除所需的信息delete_btn。以下是更改后受影响的类:
class GridRow(GridLayout):
def __init__(self, **kwargs):
self.container = kwargs.pop('container', None)
super(GridRow, self).__init__(**kwargs)
self.size_hint_y = None
self.cols = 4
self.add_widget(delete_btn(row=self, container=self.container))
self.add_widget(Label(text='00:00:00', size_hint_y=None))
self.add_widget(Label(text='00:00:00', size_hint_y=None))
self.add_widget(Label(text='00:00:00', size_hint_y=None))
class RootGridLayout(GridLayout):
def append_row(self):
self.add_widget(GridRow(container=self))
class delete_btn(Button):
def __init__(self, **kwargs):
self.container = kwargs.pop('container', None)
self.row = kwargs.pop('row', None)
super(delete_btn, self).__init__(**kwargs)
def on_release(self):
self.container.remove_widget(self.row)
所以该GridRow __init__()方法现在采用关键字参数container,它应该是RootGridLayout包含GridRows. 该delete_btn __init__()方法采用两个关键字参数container(刚从 传递过来GridRow)和row,它是GridRow包含delete_btn. 这为 提供了从delete_btn中删除其所需的所有信息。现在just的方法执行.GridRowRootGridLayouton_release()delete_btnremove_widget()
添加回答
举报