Python Forum
[Tkinter] _tkinter.TclError: can't invoke "destroy" command: application has been destroyed - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] _tkinter.TclError: can't invoke "destroy" command: application has been destroyed (/thread-33432.html)



_tkinter.TclError: can't invoke "destroy" command: application has been destroyed - knoxvilles_joker - Apr-25-2021

Still sorting through how to make a splash window close automatically after application starts

I think it has to do with the way I have to format this line:
toplevel_windows.after(3000, toplevel_windows.destroy())
from tkinter import Tk, Toplevel, Button, Label, Text
  
def open_a_toplevel_window () :
    toplevel_window = Toplevel (root)
    toplevel_window.title ('Focusing on Text Entery')
    toplevel_window.geometry ('270x100')
    label = Label(toplevel_window, text = 'Enter Text Now')
    label.pack()
    text_entry = Text (toplevel_window, width = 10, height = 3)
    text_entry.focus_set ()
    text_entry.pack ()
    toplevel_window.attributes ('-topmost', True)
    toplevel_window.mainloop()

def open_b_toplevel_window () :
    toplevel_window = Toplevel (root)
    toplevel_window.title ('Focusing on Text Entery')
    toplevel_window.geometry ('270x100')
    label = Label(toplevel_window, text = 'Enter Text Now')
    label.pack()
    text_entry = Text (toplevel_window, width = 10, height = 3)
    text_entry.focus_set ()
    text_entry.pack ()
    toplevel_window.attributes ('-topmost', True)
    toplevel_window.mainloop()

def open_c_toplevel_window () :
    toplevel_window = Toplevel (root)
    toplevel_window.title ('Focusing on Text Entery')
    toplevel_window.geometry ('270x100')
    label = Label(toplevel_window, text = 'Enter Text Now')
    label.pack()
    text_entry = Text (toplevel_window, width = 10, height = 3)
    text_entry.focus_set ()
    text_entry.pack ()
    toplevel_window.attributes ('-topmost', True)
    toplevel_window.mainloop()

def open_d_toplevel_window () :
    toplevel_windows = Toplevel (root)
    toplevel_windows.title ('Inititaling')
    toplevel_windows.geometry ('512x256')
    toplevel_windows.configure(background='black')
 #   toplevel_windows.overrideredirect(True)
    toplevel_windows.attributes ('-topmost', True)
    toplevel_windows.mainloop()
    toplevel_windows.after(3000, toplevel_windows.destroy())
 
root = Tk ()

root.title ("Root Window")  
root.geometry ("512x256")  

label1 = Label (root, text = "This is the Root Window")
button = Button (root, text = "Open Toplevel Window")
button.config (command = open_a_toplevel_window)
button1 = Button (root, text = "Open Toplevel Windowb")
button1.config (command = open_b_toplevel_window)
button2 = Button (root, text = "Open Toplevel Windowc")
button2.config (command = open_c_toplevel_window)
label1.pack ()
button2.place (x = 50, y = 75)
button1.place (x = 210, y = 100)
button.place (x = 110, y = 50)
open_d_toplevel_window ()
root.mainloop ()
I get this error after closing the windows:
Error:
H:\>python toplevel1.py Traceback (most recent call last): File "H:\toplevel1.py", line 71, in <module> File "H:\toplevel1.py", line 47, in open_d_toplevel_window toplevel_windows.after(3000, toplevel_windows.destroy()) File "C:\Users\vosg\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2580, in destroy self.tk.call('destroy', self._w) _tkinter.TclError: can't invoke "destroy" command: application has been destroyed

and it appears that I can not port this into a class and method versus the functions in the example you gave.


RE: _tkinter.TclError: can't invoke "destroy" command: application has been destroyed - Yoriz - Apr-25-2021

In the line
toplevel_windows.after(3000, toplevel_windows.destroy())
you have called the destroy method by using () so it happens imediatly, the result of the method is what is then called by after
remove the () so after makes the call itself.
toplevel_windows.after(3000, toplevel_windows.destroy)
P.S. I don't know why you have mainloop calls everywhere you should only have the one mainloop call, the mainloop call before the after call is probably blocking so that when the window is destroyed and you call destroy the window is already destroyed.


RE: _tkinter.TclError: can't invoke "destroy" command: application has been destroyed - knoxvilles_joker - Apr-25-2021

(Apr-25-2021, 08:53 AM)Yoriz Wrote: In the line
toplevel_windows.after(3000, toplevel_windows.destroy())
you have called the destroy method by using () so it happens imediatly, the result of the method is what is then called by after
remove the () so after makes the call itself.
toplevel_windows.after(3000, toplevel_windows.destroy)
P.S. I don't know why you have mainloop calls everywhere you should only have the one mainloop call, the mainloop call before the after call is probably blocking so that when the window is destroyed and you call destroy the window is already destroyed.

It was a solution to make sure every spawned window was a top window.


RE: _tkinter.TclError: can't invoke "destroy" command: application has been destroyed - deanhystad - Apr-25-2021

It is likely a source of promlems instead of a solution. Tkinter is supposed to have only one mainloop.


RE: _tkinter.TclError: can't invoke "destroy" command: application has been destroyed - knoxvilles_joker - Apr-25-2021

To deanhystad, I am using a provided solution by another member as a framework. the only thing I did was add a couple of windows so I have the number of windows to match what I am trying to do. The way the script I wanto to behave is to always have a window on top so only keyboard input can be used. I am trying to replicate a pre GUI type setup from circa 1970s. I should have prefaced that, so apologies on the lack of clarity.

To yoriz, normally I would reply to this with a direct message but that option is not available to me, is the intention to keep threads as singular as possible for solutions ability, or is it to demuddle things when folks are searching for things? The forum policies are not clearly delineated in the manner and in most help forums they typically want things self contained so experience bias is apparently creating behaviors counter to what you desire here. But this is also the first real educational forum I have had any amount of interactions on. I just have to be notified on how you want behaviors to occur so I can comply, but things were not clearly defined in the forum rules outside of the usual golden rule list of things, and the plagiarism/cheating stuff. Not sure if that is an ongoing discussion regarding posting guidelines and explicitly saying if your answer created another issue, create another thread type rule, or if that rule is buried in with the others as it did not stick out for me. Having written many, many, knowledge base articles myself, sometimes you miss things trying to cover all bases and it takes occurrences to hash out where tweaks need to occur. To say it another way it is hard to follow the rules when not all the rules are written as it is assumed they are implicitly understood or you are in a kangaroo court where the rules are what whatever anyone decides at any point in time which I sincerely doubt are the case here.

As far as the code I can get the window to destroy via a button as exampled here:
from tkinter import Tk, Toplevel, Button, Label, Text
import tkinter as tk
import tkinter.ttk as ttk
import datetime as dt
import time
import os
from tkinter import messagebox
from collections import deque
from itertools import islice
from threading import Thread
from playsound import playsound

  
def open_a_toplevel_window () :
    toplevel_window = Toplevel (root)
    toplevel_window.title ('Focusing on Text Entery')
    toplevel_window.geometry ('270x100')
    label = Label(toplevel_window, text = 'Enter Text Now')
    label.pack()
    text_entry = Text (toplevel_window, width = 10, height = 3)
    text_entry.focus_set ()
    text_entry.pack ()
    toplevel_window.attributes ('-topmost', True)
 #   toplevel_window.mainloop()

def open_b_toplevel_window () :
    toplevel_window = Toplevel (root)
    toplevel_window.title ('Focusing on Text Entery')
    toplevel_window.geometry ('270x100')
    label = Label(toplevel_window, text = 'Enter Text Now')
    label.pack()
    text_entry = Text (toplevel_window, width = 10, height = 3)
    text_entry.focus_set ()
    text_entry.pack ()
    toplevel_window.attributes ('-topmost', True)
 #   toplevel_window.mainloop()

def open_c_toplevel_window () :
    toplevel_window = Toplevel (root)
    toplevel_window.title ('Focusing on Text Entery')
    toplevel_window.geometry ('270x100')
    label = Label(toplevel_window, text = 'Enter Text Now')
    label.pack()
    text_entry = Text (toplevel_window, width = 10, height = 3)
    text_entry.focus_set ()
    text_entry.pack ()
    toplevel_window.attributes ('-topmost', True)
 #   toplevel_window.mainloop()

def open_d_toplevel_window () :
    toplevel_windows = Toplevel (root)
    toplevel_windows.title ('Inititaling')
    toplevel_windows.geometry ('512x256')
    toplevel_windows.configure(background='black')
 #   toplevel_windows.overrideredirect(True)
    text_entry = Button (toplevel_windows, width = 10, height = 3, command=toplevel_windows.destroy)
    text_entry.focus_set ()
    text_entry.pack ()
    toplevel_windows.attributes ('-topmost', True)
    toplevel_windows.mainloop()

 
root = tk.Tk ()

root.title ("Root Window")  
root.geometry ("512x256")  

label1 = Label (root, text = "This is the Root Window")
button = Button (root, text = "Open Toplevel Window")
button.config (command = open_a_toplevel_window)
button1 = Button (root, text = "Open Toplevel Windowb")
button1.config (command = open_b_toplevel_window)
button2 = Button (root, text = "Open Toplevel Windowc")
button2.config (command = open_c_toplevel_window)
label1.pack ()
button2.place (x = 50, y = 75)
button1.place (x = 210, y = 100)
button.place (x = 110, y = 50)
open_d_toplevel_window ()

root.mainloop ()
But the goal is to have a window popup and then disappear to the main window.


RE: _tkinter.TclError: can't invoke "destroy" command: application has been destroyed - knoxvilles_joker - Apr-25-2021

This is the button that was added to the desired popup window that closes it properly. Why that works, but calling a root.after 3 seconds to destroy it is beyond me.


text_entry = Button (toplevel_windows, width = 10, height = 3, command=toplevel_windows.destroy)



RE: _tkinter.TclError: can't invoke "destroy" command: application has been destroyed - knoxvilles_joker - Apr-25-2021

OK, I added root.focus_set() after defining the root window and the popup window after closing allows focus to remain on the main window and each child window.


root = tk.Tk ()

root.title ("Root Window")  
root.geometry ("512x256")  
root.focus_set()
label1 = Label (root, text = "This is the Root Window")
button = Button (root, text = "Open Toplevel Window")
button.config (command = open_a_toplevel_window)
button1 = Button (root, text = "Open Toplevel Windowb")
button1.config (command = open_b_toplevel_window)
button2 = Button (root, text = "Open Toplevel Windowc")
button2.config (command = open_c_toplevel_window)
label1.pack ()
button2.place (x = 50, y = 75)
button1.place (x = 210, y = 100)
button.place (x = 110, y = 50)
open_d_toplevel_window ()

root.mainloop ()