rix
rix

Reputation: 93

tkinter window not closing properly

My tkinter app windows are not closing properly. I am using python 3.6.6 with tkinter 8.6

My code does basically this:

Open a process, where:

  1. A test function is called via a thread that closes Gui window after 3s
  2. Gui window is created
  3. Wait for thread to complete (join) and guess window was closed

I tried to use:

I stripped it down to the following code, please copy & execute and/or tell me whats wrong...

from time import sleep, time
import threading
from multiprocessing import Process, set_start_method
from tkinter import *
CtrlApplObj = None

def Start():
    global CtrlApplObj
    CtrlApplObj = None
    CtrlApplObj = ControlApplication()
    CtrlApplObj.run()

def End():
    print("Quit now...")
    #CtrlApplObj.root.destroy()
    CtrlApplObj.root.quit()

class ControlApplication():
    def __init__(self):
        pass

    def run(self):
        self.root=Tk()
        print("Mainloop...")
        self.root.mainloop()

def test():
    sleep(3)
    End()

def execute():
    T1 = threading.Thread(target=test)
    T1.start()
    Start()
    T1.join()

if __name__ == "__main__":
    set_start_method("spawn")
    for i in range(2):
        TestProcess = Process(target=execute)
        TestProcess.start()
        TestProcess.join()

Upvotes: 1

Views: 364

Answers (1)

rix
rix

Reputation: 93

My final solution was not using any tkinter operations in test thread. Then destroy worked.

I had another problem with my test process not closing. This was because a queue was not empty. That porevented process to close.

Upvotes: 1

Related Questions