0

我有一个删除按钮,可以从另一个页面删除小部件。但是,该按钮实际上并没有删除小部件。该按钮还应该删除附加到小部件的 json 对象,但它也不这样做。

代码:

    # displays the streak that was clicked on in screen two
    def display_streak(self, obj):
        self.third_screen()
        name = obj.id
        bottle = self.root.get_screen("three")
        can = self.root.get_screen('two')
        bottle.ids.del_space.add_widget(Button(id=name, text="Delete", size=(60,25), size_hint=(None,None),
                                    font_size=18, on_press=self.del_button)) # fix later
def del_button(self, obj):
        bottle = self.root.get_screen("two")
        can = self.root.get_screen('three')
        name = obj.id

        with open("streak.json", "r+") as f:
            data = json.load(f)

        for child in self.root.screen_two.ids.streak_zone.children:
            if child.text == name:
                print("delete")
                bottle.screen_two.ids.streak_zone.remove_widget(child)

                for i in xrange(len(data)):
                    if data[i] == name:
                        data.pop(i)
                        break

                open("streak.json", "w").write(json.dump(data, sort_keys=True, indent=4))

我试图将 Button 的 id 与del_space小部件内的文本进行比较,streak_zone以便删除,但由于某种原因,这不起作用。我什至从 obj 传递过来on_press=self.del_button,它仍然无法正常工作,怎么会?

编辑

objindisplay_streak()是从将函数绑定到按钮小部件的条件传递的:

elif delay > time.time() > self.honey:  # on time (green)
                    child.background_normal = ''
                    child.background_color = [0, 1, 0, .95]
                    child.unbind(on_press=self.early_click)
                    child.bind(on_press=self.add_score)
                    child.bind(on_press=self.display_streak)
                    child.bind(on_press=self.draw_streak)

编辑

我不确定,但我相信问题可能来自我的 kivy 代码:

<ScreenTwo>
    id: screen_two
    name: "two"
    on_leave: app.restart()
    on_enter: app.display_btn()
    ScrollView:
        GridLayout:
            cols: 2
            rows: 1
            ScrollView:
                BoxLayout:
                    id: streak_zone
                    orientation: 'vertical'
                    height: self.minimum_height

进入第二页时display_btn被激活

Python:

def display_btn(self):
        ...

            for value in data.values():
                if value['delta'] is not None:
                    print(f"action={value['action']}, delta={value['delta']}, grace={value['delay']}")
                    streak_button = StreakButton(id=(value['action']), text=value['action'],
                                                 color=(0,0,0,1), size=(400, 50),
                                                 size_hint=(None, None))
                    self.root.screen_two.ids.streak_zone.add_widget(streak_button)
...

如果这是按钮不会被删除的原因,那么我将如何解决这个问题?

4

1 回答 1

0

由于kv文件在离开时会删除小部件,因此我只需删除与小部件具有相同id的json元素

def del_button(self, object):
        name = object.id

        with open("streak.json", "r") as f:
            data = json.load(f)

        with open("streak.json", "r+") as file:
            data = json.load(file)
            data.pop(name, None)
            file.seek(0)
            json.dump(data, file, indent=4)
            file.truncate()

        self.change_screen()

于 2019-06-01T03:58:18.157 回答