python - 有没有办法从泡菜文件中清除所有数据?
问题描述
我有一个程序,用户可以在程序中添加一个“项目”,然后这个输入被存储在一个二维列表中。然后我使用 pickle 存储这些数据,以便在您重新启动程序时它仍然存在。有一个窗口,其中显示了用户创建的所有不同“项目”,这里我想要一个按钮,让用户一次删除他们创建的所有项目。
这将用作“清除”按钮,并允许用户删除所有创建的项目,而无需单独删除每个项目
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()
推荐阅读
- mysql - MySQL查询列值更改的时间戳
- reactjs - 在 react 中使用 react dnd 库上传文件功能
- php - 如何获取用户输入并将其发送到同一文件中的查询?
- c# - 并行分块与顺序分块文件上传
- angular - 无法更新科尔多瓦角度的插件
- javascript - 如何在 VueJS 应用程序中为不同主题实现多个资产路径?
- php - Foreach 循环在 html 输出中创建空表行
- windows - nsExec::ExecToStack 是否正在等待进程完成?(NSIS)
- python - 如何在 PyQt5 中列出内联项目
- c++ - Lennard Jones 势能返回 inf 的动能