繁体   English   中英

有没有办法从泡菜文件中清除所有数据?

[英]Is there a way to clear al data from a pickle file?

我有一个程序,用户可以在程序中添加一个“项目”,然后这个输入被存储在一个二维列表中。 然后我使用 pickle 存储这些数据,以便在您重新启动程序时它仍然存在。 有一个 window 显示了用户创建的所有不同“项目”,这里我想要一个按钮,让用户可以一次性删除他们创建的所有项目。

这将用作“清除”按钮,并允许用户删除所有创建的项目,而无需单独删除每个项目

import tkinter as tk
import pickle

allClothing = []

filename = "savedClothing"

infile = open(filename, "rb")
allClothing = pickle.load(infile)
infile.close()
print(allClothing)

TagsOptionList = [
    "Warm",
    "Light",
    "Fancy",
    "Cozy"
]

ColorOptionList = [
    "Black",
    "White",
    "Light Blue",
    "Dark Blue",
    "Yellow",
    "Dark Green",
    "Light Green",
    "Brown",
    "Light Red",
    "Dark Red",
    "Light Gray",
    "Dark Gray"
]

PatternOptionList = [
    "Striped",
    "Checked",
    "One-Colored",
    "Print"
]

TypeOptionList = [
    "T-Shirt",
    "Hoodie",
    "Jeans",
    "Shorts",
    "Jacket",
    "Sweater",
    "Sock",
    "Underwear",
    "Coat",
    "Suit",
    "Dress",
    "Sportswear",
    "Top",
    "V-Neck"
]

inputName = ""
selectTag = ""
selectColor = ""
selectPattern = ""
selectType = ""

def saveCurrentWardrobe():
    outfile = open(filename, "wb")
    pickle.dump(allClothing, outfile)
    outfile.close()


#def resetWardrobe():
    #This where the I want should go


def addClothingWindow():
    def addClothing():
        global inputName
        inputName = clothingNameEntry.get()

        if inputName != "":
            global selectTag
            selectTag = tag.get()
            global selectColor
            selectColor = color.get()
            global selectPattern
            selectPattern = pattern.get()
            global selectType
            selectType = type.get()

            print(inputName, selectType, selectTag, selectPattern, selectColor)
            allClothing.append([inputName, selectColor, selectType, selectTag, selectPattern])

            outfile = open(filename, "wb")
            pickle.dump(allClothing, outfile)
            outfile.close()

            clothingWindow.destroy()

    clothingWindow = tk.Toplevel(root)
    clothingWindow.title("Add Clothing")
    clothingWindow.geometry("800x800")

    addClothingFrame = tk.Frame(clothingWindow, bg="#884dff")
    addClothingFrame.place(relx=0, rely=0, relwidth= 1, relheight=1)

    addClothingInnerFrame = tk.Frame(clothingWindow, height=600, width=600, bg="#80ccff")
    addClothingInnerFrame.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.8)

    clothingNameEntry = tk.Entry(addClothingInnerFrame, bg="#9999ff")
    clothingNameEntry.place(relx=0, rely=0.1, relwidth=0.5, relheight=0.06)

    tag = tk.StringVar(clothingWindow)
    tag.set(TagsOptionList[0])
    opt = tk.OptionMenu(addClothingInnerFrame, tag, *TagsOptionList)
    opt.place(relx=0, rely=0.22, relwidth=0.5)

    color = tk.StringVar(clothingWindow)
    color.set(ColorOptionList[0])
    dropDownColor = tk.OptionMenu(addClothingInnerFrame, color, *ColorOptionList)
    dropDownColor.place(relx=0, rely=0.32, relwidth=0.5)

    pattern = tk.StringVar(clothingWindow)
    pattern.set(PatternOptionList[0])
    dropDownPattern = tk.OptionMenu(addClothingInnerFrame, pattern, *PatternOptionList)
    dropDownPattern.place(relx=0, rely=0.42, relwidth=0.5)

    type = tk.StringVar(clothingWindow)
    type.set(TypeOptionList[0])
    dropDownType = tk.OptionMenu(addClothingInnerFrame, type, *TypeOptionList)
    dropDownType.place(relx=0, rely=0.52,relwidth=0.5)

    addClothingButton = tk.Button(addClothingInnerFrame, text="Add Clothing", bg="#9999ff", command=addClothing)
    addClothingButton.place(relx=0, rely=0.7, relwidth=0.5, relheight=0.15)


def myWardrobeWindow():
    print(allClothing)

    wardrobeWindow = tk.Toplevel(root)
    wardrobeWindow.title("My Wardrobe")
    wardrobeWindow.geometry("800x800")

    wardrobeFrame = tk.Frame(wardrobeWindow, bg="#884dff")
    wardrobeFrame.place(relx=0, rely=0, relwidth=1, relheight=1)

    wardrobeInnerFrame = tk.Frame(wardrobeWindow, height=600, width=600, bg="#80ccff")
    wardrobeInnerFrame.place(relx=0.1, rely=0.1, relheight=0.8, relwidth=0.8)

    resetButton = tk.Button(wardrobeWindow, text="Reset", bg="#9999ff")
    resetButton.place(relx=0.2, rely=0.85, relwidth=0.2, relheight=0.05)

    backButton = tk.Button(wardrobeWindow, text="Save and Return to Menu", bg="#9999ff")
    backButton.place(relx=0.6, rely=0.85, relwidth=0.2, relheight=0.05, command=saveCurrentWardrobe)

    for a in range(len(allClothing)):
            print(a)
            print(allClothing[0][0])
            text = allClothing[a][0] + ": " + allClothing[a][1] + ", " + allClothing[a][2] + ", " + allClothing[a][3] + ", " + allClothing[a][4]
            myClothingLabel = tk.Label(wardrobeInnerFrame, text=text, bg="#9999ff", height=2, width=100, anchor="w")
            myClothingLabel.grid(row=a, column=0, pady=15)


root = tk.Tk()

canvas = tk.Canvas(root, height=700, width=800)
canvas.pack()

'''
backgroundImage = tk.PhotoImage(file='simpleWallpaper2.png')
backgroundLabel = tk.Label(root, image=backgroundImage)
backgroundLabel.place(relx=0, rely=0, relheight=1, relwidth=1)
'''

frame = tk.Frame(root, bg="#884dff")
frame.place(relx=0, rely=0, relwidth=1, relheight=1)

innerFrame = tk.Frame(root, bg="#80ccff")
innerFrame.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.8)

addButton = tk.Button(innerFrame, text="Add", bg="#9999ff", command=addClothingWindow)
addButton.place(relx=0.5, rely=0.2, relwidth=0.25, relheight=0.25, anchor="n")

myWardrobeButton = tk.Button(innerFrame, text="My Wardrobe", bg="#9999ff", command=myWardrobeWindow)
myWardrobeButton.place(relx=0.5, rely=0.6, relwidth=0.25, relheight=0.25, anchor="n")

nameLabel = tk.Label(innerFrame, text="SmartRobe", bg="#9999ff")
nameLabel.place(relx=0.5, rely=0, relwidth=0.75, relheight=0.1, anchor="n")

root.mainloop()

当您以写入模式打开文件时,它会删除它:

open("filename", "w").close()

好的,这可能不是最好的方法,但这就是我所做的。 我拿了 2D 列表并将其清除,然后我将现在为空的列表转储到文件中并关闭它。 然后我迅速关闭并重新打开 window 以清除图形:)

def resetWardrobe():
    allClothing.clear()
    openfile = open(filename, "wb")
    pickle.dump(allClothing, openfile)
    openfile.close()

    wardrobeWindow.destroy()
    myWardrobeWindow()

    printAllClothing()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM