【问题标题】:Trying to get my GUI windows automatically exit when I open the next window当我打开下一个窗口时,试图让我的 GUI 窗口自动退出
【发布时间】:2021-12-30 19:46:06
【问题描述】:

如何在单击下一个窗口按钮时自动取消第一个窗口?

示例代码:

from tkinter import *
from tkinter import messagebox

root = Tk()
root.title("GUI practice")

def open():
    top = Toplevel()  # new window
    top.title("Kokomi")
    labels = Label(top, text="This one automatically close when i click the next window").pack()
    button2 = Button(top,text="Close window", command=top.destroy).pack()
    button3 = Button(top,text="Next window", command=open2).pack()

def open2():
    top = Toplevel()  # new window
    top.title("Guide")
    labels = Label(top, text="end").pack()
    button2 = Button(top, text="Close window", command=top.destroy).pack()  # destroy to quit things

button = Button(root, text="Open(No need to close this)", command=open).pack()


root.mainloop()

[Click open][1]
[Click Next window and after that this windows should disappear and continue to the 3rd picture][2]
[The 2nd picture goes disappear when i click the next window][3]

  [1]: https://i.stack.imgur.com/plS1T.png
  [2]: https://i.stack.imgur.com/EFW76.png
  [3]: https://i.stack.imgur.com/xSZCp.png

【问题讨论】:

  • 你需要两个open函数吗?
  • 是的先生...当我单击下一个窗口时,我试图让另一个消失..但是两个命令不起作用
  • 我觉得这里还有一些问题,你能不能把你的目标说清楚一点。你为什么要打开两个窗口
  • 我正在尝试制作地图定位器 GUI,这是我在大学时的一个项目。我需要多个窗口,例如 15 左右。
  • 那你打算做15个函数吗?我可以为您提供这 2 个功能的答案,但如果功能数量要增加,那么您可能需要更好的解决方案,这就是为什么我要求您更好地解释您的案例的原因。这 15 个窗口会相似吗?或这些窗口中的一些模式

标签: python user-interface tkinter window


【解决方案1】:

对于这种使用两个函数和两个窗口的特定情况,您只需将Toplevel 小部件重命名为不同的名称,然后将其中一个全局化,然后从另一个函数访问它并在显示新窗口之前将其销毁。

def open():
    global top
    top = Toplevel()  # new window
    top.title("Kokomi")

    Label(top, text="This one automatically close when i click the next window").pack()
    Button(top, text="Close window", command=top.destroy).pack()
    Button(top, text="Next window", command=open2).pack()

def open2():
    top.destroy() # Destroy previously open window
    top1 = Toplevel()  # new window
    top1.title("Guide")

    Label(top1, text="end").pack()
    Button(top1, text="Close window", command=top1.destroy).pack()  # destroy to quit things

如果你注意到了,我删除了按钮和标签的变量名,因为它们的值是None,阅读Tkinter: AttributeError: NoneType object has no attribute <attribute name>

当您希望使用更多功能和窗口时,您必须手动按照此步骤操作所有功能。当然,除非有更好、更简洁的方式来使用类和框架来设计您的应用。

或者您也可以从一个按钮调用两个函数,这种方法将摆脱全球化和重命名,并且可能比上述解决方案更好一点:

def open():
    top = Toplevel()  # new window
    top.title("Kokomi")

    Label(top, text="This one automatically close when i click the next window").pack()
    Button(top, text="Close window", command=top.destroy).pack()
    Button(top, text="Next window", command=lambda: [top.destroy(),open2()]).pack()

def open2():
    top = Toplevel()  # new window
    top.title("Guide")

    Label(top, text="end").pack()
    Button(top, text="Close window", command=top.destroy).pack()  # destroy to quit things

【讨论】:

  • 非常感谢先生的帮助和指导
  • @KurtLee 如果它解决了您的问题,请确保标记为正确答案:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多