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中刪除其所需的所有信息?,F在just的方法執行.GridRowRootGridLayouton_release()delete_btnremove_widget()
添加回答
舉報