用Tkinter和Python 3关闭一个子窗口

时间:2017-04-25 10:14:27

标签: python tkinter window destroy

我正在尝试关闭弹出窗口,询问用户是否要关闭该程序。它有两个按钮,表示是/否。我已经设法关闭整个程序,但由于"错误的窗口路径名称"我正在努力关闭弹出窗口(如果没有被选中)。我怀疑我没有正确调用局部变量exit_warning,或者没有正确地销毁它。下面是我的代码的简化版本。

from tkinter import *

class Window(Frame):

    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.master = master
        self.init_window()

    def init_window(self):
        self.master.title("SePic")
        self.pack(fill = BOTH, expand = 1)
        ButtonExit = Button(root, text="Exit", command=show_exit_warning)
        ButtonExit.pack()

#command implementation

def show_exit_warning():
    exit_warning = Toplevel()
    w = Label(exit_warning, text='Are you sure you want to quit?')
    ButtonYes = Button(exit_warning, text="Yes", command=exitYesPress)
    ButtonNo = Button(exit_warning, text='No', command=exitNoPress(exit_warning))
    w.pack()
    ButtonYes.pack()
    ButtonNo.pack()

##General Implementations

def exitYesPress():
    exit()

def exitNoPress(exit_warning):
    exit_warning.destroy()

root = Tk()

app = Window(root)

root.mainloop()

我的理解是你想要x.destroy()来破坏一个Window' x'。但由于窗口exit_warning是show_exit_warning中的局部变量,您需要将其发送到exitNoPress以对其执行任何操作。它是否正确?任何其他建议/指导将非常感激。谢谢你的时间。

0 个答案:

没有答案