简体   繁体   English

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

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

I am starting to make something (IDK yet) using python and pygubu.我开始使用 python 和 pygubu 制作一些东西(IDK)。 To start of I have been exploring what you can do with pygubu.首先,我一直在探索你可以用 pygubu 做什么。 I have met an issue however that I have no idea how to solve.我遇到了一个问题,但是我不知道如何解决。 My problem is that I cannot destroy a window.我的问题是我无法销毁 window。 I have tried:我努力了:

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

But I get the error:但我得到了错误:

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'

So then I tried:所以我尝试了:

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

Basically I have changed the global variable to being called window which is my parent window and I have referred to the window being destroyed as window .基本上我已将全局变量更改为称为window ,这是我的父级 window ,我将 window 称为window However I get the error:但是我得到了错误:

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

Even though I have made window a global variable.即使我已将window全局变量。 I am new to pygubu and need all the help I can get.我是 pygubu 的新手,需要我能得到的所有帮助。 How do you destroy a window using pygubu?如何使用 pygubu 销毁 window? NOTE: The error appears after closing the window.注意:关闭 window 后出现错误。 I would appreciate any help.我将不胜感激任何帮助。 I tried this link but it doesn't help .我试过这个链接,但它没有帮助

Edit:编辑:

Now I know that after app.run() the window stops working/closes but how do you make it disappear (like the tkinter destroy() function)?现在我知道在app.run()之后 window 停止工作/关闭,但你如何让它消失(如 tkinter destroy()函数)?

Thanks to the comment by @stovfl .感谢@stovfl的评论。

Add, before app.run() , app.after(2800, window.destroy)app.run()之前添加app.after(2800, window.destroy)

This means that I can edit my code and it will work:这意味着我可以编辑我的代码并且它可以工作:

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

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

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