1

对于我正在创建的编辑 MIDI 文件的程序,我导入了tkinter.messagebox模块。我正在使用的消息框功能是askokcancel. 我希望在ok选择时关闭所有父窗口和子窗口。我该如何做到这一点?

我已经尝试去其他网站看看如何,但我没有找到任何答案。

from tkinter import *
import tkinter.messagebox

class Window(Frame):

        def init_window(self):

        menu = Menu(self.master)

        self.master.config(menu=menu)

        file = Menu(menu)

        file.add_command(label="Exit", command=self.client_exit)

        menu.add_cascade(label="File", menu=file)

    def exit(self):

        exit()

    def client_exit(self):

        messagebox.askokcancel('Exit?', 'Are you sure you want to exit?', default='ok') 

#Here, I want the "exit" function to be the function.

        if self.reading:

            self.top.quit()

app = Window(tk)

这只是我分享的代码示例。如果其他代码可能有错误,我会分享。

4

1 回答 1

2
def client_exit(self):
    MsgBox = messagebox.showinfo('Exit?', 'Are you sure you want to exit?',icon = 'warning')
    if MsgBox == 'ok':
        #Some code

在此处输入图像描述

或者:

def client_exit(self):
    MsgBox = messagebox.askquestion ('Exit?', 'Are you sure you want to exit?',icon = 'warning')
    if MsgBox == 'yes':
        # Your code
    else:
        # Your code

在此处输入图像描述

于 2019-04-30T12:55:56.393 回答