python tkinter Toplevel .destroy()和.quit()不能按预期工作 [英] python tkinter Toplevel .destroy() vs .quit() not working as intended

查看:154
本文介绍了python tkinter Toplevel .destroy()和.quit()不能按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Duplicates 类,用于检查40个字以内的重复项.

I have a class Duplicates that checks for duplicates within 40 words.

我有一个 class窗口,它将创建并运行主窗口,并在其中发布结果.

I have a class Window that creates and runs the main window where i post the result.

我有一个 class popWindow ,当询问用户如何处理可能的double时会创建一个顶级窗口.

I have a class popWindow that creates a Toplevel window when asking user for what to do with a possible double.

我的问题是,一旦提交选择,就关闭 popWindow .

My problem is closing the popWindow once a choice is submited.

我实际运行并发布一个aswer(带有重复标记的文本)的版本使用quit来终止窗口(意味着弹出窗口仍然存在)或简单地具有多个弹出窗口,直到完成为止./p>

the version I have that actualy runs and posts an aswer (the text with marked duplicates) uses quit to terminate the window (meaning the popup is still there in the way) or to simply have multiple popups till you are done.

class Duplicates:

    def markWord(self):
        self.appendMarkedWord(self.word)
        self.checked.append(self.word)
        self.pop.topLevel_exit()
        return ""

class popUpWindow:

    temp = Button( self, font    = 8,
                         text    = "Allowed this run only",
                         command = app.newFile.markWord
                         )
    temp.place( x = 178,
                y = 55
                )

如果我改用 .destroy(),则窗口关闭,但程序停止运行,这更糟.

if I instead use .destroy() the window shuts but the program stops running and that is worse.

如何解决此问题,以便它关闭窗口,但仍继续运行该程序?

好吧,好几个小时后,似乎真正的问题是destroy()并没有停止我的popUpWindow.mainloop(),所以我现在更改了退出代码,首先执行quit(),然后执行destroy().这根本不是我所看到的示例,在我看来顶级mainloop上的destroy()并没有终止它(destroy()在我的root.mainloop上工作正常).

Ok, after many many hours it seemed the real problem was destroy() was not stopping my popUpWindow.mainloop() so I now have altered my exit code to first do quit() and then do destroy(). This is not what i have seen as examples at all and it seems to me that destroy() on toplevel mainloop is not terminating it (destroy() works fine on my root.mainloop).

def topLevel_exit(self):
    self.pop.quit()
    self.pop.destroy()

推荐答案

如果在顶级窗口上调用 destroy(),它不会停止应用程序的运行.如果您的应用程序停止了,那么您的代码中肯定还有您要告诉我们的内容.毫无疑问,摆脱弹出窗口的正确方法是在 Toplevel 的实例上调用 destroy .

If you call destroy() on a toplevel window, it will not stop the application from running. If your application stops, there must be more to your code that what you're telling us. Without question, the right way to get rid of the popup is to call destroy on the instance of the Toplevel.

这篇关于python tkinter Toplevel .destroy()和.quit()不能按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆