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
Related
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.
Please am creating an app view and in python and trying to show some images but am getting error. bellow is the code
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title("Developing Software")
root.iconbitmap("icon.ico")
image = Image.open("tree_50b.JPG").convert("RGB")
my_img = ImageTk.PhotoImage(image)
my_label = Label(my_img)
my_label.pack()
button_quit = Button(root, text='Exit Program', fg='red', command=root.quit)
button_quit.pack()
root.mainloop()
This is the error message
Traceback (most recent call last):
File "<ipython-input-1-c9a970a4c796>", line 16, in <module>
my_label = Label(my_img)
File "C:\anconda\lib\tkinter\__init__.py", line 2766, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\anconda\lib\tkinter\__init__.py", line 2292, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\anconda\lib\tkinter\__init__.py", line 2269, in _setup
if master._last_child_ids is None:
AttributeError: 'PhotoImage' object has no attribute '_last_child_ids'
I need help please
The first argument to Label(...) needs to be a widget, not an image, and the image needs to be a keyword argument.
Label(root, image=my_img)
I´m new to python and have problems with tkinker and the Images.
My error is:
Traceback (most recent call last):
File "D:/python/First Project/Weather app.py", line 12, in <module>
background_image = tk.PhotoImage(file='landscape.jpg')
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3542, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3498, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "landscape.jpg"
and the associated code is:
import tkinter as tk
root = tk.Tk()
background_image = tk.PhotoImage(file='D:\python\First Project\landscape.jpg')
background_label = tk.Label(root, image=background_image)
background_label.place(relwidth=1, relheight=1)
root.mainloop()
The ending of the file isn´t renamed and originally .jpg.
I also tried to edit it with
background_image = ImageTk.PhotoImage(Image.open('your.png'))
The PhotoImage method does not appear to support JPG files. I received the same error (couldn't recognise data in "image.jpg") when using a JPG file. When using a PNG file I did not receive any such errors.
Note that you cannot just change the file extension of a JPG file to turn it into a PNG file, because the data in PNG files is different from a JPG file. You will need to convert your JPG file to a PNG (or any other image file type supported by the PhotoImage method.)
I've been learning how to use Tkinter from scratch and while I try to set a simple Label widget in a frame:
from Tkinter import *
from ttk import *
root = Tk()
root.title("Practice")
mainW = LabelFrame(root, text = "Main info")
mainW.grid()
image = Label(mainW, image = "C:\Users\Oscar Ramirez\Pictures\image.png")
image.grid(column = 0, row = 0)
codeEntry = Entry(mainW, text = "User Code")
codeEntry.grid(column = 1, row = 0)
root.mainloop()
I'm getting the following error:
Traceback (most recent call last):
File "Tutorial.py", line 10, in <module>
image = Label(mainW, image = "C:\Users\Oscar Ramirez\Pictures\image.png")
File "C:\Python27\lib\lib-tk\ttk.py", line 757, in __init__
Widget.__init__(self, master, "ttk::label", kw)
File "C:\Python27\lib\lib-tk\ttk.py", line 555, in __init__
Tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2096, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image specification must contain an odd number of elements
I've checked the image format, the route, etc. And now I don't really know what can be causing me trouble.
image
The image to display in the widget. The value should be a
PhotoImage, BitmapImage, or a compatible object. If specified, this
takes precedence over the text and bitmap options. (image/Image)
Right now you are just passing a string for image option of label. You need something like,
photo = PhotoImage(file="image.gif")
label = Label(..., image=photo)
label.photo = photo #reference keeping is important when working with images
Right now, since you are using PNG image, you need to install and use Python Imaging Library (PIL) though. For more info, you can read Photo Image section from effbot.
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.