简体   繁体   English

如何在另一个函数中关闭 tkinter 窗口?

[英]How do you close a tkinter window in another function?

I want a button in my window to open a new window and close the previous one.我希望我的窗口中有一个按钮来打开一个新窗口并关闭上一个窗口。 Is it possible to have one button do both of these?是否有可能让一个按钮同时完成这两项工作? I've tried in the following code, but it hasn't worked, just told me that window is not defined:我在下面的代码中尝试过,但没有奏效,只是告诉我没有定义窗口:

import tkinter
def window1():
    window = tkinter.Tk()
    tkinter.Button(window, text = "Next", command = window2).pack()
    window.mainloop()
def window2():
    window.destroy() #This is where the error is
    menu = tkinter.Tk()
    etc, etc, etc
window1()

First, you need to return the window object from the first function:首先,您需要从第一个函数返回窗口对象:

def window1():
    window = tkinter.Tk()
    tkinter.Button(window, text = "Next", command = lambda: window2(window)).pack()
    window.mainloop()
    return window

Then, you need to pass the window as an argument to your function:然后,您需要将窗口作为参数传递给您的函数:

def window2(window):
    window.destroy()
    menu = tkinter.Tk()

And then call window1 with:然后使用以下命令调用 window1:

window = window1()

and click the button to destroy it and do the rest然后单击按钮将其销毁并完成剩下的工作

This is an example using Toplevels, which is usually a better choice than creating, destroying, re-creating Tk() instances.这是一个使用 Toplevels 的示例,这通常比创建、销毁、重新创建 Tk() 实例更好。 The unique Toplevel ID is passed to the close_it function using partial().使用 partial() 将唯一的 Toplevel ID 传递给 close_it 函数。 You would, of course, combine them or have the close function call the open function.当然,您可以将它们组合起来,或者让 close 函数调用 open 函数。

try:
    import Tkinter as tk     ## Python 2.x
except ImportError:
    import tkinter as tk     ## Python 3.x

from functools import partial

class OpenToplevels():
    """ open and close additional Toplevels with a button
    """
    def __init__(self):
        self.root = tk.Tk()
        self.button_ctr=0
        but=tk.Button(self.root, text="Open a Toplevel",
                      command=self.open_another)
        but.grid(row=0, column=0)
        tk.Button(self.root, text="Exit Tkinter", bg="red",
                  command=self.root.quit).grid(row=1, column=0, sticky="we")
        self.root.mainloop()

    def close_it(self, id):
        id.destroy()

    def open_another(self):
        self.button_ctr += 1
        id = tk.Toplevel(self.root)
        id.title("Toplevel #%d" % (self.button_ctr))
        tk.Button(id, text="Close Toplevel #%d" % (self.button_ctr),
                  command=partial(self.close_it, id),
                  bg="orange", width=20).grid(row=1, column=0)

Ot=OpenToplevels()

Yes.是的。 Is possible.是可能的。 But you'll need to def that:但是,你需要的是变形点焊

def window1:
   blablabla
   blablabla
def window2:
   window2.destroy() <-- Here where the error was

How you noticed, put your name of window what you want Destroy and it will work!你是怎么注意到的,把你想要的窗口名称写成销毁,它会起作用!

using Python3使用 Python3
You could use a "global" such as:您可以使用“全局”,例如:

root = Tk()
root.title('This is the root window')

def window_create():
    global window_one
    window_one = Tk()
    window_one.title('This is window 1')

Then, from any function (or elsewhere) when you want to destroy window_one, do:然后,当您想销毁 window_one 时,从任何函数(或其他地方)执行:

def window_destroyer():
    window_one.destroy()

You could call your window_destroyer function from a button anywhere such as root which the example shows:您可以从任何位置的按钮调用 window_destroyer 函数,例如示例显示的 root:

kill_window_btn = Button(root, text="Destroy", command=window_destroyer).pack()

Of course, follow your own naming conventions.当然,请遵循您自己的命名约定。 :) :)
It seems to me, just ' global window_one ' would solve it.在我看来,只需' global window_one ' 就可以解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM