python - 重新启动程序后附加列表时,pickle 数据将被删除

标签 python python-3.x list tkinter pickle

每次我重新启动程序时,它都会显示之前附加到的列表和 pickle 列表。当我尝试将新数据添加到列表中时会出现问题。之前存储的数据将被删除,新数据将被 pickle 。我真的很困惑,两天来一直在试图解决这个问题。任何帮助都会很棒,谢谢!

我的程序的背景: 我正在尝试制作一个 GUI,它使我能够轻松保存各种类型的数据,以便以后可以使用该数据进行可视化表示。

这是我的整个程序:

import pickle
import os
os.system("clear")
import tkinter as tk

win = tk.Tk()
win.title("first gui")
#Program
#Functions
tips = []
days = []
amount_of_cars = []
weather = []
temperature = []



def add_all_stuff():
    pickle1 = open("tips.txt","wb")
    pickle2 = open("days.txt","wb")
    pickle3 = open("cars.txt","wb")
    pickle4 = open("weather.txt","wb")
    pickle5 = open("temperature.txt","wb")
    a1=tips_ask.get()
    a2=days_ask.get()
    a3=cars_ask.get()
    a4=weather_ask.get()
    a5=temperature_ask.get()
    print(temperature_ask.get())
    print(weather_ask.get())
    print(cars_ask.get())
    print(days_ask.get())
    print(tips_ask.get())
    tips.append(a1)
    days.append(a2)
    amount_of_cars.append(a3)
    weather.append(a4)
    temperature.append(a5)
    print(tips)
    print(days)
    print(amount_of_cars)
    print(weather)
    print(temperature)
    #pickle dumps
    pickle.dump(tips, pickle1)
    pickle1.close

    pickle.dump(days, pickle2)
    pickle2.close

    pickle.dump(tips, pickle3)
    pickle3.close

    pickle.dump(tips, pickle4)
    pickle4.close

    pickle.dump(tips, pickle5)
    pickle5.close

def show_lists():
    pickle_in1 = open("tips.txt", "rb")
    tips = pickle.load(pickle_in1)
    print(tips)

#labels
tk.Label(win, text = "menu").grid(row=0)
tk.Label(win, text= "Tips").grid(row=1)
tk.Label(win, text= "Day").grid(row=2)
tk.Label(win, text= "Amount of Cars").grid(row=3)
tk.Label(win, text= "Weather").grid(row=4)
tk.Label(win, text= "Temperature").grid(row=5)
#entryboxes
tips_ask = tk.Entry(win)
tips_ask.grid(row=1, column=1)

days_ask = tk.Entry(win)
days_ask.grid(row=2, column=1)

cars_ask = tk.Entry(win)
cars_ask.grid(row=3, column=1)

weather_ask = tk.Entry(win)
weather_ask.grid(row=4, column=1)

temperature_ask = tk.Entry(win)
temperature_ask.grid(row=5, column=1)
#Buttons
tk.Button(win, text="add", command = add_all_stuff).grid(row=6, column = 1)
tk.Button(win, text="View saved lists", command = show_lists).grid(row=6, column=2)
win.mainloop()

最佳答案

您需要先读取pickle数据。您正在打开文件进行写入,但不读取之前附加到的变量中的数据。打开“wb”会覆盖该文件。所以

    pickle1 = open("tips.txt","wb")

删除并打开一个空文件进行写入。

您可以添加

def add_all_stuff():
    tips = open("tips.txt","rb")
    days = open("days.txt","rb")
    amount_of_cars = open("cars.txt","rb")
    weather = open("weather.txt","rb")
    temperature = open("temperature.txt","rb")
    #... the rest of your code, that should help

更新:这是一个完整的程序,即使在关闭后也可以运行:

import pickle
import os
os.system("clear")
import tkinter as tk

win = tk.Tk()
win.title("first gui")
#Program
#Functions
tips = []
days = []
amount_of_cars = []
weather = []
temperature = []


def get_make_pickle_data(filename):
   if os.path.isfile(filename):
       pickle_data = pickle.load( open( filename, "rb" ) )
   else:
       pickle_data = []
       pickle.dump( pickle_data, open( filename, "wb" ) )
   return pickle_data


def add_all_stuff():

    tips=get_make_pickle_data('tips.txt')
    days=get_make_pickle_data('days.txt')
    amount_of_cars=get_make_pickle_data('amount_of_cars.txt')
    weather=get_make_pickle_data('weather.txt')
    temperature=get_make_pickle_data('temperature.txt')

    a1=tips_ask.get()
    a2=days_ask.get()
    a3=cars_ask.get()
    a4=weather_ask.get()
    a5=temperature_ask.get()
    print(temperature_ask.get())
    print(weather_ask.get())
    print(cars_ask.get())
    print(days_ask.get())
    print(tips_ask.get())
    tips.append(a1)
    days.append(a2)
    amount_of_cars.append(a3)
    weather.append(a4)
    temperature.append(a5)
    print(tips)
    print(days)
    print(amount_of_cars)
    print(weather)
    print(temperature)
    #pickle dumps


    pickle1 = open("tips.txt","wb")
    pickle2 = open("days.txt","wb")
    pickle3 = open("amount_of_cars.txt","wb")
    pickle4 = open("weather.txt","wb")
    pickle5 = open("temperature.txt","wb")


    pickle.dump(tips, pickle1)
    pickle1.close

    pickle.dump(days, pickle2)
    pickle2.close

    pickle.dump(amount_of_cars, pickle3)
    pickle3.close

    pickle.dump(weather, pickle4)
    pickle4.close

    pickle.dump(temperature, pickle5)
    pickle5.close

def show_lists():
    tips=get_make_pickle_data('tips.txt')
    days=get_make_pickle_data('days.txt')
    amount_of_cars=get_make_pickle_data('amount_of_cars.txt')
    weather=get_make_pickle_data('weather.txt')
    temperature=get_make_pickle_data('temperature.txt')
    print(tips,days,amount_of_cars,weather,temperature)

#labels
tk.Label(win, text = "menu").grid(row=0)
tk.Label(win, text= "Tips").grid(row=1)
tk.Label(win, text= "Day").grid(row=2)
tk.Label(win, text= "Amount of Cars").grid(row=3)
tk.Label(win, text= "Weather").grid(row=4)
tk.Label(win, text= "Temperature").grid(row=5)
#entryboxes
tips_ask = tk.Entry(win)
tips_ask.grid(row=1, column=1)

days_ask = tk.Entry(win)
days_ask.grid(row=2, column=1)

cars_ask = tk.Entry(win)
cars_ask.grid(row=3, column=1)

weather_ask = tk.Entry(win)
weather_ask.grid(row=4, column=1)

temperature_ask = tk.Entry(win)
temperature_ask.grid(row=5, column=1)
#Buttons
tk.Button(win, text="add", command = add_all_stuff).grid(row=6, column = 1)
tk.Button(win, text="View saved lists", command = show_lists).grid(row=6, column=2)
win.mainloop()

关于python - 重新启动程序后附加列表时,pickle 数据将被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58827988/

相关文章:

python - 在 Pandas 中拆分堆叠数据框

c++ - 使用accumulate方法但不能第一个元素总是显示为0

python - 将 R data.table 转换为 pandas.DataFrame 的最佳方法?

python - 在python中评估shellcode的方法?

Python 字典数组

python - numpy 中连续整数的总和不正确

python - Flask:如何自动化 OpenAPI v3 文档?

python-3.x - python包中的requirements.txt

list - 从列表中删除项目

python - 如何用python去除元组列表?