Python 3.x 单击按钮时,函数将运行
我正在使用Python3.x制作一个windows应用程序。我的职责是:Python 3.x 单击按钮时,函数将运行,python-3.x,tkinter,Python 3.x,Tkinter,我正在使用Python3.x制作一个windows应用程序。我的职责是: def deletefolder(): currentUser=os.getlogin() folderPath="C:/users/{username}/appdata/roaming/Strategy Folder/Home/PROFIILE/CACHE".format(username=getpass.getuser()) shutil.rmtree(folderPath) os
def deletefolder():
currentUser=os.getlogin()
folderPath="C:/users/{username}/appdata/roaming/Strategy Folder/Home/PROFIILE/CACHE".format(username=getpass.getuser())
shutil.rmtree(folderPath)
os.rmdir(folderPath)
delbtn = Button(deltab, text="Delete Cache", command=deletefolder)
delbtn.pack(pady=20)
当我去测试它时,它会立即运行“deletefolder”函数。我希望它在我单击按钮时运行。我试着做一个if/then语句,然后让一个标签显示它是完整的。相反,它似乎一直在运行,并给出一个错误,文件夹不再存在(因为代码实际上是用来删除文件夹的)。我在这里看了几篇文章,似乎if/then可能是最好的。但是,如果它在程序运行时开始运行,那就不好了,因为它会一次又一次地占用资源
谢谢你的帮助 导入消息框:
from tkinter import messagebox
并使用try,但以下情况除外:
def deletefolder():
try:
currentUser=os.getlogin()
folderPath="C:/users/{username}/appdata/roaming/Strategy Folder/Home/PROFIILE/CACHE".format(username=getpass.getuser())
shutil.rmtree(folderPath)
os.rmdir(folderPath)
except Exception:
messagebox.showinfo('Warning','Folder not exist')
您发布的代码不会执行您所说的操作。请提供一份报告。