Cannot invoke button command: application has been destroyed - python

Given below is the code for creating independent windows using Tkinter and Python:
import Tkinter
Tkinter.NoDefaultRoot()
win1=Tkinter.Tk()
win2=Tkinter.Tk()
Tkinter.Button(win1, text='Woho!',command=win1.destroy()).pack()
Tkinter.Button(win2, text='Woho!',command=win2.destroy()).pack()
win1.mainloop()
On execution it displays:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\eclipse\Python Projects\Project 1\Source1\mod.py", line 8, in <module>
Tkinter.Button(win1, text='Woho!',command=win1.destroy()).pack()
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2106, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2036, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: can't invoke "button" command: application has been destroyed
I am new to Python and hence so not understand what it means. Where am I going wrong?

Remove () from win1.destroy() and win2.destroy().
Tkinter.Button(win1, text='Woho!',command=win1.destroy()).pack()
Tkinter.Button(win2, text='Woho!',command=win2.destroy()).pack()
^^
It cause win1.destroy method call, and use the return value of the method as callback, instead of the method itself.; cause the main window destroy before Button creation.
Tkinter.Button(win1, text='Woho!',command=win1.destroy).pack()
Tkinter.Button(win2, text='Woho!',command=win2.destroy).pack()

Related

weird error message in tkinter when UFD closes a window that's in use

I have a code that constantly updates a clock and it's like this:
class countdown:
#initiatiate stuff
def count(self):
# countdown
# update textbox with the correct time
self.countdown_complete = customtkinter.CTkButton(self.countdownWindow,width=100, height =30, text="Complete", font=("Verdana",18), command = lambda: self.save_info(self.timer, i),fg_color=self.lightbutton, text_color=self.lightfont, hover_color=self.lightbutton)
self.countdown_complete.place(relx=0.5, rely=0.7, anchor=CENTER)
# I have a save function which saves some value as well as delete the window
# when number reaches 00:00:00, count up and update label to say its overdue
the problem is that after the complete button is pressed and the udf linked to the button deletes the window, the rest of the code continues to run.
i have a dict with elements inside, so if there is 1 element and when the only element is deleted, the lines referencing the array's crashes because it is now empty.
I need a way to stop count() from continuing after the complete button is pressed and window is deleted.
at least that's what I think the error is, here is the error message:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 1921, in __call__
return self.func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 839, in callit
func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/customtkinter/windows/widgets/scaling/scaling_tracker.py", line 178, in check_dpi_scaling
if window.winfo_exists() and not window.state() == "iconic":
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 1112, in winfo_exists
self.tk.call('winfo', 'exists', self._w))
_tkinter.TclError: can't invoke "winfo" command: application has been destroyed
invalid command name "4649351040_click_animation"
while executing
"4649351040_click_animation"
("after" script)
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 1921, in __call__
return self.func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 839, in callit
func(*args)
File "/Users/tanhongen/Documents/GitHub/computing-coursework_S4-01/main.py", line 700, in countdown
self.countdownWindow.after(1000, self.countdown_time.configure(text=self.timer)) # updating the timer
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/customtkinter/windows/widgets/ctk_label.py", line 189, in configure
self._label.configure(text=self._text)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 1675, in configure
return self._configure('configure', cnf, kw)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 1665, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!ctklabel2.!label"

tkinter returns "no such file or directory" error

This is my code:
from tkinter import *
logoImage = PhotoImage(file='logo.png')
logoLabel = Label(root, image=logoImage, bg='dodgerblue3')
logoLabel.grid(row=0, column=0)
The image is in the same directory as the project. In fact, when I open it with cmd or Python it works. But when I use VS Code or Turn it into .exe file, it shows this error:
Traceback (most recent call last):
File "c:\Users\Simo\OneDrive\Python\Scientific calculator\calc.py", line 180, in <module>
logoImage = PhotoImage(file='logo.png')
File "C:\Users\Simo\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4093, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\Simo\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4038, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "logo.png": no such file or directory
How can I fix this?
Maybe try putting the file into your workspace and using the relative path.

tkinter tk wiget turtle python

i am trying to do a turtle game with the tkinter.Tk() class.
Here is the code:
from tkinter import *
import turtle
main = Tk(className="Castle Game")
s=turtle.TurtleScreen(cv=main,mode='standard', colormode=1.0, delay=10)
cover=turtle.RawTurtle(s)
It tries to return the following:
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "H:\2 Computer science\Year 9\4Python\Castle.py", line 7, in <module>
s=turtle.TurtleScreen(cv=main,mode='standard', colormode=1.0, delay=10)
File "C:\Python33\lib\turtle.py", line 989, in __init__
TurtleScreenBase.__init__(self, cv)
File "C:\Python33\lib\turtle.py", line 488, in __init__
self.cv.config(scrollregion = (-w//2, -h//2, w//2, h//2 ))
File "C:\Python33\lib\tkinter\__init__.py", line 1263, in configure
return self._configure('configure', cnf, kw)
File "C:\Python33\lib\tkinter\__init__.py", line 1254, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-scrollregion"
>>>
In turtle graphics, the cv option stands for canvas, but you're giving it a Tk. That's why there's an error. To fix that, you will need to do this:
from tkinter import *
import turtle
root=Tk(className="Castle Game")
main = Canvas(root)
main.pack()
s=turtle.TurtleScreen(cv=main,mode='standard', colormode=1.0, delay=10)
cover=turtle.RawTurtle(s)
By changing main to Canvas and pack it onto a root.

pyimage2 does not exist error when I try to insert an image to window

I am trying to insert an image to window but I get an error.
Traceback (most recent call last):
File "C:\Users\Afro\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:\Users\Afro\Desktop\h.py", line 151, in start
pic_lab = Label(wind,image=pik)
File "C:\Users\Afro\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2605, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\Afro\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2138, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage2" doesn't exist
Here's my code
wind = Tk()
name = money.jpg'
pik = ImageTk.PhotoImage(Image.open(name))
pic_lab = Label(wind,image=pik)
pic_lab.grid()
What do I need to do?
The string 'money.jpg' that is assigned to the variable name is missing a single quotation mark on the left.

TclError: can't invoke "canvas" command: application has been destroyed

I'm trying to run some code from the console, but am getting the TclError. Below is the entire traceback:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
return self.func(*args)
File "SM_analyser.py", line 446, in OnB_maxq
self.canvas = FigureCanvasTkAgg(self.plotter, self)
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 225, in __init__
master=master, width=w, height=h, borderwidth=4)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2195, in __init__
Widget.__init__(self, master, 'canvas', cnf, kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2055, in __init__
(widgetName, self._w) + extra + self._options(cnf))
TclError: can't invoke "canvas" command: application has been destroyed
I'm reasonably sure the the following block of code is responsible. It should be adding the plot 'figure' to a tkinter canvas, but when I run it 'figure' is plotted in a separate window and the Tcl error is given.
self.plotter = plt.figure('figure')
plt.contour(array, linewidths = 1, colors = 'k')
plt.contourf(array, cmap = plt.cm.jet)
plt.ylabel('Y', fontdict = {'fontsize':16})
plt.xlabel('A', fontdict = {'fontsize':16})
plt.colorbar()
plt.title('figure', fontdict = {'fontsize':20})
plt.show()
self.canvas = FigureCanvasTkAgg(self.plotter, self)
self.canvas.get_tk_widget().grid(column=14,row=2,rowspan=34)
plt.close()
self.canvas._tkcanvas.config(highlightthickness=0)
TclError: can't invoke "canvas" command: application has been destroyed means that you are trying to create an instance of a Canvas class, but that the main window of the application no longer exists. You might want to step through the logic of your program to see if you're destroying the root window at some point prior to creating the canvas.

Categories