微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我在 tkinter 中使用 messagebox.askokcancel 函数时遇到问题

如何解决我在 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()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。