在尝试列出腌制数据时,我只获取上次存储的数据,如何显示所有腌制数据?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在尝试列出腌制数据时,我只获取上次存储的数据,如何显示所有腌制数据?相关的知识,希望对你有一定的参考价值。

我想在列表中显示所有我的腌制值。但是,当我运行程序并按下将我带到显示所有值的页面的按钮时,我只显示一个值。该值来自上次运行程序的时间。当我关闭程序并再次尝试时,它会显示我上次尝试的值。

这就是我所说的:https://imgur.com/a/m79BkMY

这是发生数据酸洗和显示的代码

class Streak():
    def __init__(self, action, streak_num, day, hour, minute, stored_streaks):
        self.action = action
        self.streak_num = streak_num
        self.day = day
        self.hour = hour
        self.minute = minute


    def get_time(self):
        self.action = app.frames[PageOne].action_text.get()
        self.streak_num = int(app.frames[PageOne].streak_num_text.get())
        self.day = int(app.frames[PageOne].due_day.get())
        self.hour = int(app.frames[PageOne].due_hour.get())
        self.minute = int(app.frames[PageOne].due_minute.get())

        stored_streaks = {}

        total = (self.day * 86400) + (self.hour * 3600) + (self.minute * 60)

        streak_input = {"action": self.action , "streak_num": self.streak_num,
                        "total": total}

        store_streak = stored_streaks.update(streak_input.copy())

        with open("streak.pkl", "wb") as wpickle_file:
            pickle.dump(stored_streaks, wpickle_file)

        print(streak_input)
        app.show_frame(PageTwo)


    def read_file(self):

        with open("streak.pkl", "rb") as pickle_file:
            stored_streaks = pickle.load(pickle_file)

        #stored_streaks.update(streak_input) ## comeback to this
        return stored_streaks

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        streak_title = tk.Label(self, text="New Streak", font=XLARGE_FONT)
        streak_title.pack()

        action_label = tk.Label(self, text="Action", font=LARGE_FONT)
        action_label.pack(anchor="w", padx=30, pady=10)

        self.action_text = tk.Entry(self, width=250)
        self.action_text.pack(anchor="w", padx=30)

        streak_num_label = tk.Label(self, text="Streak #", font=LARGE_FONT)
        streak_num_label.pack(anchor="w", padx=30, pady=10)

        self.streak_num_text = tk.Entry(self, width=250)
        self.streak_num_text.pack(anchor="w", padx=30, pady=10)

        day_label = tk.Label(self, text="Day(s)")
        day_label.pack()
        self.due_day = tk.Entry(self)
        self.due_day.pack()

        hour_label = tk.Label(self, text="Hour(s)")
        hour_label.pack()
        self.due_hour = tk.Entry(self)
        self.due_hour.pack()

        minute_label = tk.Label(self, text="Minute(s)")
        minute_label.pack()
        self.due_minute = tk.Entry(self)
        self.due_minute.pack()

        get_time_btn = tk.Button(self, bg="medium purple", text="Add",
                                command=lambda: Streak.get_time(self))
        get_time_btn.pack()

class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        test_label = tk.Label(self, text="TEST", font=XLARGE_FONT)
        test_label.pack()


        streak_list = Streak.read_file(self)

        action_list = streak_list.get("action", "none")

        display_list = [action_list]

        for action_list in display_list:
            list = tk.Button(self, bg="medium purple", text=action_list)
            list.pack()

        back_btn = tk.Button(self, bg="medium purple", text="BACK",
                            command=lambda: controller.show_frame(PageOne))
        back_btn.pack()

我包含print,所以我可以确保get_time函数正常工作。从第一张图片打印出来:

{'action':'first','streak_num':1,'total':120}

当我打印streak_list时,它会打印在程序关闭之前输入的数据

答案

我可以看到你正在创建你的Streak类的多个实例而不保留引用,因此只存储了最新的数据。尝试修改如下:

class Streak:
    def __init__(self):
        self.stored_streaks = {}

    def get_time(self):

        ...

        self.stored_streaks.update(streak_input.copy()) #dictionary update are done in place

        with open("streak.pkl", "wb") as wpickle_file:
            pickle.dump(self.stored_streaks, wpickle_file)

        print(streak_input)
        app.show_frame(PageTwo)

    def read_file(self):
        with open("streak.pkl", "rb") as pickle_file:
            self.stored_streaks = pickle.load(pickle_file)
        return self.stored_streaks

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.streak = Streak()

        ...

        get_time_btn = tk.Button(self, bg="medium purple", text="Add",command=self.streak.get_time)
        get_time_btn.pack()

以上是关于在尝试列出腌制数据时,我只获取上次存储的数据,如何显示所有腌制数据?的主要内容,如果未能解决你的问题,请参考以下文章

在腌制字典中检索单个对象的问题(Python 3)

如何使用 SharedPreferences 系统地存储和获取数据?

腌制数据--python(pickle标准库)

未腌制的对象与原始对象不匹配

如何列出所有 Informix 数据库名称

TypeError:无法腌制 CompiledFFI 对象