python - 我在 tkinter 中使用 messagebox.askokcancel 函数时遇到问题
问题描述
我试图在完成某个操作后弹出一条消息,但确定按钮不起作用。
def delete_all_songs():
if messagebox.askokcancel("Delete all Songs","Are you sure you want to delete all Songs"):
print("doing")
filelisttodelete = [f for f in os.listdir("C:/MusicPlayer/Songs/") if f.endswith(".mp3")]
for f in filelisttodelete:
os.remove(os.path.join("C:/MusicPlayer/Songs/", f))
songs_box.delete(0, END)
pygame.mixer.music.stop()
else:
pass
解决方案
函数“ askokcancel()
...返回一个布尔值:True 表示“OK”或“Yes”选择,False 表示“No”或“Cancel”选择”——你必须使用它的返回值来弹出消息。这是一个独立的可运行示例:
from tkinter import *
from tkinter import messagebox
def delete_all_songs():
if messagebox.askokcancel("Delete all Songs",
"Are you sure you want to delete all Songs?"):
# filelisttodelete = [f for f in os.listdir("C:/MusicPlayer/Songs/")
# if f.endswith(".mp3")]
# for f in filelisttodelete:
# os.remove(os.path.join("C:/MusicPlayer/Songs/", f))
#
# songs_box.delete(0, END)
# pygame.mixer.music.stop()
messagebox.showinfo("Info", "All Songs Deleted")
else:
messagebox.showinfo("Info", "Song Deletion Canceled")
win = Tk()
Button(win, text="Test", command=delete_all_songs).pack()
Button(win, text="Quit", command=win.quit).pack()
win.mainloop()
推荐阅读
- c++ - 当我编译并即将推送元素时,出现分段错误
- scala - pureconfig Enumeratum 更好的异常
- php - 安装 datadog APM ddtrace php 后,我的应用程序无法正常工作
- sql - 将特殊字符存储在数据库中作为 html 转义等效字符是否是一种好习惯,例如。& 作为 &
- c# - 在.net中获取函数返回值“运行中”?
- http - 骆驼 http 在 url 中使用 ${body}
- android - 如何创建 Observables 并使用 Retrofit 后来的 RxJava 更新它们?
- angular7 - Angular-Calendar 上下文菜单不起作用如何调出?
- python - 将图像从文件夹 python 导入到 numpy 数组列表
- java - 当我在 iOS appium 中从本机切换到 webview 上下文时,Appium 驱动程序正在获取与当前页面 url 不同的 url