如何在不退出程序的情况下退出tkinter中的窗口?

时间:2013-12-12 15:38:57

标签: python user-interface tkinter command quit

我想第二个'Enter'按钮允许用户退出此窗口。命令是什么?我相信self.quit会退出一切,但我使用的命令不起作用。

import tkinter as tk

class Enter_Name_Window(tk.Toplevel):
    '''A simple instruction window'''
    def __init__(self, parent):
        tk.Toplevel.__init__(self, parent)
        self.text = tk.Label(self, width=40, height=2, text= "Please enter your name and class." )
        self.text.pack(side="top", fill="both", expand=True)

        enter_name = Entry(self)
        enter_name.pack()
        enter_name.focus_set()


        def callback():
            self.display_name = tk.Label(self, width=40, height=2, text = "Now please enter your tutor group.")
            self.display_name.pack(side="top", fill="both", expand=True)
            tutor = Entry(self)
            tutor.pack()
            tutor.focus_set()
            Enter_0.config(state="disabled")

            Enter_0_2 = Button(self, text="Enter", width=10, command=Enter_Name_Window.quit)
            Enter_0_2.pack()

        Enter_0 = Button(self, text="Enter", width=10, command=callback)
        Enter_0.pack()

1 个答案:

答案 0 :(得分:1)

开始时有很多错误,最值得注意的是:

command=Enter_Name_Window.quit

应该是

command=self.destroy

避免使用quit()方法作为其unstable,并传递类实例self而不是新的类对象

这里的任何地方都是您修改后的代码:

class Enter_Name_Window(tk.Toplevel):
    '''A simple instruction window'''
    def __init__(self, parent):
        tk.Toplevel.__init__(self, parent)
        self.parent = parent
        self.text = tk.Label(self.parent, width=40, height=2, text= "Please enter your name and class." )
        self.text.pack(side="top", fill="both", expand=True)

        enter_name = tk.Entry(self)
        enter_name.pack()
        enter_name.focus_set()


        def callback():
            self.display_name = tk.Label(self.parent, width=40, height=2, text = "Now please enter your tutor group.")
            self.display_name.pack(side="top", fill="both", expand=True)
            tutor = tk.Entry(self.parent)
            tutor.pack()
            tutor.focus_set()
            Enter_0.config(state="disabled")

            Enter_0_2 = tk.Button(self.parent, text="Enter", width=10, command=self.destroy)
            Enter_0_2.pack()

        Enter_0 = tk.Button(self.parent, text="Enter", width=10, command=callback)
        Enter_0.pack()