重新启动程序后将列表附加到泡菜数据中

每次我重新启动程序时,它都会显示以前附加的列表和腌制的列表。当我尝试将新数据添加到列表时,会出现问题。先前存储的数据将被删除,新数据将被腌制。我真的很困惑,并且一直在努力解决这一问题长达2天。任何帮助都将是惊人的,谢谢!

关于我的程序的背景: 我正在尝试制作一个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()

bufenglangzi 回答:重新启动程序后将列表附加到泡菜数据中

您需要首先读取泡菜数据。您正在打开要写入的文件,但未读取之前要附加到变量的数据。打开“ 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()
本文链接:https://www.f2er.com/3113767.html

大家都在问