如何在 app.run() 之后销毁 pygubu window?

How to destroy a pygubu window after app.run()?

我开始使用 python 和 pygubu 制作一些东西(还没有 IDK)。首先,我一直在探索使用 pygubu 可以做什么。我遇到了一个问题,但是我不知道如何解决。我的问题是我无法销毁 window。我试过:

import time
from time import sleep
import tkinter as tk
import pygubu

def func():
    time.sleep(2.8)
    app.destroy()


class HelloWorldApp:
    global app

    def __init__(self):

        #1: Create a builder
        self.builder = builder = pygubu.Builder()

        #2: Load an ui file
        builder.add_from_file('IDKyet.ui')

        #3: Create the mainwindow
        self.mainwindow = builder.get_object('window')

    def run(self):
        self.mainwindow.mainloop()


if __name__ == '__main__':
    app = HelloWorldApp()
    app.run()
    func()

但我得到错误:

Traceback (most recent call last):
  File "C:\Users\tom\Documents\python\In development\I Do Not Know (yet).py", line 42, in <module>
    func()
  File "C:\Users\tom\Documents\python\In development\I Do Not Know (yet).py", line 11, in func
    app.destroy()
AttributeError: 'HelloWorldApp' object has no attribute 'destroy'

然后我尝试了:

import time
from time import sleep
import tkinter as tk
import pygubu

def func():
    time.sleep(2.8)
    window.destroy()

class HelloWorldApp:
    global window

    def __init__(self):

        #1: Create a builder
        self.builder = builder = pygubu.Builder()

        #2: Load an ui file
        builder.add_from_file('IDKyet.ui')

        #3: Create the mainwindow
        self.mainwindow = builder.get_object('window')

    def run(self):
        self.mainwindow.mainloop()


if __name__ == '__main__':
    app = HelloWorldApp()
    app.run()
    func()

基本上我已经将全局变量更改为被调用的 window,它是我的父 window,并且我将被销毁的 window 称为 window。但是我得到错误:

Traceback (most recent call last):
  File "C:\Users\tom\Documents\python\In development\I Do Not Know (yet).py", line 42, in <module>
    func()
  File "C:\Users\tom\Documents\python\In development\I Do Not Know (yet).py", line 11, in func
    window.destroy()
NameError: name 'window' is not defined

尽管我已将 window 设为全局变量。我是 pygubu 的新手,需要我能得到的所有帮助。 如何使用 pygubu 销毁 window? 注意: 关闭 [=44= 后出现错误]. 我将不胜感激任何帮助。 I tried this link but it doesn't help.

编辑:

现在我知道在 app.run() 之后 window 停止 working/closes 但是你如何让它消失(就像 tkinter destroy() 函数)?

感谢 @stovfl 的评论。

Add, before app.run(), app.after(2800, window.destroy)

这意味着我可以编辑我的代码并且它会起作用:

import time
from time import sleep
import tkinter as tk
import pygubu



class HelloWorldApp:
    global window

    def __init__(self):

        #1: Create a builder
        self.builder = builder = pygubu.Builder()

        #2: Load an ui file
        builder.add_from_file('IDKyet.ui')

        #3: Create the mainwindow
        self.mainwindow = builder.get_object('window')

    def run(self):
        self.mainwindow.mainloop()


if __name__ == '__main__':
    app = HelloWorldApp()
    app.after(2800, window.destroy)
    app.run()