【问题标题】:I have a problem using the messagebox.askokcancel function in tkinter我在 tkinter 中使用 messagebox.askokcancel 函数时遇到问题
【发布时间】:2021-07-11 11:20:55
【问题描述】:

我正在尝试在某个操作完成后弹出一条消息,但确定按钮不起作用。

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

【问题讨论】:

  • 当您单击Cancel 时,它会返回False。你试过打印吗?
  • 什么意思
  • 如果我点击确定按钮没有任何反应。
  • x=messagebox.askokcancel("Delete all Songs","Are you sure you want to delete all Songs"), print(x)
  • 对我来说效果很好,单击“确定”按钮时控制台中会显示“正在执行”。

标签: python tkinter popup messagebox


【解决方案1】:

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()

【讨论】:

    猜你喜欢
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多