代码之家  ›  专栏  ›  技术社区  ›  Ambrose Odongkara

为什么我的数据存储不能在python上使用pickle?

  •  -1
  • Ambrose Odongkara  · 技术社区  · 7 年前

    我基本上是在python上制作一个tkinter应用程序,在这里我可以添加条目,然后从提供的菜单中手动保存它们。现在我正在使用python的pickle模块将字典保存到字节和文件中。但是当我使用必要的编程来保存/.将字典转储到文件中并加载/.加载文件时。有个问题。

    from tkinter import *
    import tkinter as tk
    from tkinter import messagebox
    import datetime
    import pickle
    
    Dictionary = {"Admin":["Test", "Testing App", "027745"]} #No data Because nothing is loaded
    print(Dictionary)
    
    BackgroundColor = "#D3D3D3"
    Height = 350
    Width = 550
    
    window = Tk()
    window.title("Record Library | By Ambrose")
    window.configure(background=BackgroundColor)
    window.minsize(width = Width, height = Height)
    window.maxsize(width = Width, height = Height)
    #-------------------------------------------
    # Variables
    #-------------------------------------------
    
    TimeRightNow = str(datetime.datetime.now())
    FilteredTime = str("Date : " + TimeRightNow[0] + TimeRightNow[1] + TimeRightNow[2] + TimeRightNow[3] + TimeRightNow[4] + TimeRightNow[5] + TimeRightNow[6] + TimeRightNow[7] + TimeRightNow[8] + TimeRightNow[9] + " | Time : " + TimeRightNow[11] + TimeRightNow[12] + TimeRightNow[13] + TimeRightNow[14] + TimeRightNow[15])
    
    NameTitle_Txt = StringVar()
    Information_Txt = StringVar()
    Extra_Txt = StringVar()
    IDCode_Txt = StringVar()
    #-------------------------------------------
    # Functions
    #-------------------------------------------
    
    def Save():
        print("_Saving... | " + FilteredTime)
        DataFile_ToSave = open("RecordLibrary_Data.p","wb")#Save Data
        pickle.dump(Dictionary, DataFile_ToSave)
        DataFile_ToSave.close()
    
    def Load():
        print("_Loading... | " + FilteredTime)
        DataFile_ToLoad = open("RecordLibrary_Data.p","rb") #Load Data
        Dictionary = pickle.load(DataFile_ToLoad)
        DataFile_ToLoad.close()
    
    def ShowData():
        print("_Loading Data")
        print("_Data loaded : " + str(Dictionary))
    def ViewAll_Command():
        print("_Viewing All | " + FilteredTime)
    
    def SearchEntry_Command():
        print("_Searching Entry | " + FilteredTime)
    
    def AddEntry_Command():
        print("_Adding Entry | " + FilteredTime)
        RecordBox.insert(tk.END, "Name/Title : " + NameTitle_Txt.get() + " | Information : " + Information_Txt.get() + " | Extra :" + Extra_Txt.get() + " | Id : " + IDCode_Txt.get())
        Dictionary[str(NameTitle_Txt)] = [str(Information_Txt), str(Extra_Txt), str(IDCode_Txt)]
    
    def UpdateEntry_Command():
        print("_Updating Entry | " + FilteredTime)
    
    def DeleteEntry_Command():
        print("_Deleting Entry | " + FilteredTime)
    
    #-------------------------------------------
    # Labels [DONE]
    #-------------------------------------------
    NameLabel = Label(window, text="Name / Title", background=BackgroundColor)
    NameLabel.grid(row=0, column=0)
    
    InformationLabel = Label(window, text="Information", background=BackgroundColor)
    InformationLabel.grid(row=0, column=2)
    
    ExtraLabel = Label(window, text="Extra", background=BackgroundColor)
    ExtraLabel.grid(row=1, column=0)
    
    IdCodeLabel = Label(window, text="ID / Code", background=BackgroundColor)
    IdCodeLabel.grid(row=1, column=2)
    
    #-------------------------------------------
    # Entries / Entry Boxes / List
    #-------------------------------------------
    
    e1 = Entry(window, textvariable = NameTitle_Txt)
    e1.grid(row=0, column=1)
    
    Information_Txt = StringVar()
    e2 = Entry(window, textvariable = Information_Txt)
    e2.grid(row=0, column=3)
    
    Extra_Txt = StringVar()
    e3 = Entry(window, textvariable = Extra_Txt)
    e3.grid(row=1, column=1)
    
    IDCode_Txt = StringVar()
    e3 = Entry(window, textvariable = IDCode_Txt)
    e3.grid(row=1, column=3)
    
    RecordBox = Listbox(window, height=15, width=50)
    RecordBox.grid(row=2, column=0, rowspan=6, columnspan=2)
    
    sb1 = Scrollbar(window)
    sb1.grid(row=2, column=2, rowspan=6)
    
    sb2 = Scrollbar(window, orient = HORIZONTAL)
    sb2.grid(row=9, column=2, rowspan=6)
    
    RecordBox.configure(yscrollcommand=sb1.set, xscrollcommand = sb2.set)
    sb1.configure(command=RecordBox.yview)
    sb2.configure(command=RecordBox.xview)
    
    #RecordBox.bind('<<ListboxSelect>>', get_selected_row)
    
    #-------------------------------------------
    # Buttons [DONE]
    #-------------------------------------------
    b1 = Button(window, text="View all", width=12, command = ViewAll_Command)
    b1.grid(row=2, column=3)
    
    b2 = Button(window, text="Search entry", width=12, command = SearchEntry_Command)
    b2.grid(row=3, column=3)
    
    b3 = Button(window, text="Add entry", width=12, command = AddEntry_Command)
    b3.grid(row=4, column=3)
    
    b4 = Button(window, text="Update selected", width=12, command = UpdateEntry_Command)
    b4.grid(row=5, column=3)
    
    b5 = Button(window, text="Delete selected", width=12, command = DeleteEntry_Command)
    b5.grid(row=6, column=3)
    
    b6 = Button(window, text="Close", width=12, command = window.destroy)
    b6.grid(row=7, column=3)
    
    #-------------------------------------------
    # Menu
    #-------------------------------------------
    MenuBar = Menu(window)
    
    FileMenu = Menu(MenuBar, tearoff=0)
    FileMenu.add_separator()
    FileMenu.add_command(label="Load", command = Load)
    FileMenu.add_separator()
    FileMenu.add_command(label="Save", command = Save)
    FileMenu.add_separator()
    FileMenu.add_command(label="Show Data", command = ShowData)
    MenuBar.add_cascade(label="Data", menu=FileMenu)
    
    #-------------------------------------------
    # Last things
    #-------------------------------------------
    
    window.config(menu=MenuBar)
    window.mainloop()
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   FlyingTeller AEgo    7 年前
    def Load():
        print("_Loading... | " + FilteredTime)
        DataFile_ToLoad = open("RecordLibrary_Data.p","rb") #Load Data
        Dictionary = pickle.load(DataFile_ToLoad)
        DataFile_ToLoad.close()
    

    这将指定 pickle.load 一个新的局部变量 Dictionary

    def Load():
        global Dictionary
        print("_Loading... | " + FilteredTime)
        DataFile_ToLoad = open("RecordLibrary_Data.p","rb") #Load Data
        Dictionary = pickle.load(DataFile_ToLoad)
        DataFile_ToLoad.close()