Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在不退出程序的情况下退出tkinter中的窗口?_Python_User Interface_Tkinter_Command_Quit - Fatal编程技术网

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

Python 如何在不退出程序的情况下退出tkinter中的窗口?,python,user-interface,tkinter,command,quit,Python,User Interface,Tkinter,Command,Quit,我希望第二个“回车”按钮允许用户从此窗口退出。命令是什么?我相信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

我希望第二个“回车”按钮允许用户从此窗口退出。命令是什么?我相信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()

一开始有很多bug,最值得注意的是:

command=Enter_Name_Window.quit
应该是

command=self.destroy
不要使用不稳定的
quit()
方法,而是传递类实例
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()

您的代码格式混乱,难以阅读。您所说的“从此窗口退出”是什么意思?你是在问如何破坏窗户,还是只是把它藏起来?