【问题标题】:How to determine if a client selected "ok" in a tkinter messagebox?如何确定客户端是否在 tkinter 消息框中选择了“ok”?
【发布时间】:2019-04-30 12:36:07
【问题描述】:

对于我正在创建的编辑 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)

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

【问题讨论】:

    标签: python python-3.x tkinter tkmessagebox


    【解决方案1】:
    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
    

    【讨论】:

    • 谢谢...我很感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 2011-06-12
    • 1970-01-01
    • 2012-04-05
    相关资源
    最近更新 更多