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.
Related
I've been working with the same Python/Tkinter code for a week now, and seemingly out of nowhere I am getting the error "Bad label specification":
Traceback (most recent call last):
File "C:/Users/shapirju/Desktop/NumericalAnalysis-Project2/gui.py", line 1188, in <module>
gui = App()
File "C:/Users/shapirju/Desktop/NumericalAnalysis-Project2/gui.py", line 62, in __init__
self.createWidgets()
File "C:/Users/shapirju/Desktop/NumericalAnalysis-Project2/gui.py", line 70, in createWidgets
self.interpLabelframe = LabelFrame(self.root, text = "Interpolation", labelanchor=N)
File "C:\Program Files\Python27\lib\lib-tk\ttk.py", line 776, in __init__
Widget.__init__(self, master, "ttk::labelframe", kw)
File "C:\Program Files\Python27\lib\lib-tk\ttk.py", line 555, in __init__
Tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "C:\Program Files\Python27\lib\lib-tk\Tkinter.py", line 2096, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: Bad label anchor specification 443664640LN
It is caused by the line self.interpLabelframe = LabelFrame(self.root, text = "Interpolation", labelanchor=N)
As this worked a few hours ago, maybe this is an environmental issue? I cannot find anything about this issue on Google.
Changing it to self.interpLabelframe = LabelFrame(self.root, text = "Interpolation", labelanchor='n') fixes the issue.
According to the source code for LabelFrame, the label anchors are defined as strings 'n', 'we', etc.
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.
I changed from Python 3.4 to Python 2.7, and while debugging my script I found the interpreter unable to read a base64 graphic encoded in a string. I wanted to use the encoded image as a background for a tkinter canvas like so, but I am unable to do so now as I had been with Python 3.4:
background="""
iVBORw0KGgoAAA #....continues
"""
photo = tk.PhotoImage(data=background)
width, height = photo.width(), photo.height()
canvas = tk.Canvas(root, width=width, height=height, bd=-2)
canvas.pack()
canvas.create_image(0, 0, image=photo, anchor="nw")
When I run the script, I get this error:
>Traceback (most recent call last):
File "main.py", line 31, in <module>
photo = tk.PhotoImage(data=background)
File "c:\Python27\lib\lib-tk\Tkinter.py", line 3323, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "c:\Python27\lib\lib-tk\Tkinter.py", line 3279, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize image data
I tried to run this simple program on python 2.5 and get the following error, I am not sure what is going on.
Code:
import pylab
pylab.figure(1) # make figure 1 the current figure
pylab.plot([1,2,3,4], [1,7,3,5]) # draw on figure 1
pylab.show() # show figure on screen
Error:
Traceback (most recent call last):
File "/Users/sebastiancarrasco/Desktop/Courses/6.00.2x/Week1/plotting-seg1.py", line 3, in <module>
pylab.figure(1) # make figure 1 the current figure
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/pyplot.py", line 270, in figure
**kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/backends/backend_tkagg.py", line 85, in new_figure_manager
figManager = FigureManagerTkAgg(canvas, num, window)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/backends/backend_tkagg.py", line 403, in __init__
self.toolbar = NavigationToolbar2TkAgg( canvas, self.window )
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/backends/backend_tkagg.py", line 670, in __init__
NavigationToolbar2.__init__(self, canvas)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/backend_bases.py", line 2273, in __init__
self._init_toolbar()
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/backends/backend_tkagg.py", line 719, in _init_toolbar
command=self.home)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/backends/backend_tkagg.py", line 702, in _Button
im = Tk.PhotoImage(master=self, file=file)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk/Tkinter.py", line 3294, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk/Tkinter.py", line 3250, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: couldn't open "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/mpl-data/images/home.ppm": permission denied
For anyone else who runs into this problem and comes across this page: I eventually found the answer here. Long story short: some problem with the installer messes up the permissions for all the files in the "images" folder. All you have to do is change the permissions to give yourself read privileges.
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()