Python Forum
[Tkinter] _tkinter.TclError: can't invoke "destroy" command: application has been destroyed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] _tkinter.TclError: can't invoke "destroy" command: application has been destroyed
#1
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.
Yoriz write Apr-25-2021, 08:48 AM:
Please create new threads for new issues
Reply
#2
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.
Reply
#3
(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.
Reply
#4
It is likely a source of promlems instead of a solution. Tkinter is supposed to have only one mainloop.
Reply
#5
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.
Yoriz write Apr-26-2021, 04:50 PM:
Please see a new thread I posted on your behalf
https://python-forum.io/Thread-Forum-rul...wn-threads
Reply
#6
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)
Reply
#7
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 ()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  .get() invoke after a button nested press iddon5 5 1,219 Mar-29-2021, 03:55 AM
Last Post: deanhystad
  Menu destroy Heyjoe 5 1,412 Mar-02-2021, 01:45 AM
Last Post: Heyjoe
  tkinter destroy label inside labelFrame Nick_tkinter 2 1,773 Feb-24-2021, 03:31 AM
Last Post: deanhystad
  "tkinter.TclError: NULL main window" Rama02 1 2,111 Feb-04-2021, 06:45 PM
Last Post: deanhystad
  [Tkinter] _tkinter.TclError: bitmap "Icon.gif" not defined djwilson0495 2 5,601 Aug-05-2020, 02:27 PM
Last Post: wuf
  [Tkinter] _tkinter.TclError: image "pyimage2" doesn't exist Killdoz 1 4,993 May-30-2020, 09:48 AM
Last Post: menator01
  tkinter.TclError: bad window path name kenwatts275 3 6,303 Apr-26-2020, 08:16 PM
Last Post: kenwatts275
  AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' edphilpot 5 4,679 Dec-20-2019, 09:52 PM
Last Post: joe_momma
  clear button destroy can't work jacklee26 1 2,832 Jul-07-2019, 03:44 AM
Last Post: DeaD_EyE
  "ModuleNotFoundError: No module named '_tkinter' in Python 3.8 Alfa 0 programmerc 1 4,702 Oct-21-2018, 06:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020