在GUI中创建一个按钮,该按钮将删除.txt文件的内容



嗨,我正在尝试为应用程序制作一个清晰的按钮。当我点击clear按钮时,什么也没发生(这可能是因为我懂python)。我到处找,但我找不到答案,所以如果有人能帮助我,那将是伟大的(顺便说一下,我张贴了我写的所有代码)。

import tkinter as tk
from tkinter import filedialog, Text
import os
root = tk.Tk()
root.title('Application Launcher')
apps = []
if os.path.isfile('save.txt'):
with open('save.txt', 'r') as f:
tempApps = f.read()
tempApps = tempApps.split(',')
apps = [x for x in tempApps if x.strip()]

def addApp():
for widget in frame.winfo_children():
widget.destroy()
filename= filedialog.askopenfilename(initialdir="/", title="Select File",
filetypes=(("executables", "*.exe"), ("all files", "*.*")))
apps.append(filename)
print(filename)
for app in apps:
label = tk.Label(frame, text=app, bg="gray")
label.pack()
def runApps():
for app in apps:
os.startfile(app)
def clearApps():
if 'save.txt' in os.path.("save.txt")():
os.remove('save.txt')
tk.messagebox.showinfo('Removed!', 'Successfully Removed!')
else:
tk.messagebox.showerror('Error!', 'File not found!')

canvas = tk.Canvas(root, height=500, width=500, bg="#262e2e")
canvas.pack()
frame = tk.Frame(root, bg="#262e2e")
frame.place(relwidth=0.7, relheight=0.7, relx=0.1, rely=0.1)
openFile = tk.Button(root, text="Open File", padx=10, pady=5, fg="black", bg="white", command=addApp)
openFile.pack()
runApps = tk.Button(root, text="Run Apps", padx=10, 
pady=5, fg="black", bg="white", command= runApps)
runApps.pack()
clearApps = tk.Button(root, text="Clear")
clearApps.pack(side=tk.LEFT)
for app in apps:
label = tk.Label(frame, text=app)
label.pack()
root.mainloop()
with open('save.txt', 'w') as f:
for app in apps:
f.write(app + ',')

如果你说的是清除应用程序列表,你没有给你的清除按钮分配一个命令。

clearApps = tk.Button(root, text="Clear", command=clearApps)

最新更新