Kivy Popup 确认删除列表视图项
Kivy Popup confirmation to delete listview item
如果用户单击删除会议按钮,我想从列表视图中删除一个项目。
在这种情况下,将出现一个弹出窗口,要求确认删除会议。
一切正常,但当用户单击“是”按钮(以确认删除会议)时,我未能从列表视图中删除会议。
我想为“是”按钮写信:
on_press : self.meeting_list.adapter.data.remove(selection) and self.meeting_list._trigger_reset_populate()
class MeetingDB(BoxLayout):
meeting_list = ObjectProperty()
attendance_list = ObjectProperty()
def delete_meeting(self):
if self.meeting_list.adapter.selection:
selection = self.meeting_list.adapter.selection[0].text
self.box_popup = BoxLayout(orientation = 'horizontal')
self.box_popup.add_widget(Label(text = "Confirm the deletion of the meeting ?"))
self.box_popup.add_widget(Button(
text = "Yes",
on_press = App.get_running_app().root.ids.meeting_list.adapter.data.remove(selection),
size_hint = (0.215, 0.075)))
self.box_popup.add_widget(Button(
text = "No",
on_press = lambda *args: self.popup_exit.dismiss(),
size_hint=(0.215, 0.075)))
self.popup_exit = Popup(title = "Delete Meeting Confirmation",
content = self.box_popup,
size_hint = (0.4, 0.4),
auto_dismiss = True)
self.popup_exit.open()
#self.meeting_list.adapter.data.remove(selection)
#self.meeting_list._trigger_reset_populate()
Kv 文件:
<MeetingDB>:
orientation:'vertical'
meeting_list: meeting_view
attendance_list: attendance_view
BoxLayout:
size_hint_y: None
height: "40dp"
Button:
text: 'Add meeting'
size_hint_x: 20
on_press: root.add_meeting()
Button:
text: 'Delete Meeting'
size_hint_x: 20
on_press: root.delete_meeting()
ListView:
id: meeting_view
adapter:
ListAdapter(data = app.r, cls = main.MeetingListButton)
ListView:
id: attendance_view
adapter:
ListAdapter(data = [], cls = main.AttendanceListButton, selection_mode = 'multiple')
第一个问题是您将 remove()
的执行存储为 on_press
的绑定函数,而不是函数 remove
.
因此,执行以下操作会更有意义:
on_press = App.get_running_app().root.ids.meeting_list.adapter.data.remove
但我会将调用转换为用户创建的函数,以便您可以更好地控制所发生的事情,方法如下:
class MeetingDB(BoxLayout):
meeting_list = ObjectProperty()
def remove_thing(self, event):
selection = self.meeting_list.adapter.selection[0].text
App.get_running_app().root.ids.meeting_list.adapter.data.remove(selection),
def delete_meeting(self):
if self.meeting_list.adapter.selection:
self.box_popup = BoxLayout(orientation = 'horizontal')
self.box_popup.add_widget(Label(text = "Confirm the deletion of the meeting ?"))
self.box_popup.add_widget(Button(
text = "Yes",
on_press = self.remove_thing
size_hint = (0.215, 0.075))
)
self.popup_exit.open()
现在,我没有安装 Kivy,我无法测试这段代码。
如果它不完美,我们深表歉意,但你应该明白了。
如果用户单击删除会议按钮,我想从列表视图中删除一个项目。
在这种情况下,将出现一个弹出窗口,要求确认删除会议。
一切正常,但当用户单击“是”按钮(以确认删除会议)时,我未能从列表视图中删除会议。
我想为“是”按钮写信:
on_press : self.meeting_list.adapter.data.remove(selection) and self.meeting_list._trigger_reset_populate()
class MeetingDB(BoxLayout):
meeting_list = ObjectProperty()
attendance_list = ObjectProperty()
def delete_meeting(self):
if self.meeting_list.adapter.selection:
selection = self.meeting_list.adapter.selection[0].text
self.box_popup = BoxLayout(orientation = 'horizontal')
self.box_popup.add_widget(Label(text = "Confirm the deletion of the meeting ?"))
self.box_popup.add_widget(Button(
text = "Yes",
on_press = App.get_running_app().root.ids.meeting_list.adapter.data.remove(selection),
size_hint = (0.215, 0.075)))
self.box_popup.add_widget(Button(
text = "No",
on_press = lambda *args: self.popup_exit.dismiss(),
size_hint=(0.215, 0.075)))
self.popup_exit = Popup(title = "Delete Meeting Confirmation",
content = self.box_popup,
size_hint = (0.4, 0.4),
auto_dismiss = True)
self.popup_exit.open()
#self.meeting_list.adapter.data.remove(selection)
#self.meeting_list._trigger_reset_populate()
Kv 文件:
<MeetingDB>:
orientation:'vertical'
meeting_list: meeting_view
attendance_list: attendance_view
BoxLayout:
size_hint_y: None
height: "40dp"
Button:
text: 'Add meeting'
size_hint_x: 20
on_press: root.add_meeting()
Button:
text: 'Delete Meeting'
size_hint_x: 20
on_press: root.delete_meeting()
ListView:
id: meeting_view
adapter:
ListAdapter(data = app.r, cls = main.MeetingListButton)
ListView:
id: attendance_view
adapter:
ListAdapter(data = [], cls = main.AttendanceListButton, selection_mode = 'multiple')
第一个问题是您将 remove()
的执行存储为 on_press
的绑定函数,而不是函数 remove
.
因此,执行以下操作会更有意义:
on_press = App.get_running_app().root.ids.meeting_list.adapter.data.remove
但我会将调用转换为用户创建的函数,以便您可以更好地控制所发生的事情,方法如下:
class MeetingDB(BoxLayout):
meeting_list = ObjectProperty()
def remove_thing(self, event):
selection = self.meeting_list.adapter.selection[0].text
App.get_running_app().root.ids.meeting_list.adapter.data.remove(selection),
def delete_meeting(self):
if self.meeting_list.adapter.selection:
self.box_popup = BoxLayout(orientation = 'horizontal')
self.box_popup.add_widget(Label(text = "Confirm the deletion of the meeting ?"))
self.box_popup.add_widget(Button(
text = "Yes",
on_press = self.remove_thing
size_hint = (0.215, 0.075))
)
self.popup_exit.open()
现在,我没有安装 Kivy,我无法测试这段代码。
如果它不完美,我们深表歉意,但你应该明白了。