简体   繁体   中英

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

I am starting to make something (IDK yet) using python and pygubu. To start of I have been exploring what you can do with pygubu. I have met an issue however that I have no idea how to solve. My problem is that I cannot destroy a 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 . 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. I am new to pygubu and need all the help I can get. How do you destroy a window using pygubu? NOTE: The error appears after closing the 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)?

Thanks to the comment by @stovfl .

Add, before 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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