简体   繁体   中英

Destroy tkinter window from secondary process

I have a program that, at one point, pops up a tkinter window. Once you close the window, the program continues. This is ok, but I need the program to close the window after 2 minutes if the user hasn't done so.

Since I use Pyinstaller, I understand that I need to use multiprocess instead of threading. Either way, when I call root.destroy() from a process, it fails because it can't find it ("name 'root' is not defined"; command line says this, IDLE says nothing).

I've spent hours researching how to implement this ""simple"" feature. I just want a quick fix. This wasn't even my original issue, originally the secondary process would bypass or "enter a value" into an "input()" in the main process so that it would continue, but I don't know how to do that either. If that's easier to solve, I'm up for it.

I've tried many things but I guess I'll just paste where I'm at:

from multiprocessing import Process
import time
import tkinter as tk

def func2():
    global root
    time.sleep(3)
    root.destroy()

if __name__ == '__main__':
    global root
    root = tk.Tk()
    T = tk.Text(root, height=20, width=60)
    T.pack()
    T.insert(tk.END, "test")

    p2 = Process(target=func2)
    p2.start()

    tk.mainloop()

Thanks a lot,

You can use tkinter after mechanism to register something todo after a while. See: tkinter: how to use after method

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