【问题标题】:How do you close a tkinter window in another function?如何在另一个函数中关闭 tkinter 窗口?
【发布时间】:2016-09-23 12:11:09
【问题描述】:

我希望我的窗口中有一个按钮来打开一个新窗口并关闭前一个窗口。是否可以一个按钮同时完成这两项工作?我试过下面的代码,但没有成功,只是告诉我没有定义窗口:

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

【问题讨论】:

  • 这是家庭作业吗?这是我在过去一个小时里看到的几乎完全是这个主题的第二个问题。

标签: python tkinter


【解决方案1】:

首先,你需要从第一个函数中返回窗口对象:

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

然后,您需要将窗口作为参数传递给您的函数:

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

然后调用window1:

window = window1()

然后单击按钮将其销毁并完成其余操作

【讨论】:

  • 您忽略了如何使用按钮传递参数的相当重要的细节。
  • return window 有点无意义,因为在函数返回时窗口已经被销毁了。
【解决方案2】:

这是一个使用 Toplevels 的示例,这通常是比创建、销毁、重新创建 Tk() 实例更好的选择。唯一的顶级 ID 使用 partial() 传递给 close_it 函数。当然,您可以将它们组合起来,或者让 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()

【讨论】:

    【解决方案3】:

    是的。是可能的。但是您需要定义

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

    你怎么注意到的,把你想要的窗口的名字写成Destroy,它会起作用的!

    【讨论】:

      【解决方案4】:

      使用 Python3
      您可以使用“全局”,例如:

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

      然后,当您想要销毁 window_one 时,从任何函数(或其他地方)执行:

      def window_destroyer():
          window_one.destroy()
      

      您可以从任何地方的按钮调用您的 window_destroyer 函数,例如示例所示的 root:

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

      当然,请遵循您自己的命名约定。 :)
      在我看来,只需 'global window_one' 就可以解决它。

      【讨论】:

        猜你喜欢
        • 2018-05-18
        • 1970-01-01
        • 2018-01-08
        • 2017-04-16
        • 2022-01-07
        • 1970-01-01
        • 2022-09-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多