kivyで recycleviewのリストに削除ボタンを設定しました。
選択した行を削除したいです。
選択していない行は、削除ボタンを押しても削除されないようにしたいです。
SelectablLabelクラスの中に削除用のモジュールを作りたいです。
あと、行が選択されているか否かの判別も行いたいです。
from kivy.app import App from kivy.lang import Builder from kivy.uix.recycleview import RecycleView from kivy.uix.recycleview.views import RecycleDataViewBehavior from kivy.uix.label import Label from kivy.uix.button import Button from kivy.uix.gridlayout import GridLayout from kivy.uix.boxlayout import BoxLayout from kivy.properties import BooleanProperty from kivy.uix.recycleboxlayout import RecycleBoxLayout from kivy.uix.behaviors import FocusBehavior from kivy.uix.recycleview.layout import LayoutSelectionBehavior import japanize_kivy Builder.load_string(''' <SelectableLabel>: canvas.before: Color: rgba: (.8, .8, .8, 1) if self.selected else (1, .8, .8, 1) Rectangle: pos: self.pos size: self.size label1_text: 'label 1 text' label2_text: 'label 2 text' label3_text: 'label 3 text' pos: self.pos size: self.size #Label: # id: id_label1 # text: root.label1_text # color: [0,0,0,1] # font_size: 20 Label: id: id_label2 text: root.label2_text color: [0,0,0,1] font_size: 20 size_hint_x: 4 Label: id: id_label3 text: root.label3_text color: [0,0,0,1] font_size: 20 size_hint_x: 4 Button: text: '削除' font_size: 20 size_hint_x: 2 on_press: detach_recycleview() #どのように当該の行と関連づけるか? <RV>: viewclass: 'SelectableLabel' SelectableRecycleBoxLayout: default_size: None, dp(45) default_size_hint: 1, None size_hint_y: None height: self.minimum_height orientation: 'vertical' multiselect: True touch_multiselect: True ''') items_1 = ['カツ丼','天丼','親子丼','玉子丼','オムライス','カツカレー'] items_2 = ['850','850','750','670','800','850'] class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout): pass class SelectableLabel(RecycleDataViewBehavior, GridLayout): index = None selected = BooleanProperty(False) selectable = BooleanProperty(True) cols = 3 def refresh_view_attrs(self, rv, index, data): '''Catch and handle the view changes ''' self.index = index self.label1_text = str(index) self.label2_text = data['label2']['text'] self.label3_text = data['label3']['text'] return super(SelectableLabel, self).refresh_view_attrs( rv, index, data) def on_touch_down(self, touch): ''' Add selection on touch down ''' if super(SelectableLabel, self).on_touch_down(touch): return True if self.collide_point(*touch.pos) and self.selectable: return self.parent.select_with_touch(self.index, touch) def apply_selection(self, rv, index, is_selected): ''' Respond to the selection of items in the view. ''' self.selected = is_selected if is_selected: print('selection changed to {}'.format(rv.data[index])) else: print('selection removed for {}'.format(rv.data[index])) class RV(RecycleView): def __init__(self, **kwargs): super(RV, self).__init__(**kwargs) paired_iter = zip(items_1, items_2) self.data = [] for i1, i2 in paired_iter: d = {'label2': {'text': i1}, 'label3': {'text': i2}} self.data.append(d) class Menu01App(App): def build(self): return RV() if __name__ == '__main__': Menu01App().run()
よろしくお願いいたします。
その後調べましたが、削除Buttonのon_pressにdetach_recycleview()を付けること
ぐらいしか思いつきませんでした。
今知りたいことは、
- detach_recycleview であっているかどうか?
- あっている場合、その削除Buttonと削除したい行をどのように関連づけるか?
- あと、kivyファイルの方に書き込んだ方がよいか、classの中にdefとして、
書き込んだ方がよいかです。
知識がなさ過ぎて、質問の仕方もよくわからなくてすみません。
なんとかしてこの問題を解決したいので、
質問の仕方も含め、間違っていたらいろいろ教えてください。
よろしくお願いいたします。
kvファイルの中の削除buttonのon_pressにdetach_recycleview()を書き込んで、
実行したところ
NameError: name 'detach_recycleview' is not defined
がでました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/11 12:43 編集
2020/02/11 12:36
2020/02/11 14:19
2020/02/11 14:24
2020/02/11 14:31
2020/02/11 14:36
2020/02/11 14:44
2020/02/11 14:56
2020/02/11 16:43 編集
2020/02/13 12:41
2020/02/13 13:14 編集
2020/02/13 13:29
2020/02/13 13:58 編集
2020/02/13 14:06
2020/02/13 14:29
2020/02/14 12:40
2020/02/14 12:53
2020/02/17 09:47
2020/02/17 10:04
2020/02/17 11:52
2020/05/13 09:16