【问题标题】:How do I quit a window in tkinter without quitting program? [duplicate]如何在不退出程序的情况下退出 tkinter 中的窗口? [复制]
【发布时间】:2013-12-12 15:38:56
【问题描述】:

我希望第二个“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()

【问题讨论】:

  • 你的代码格式乱七八糟,看不懂代码。
  • “从这个窗口退出”是什么意思?您是在问如何销毁窗口,还是只是隐藏它?

标签: python user-interface tkinter command quit


【解决方案1】:

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

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    相关资源
    最近更新 更多