Kivy Popup确认删除列表视图项

时间:2019-02-17 08:33:06

标签: python popup 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')

1 个答案:

答案 0 :(得分:0)

第一个问题是您将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,并且无法测试此代码。
不好意思,所以道歉,但是您应该明白这个想法。