I want to display an icon on a menu bar, so using this information, I coded this:
img = Image.open("help.png")
menubar.add_cascade(label="Help",menu=helpmenu,bitmap=ImageTk.PhotoImage(img))
I got this error:
Traceback (most recent call last):
File "mine.py", line 67, in <module>
m.menus(root)
File "mine.py", line 55, in menus
menubar.add_cascade(label="Help",menu=helpmenu,bitmap=ImageTk.PhotoImage(img))
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2699, in add_cascade
self.add('cascade', cnf or kw)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2696, in add
self._options(cnf, kw))
_tkinter.TclError: bitmap "pyimage2" not defined
How to fix this ?
To display a PhotoImage you should use the image attribute, not bitmap.
You can also simply open an image file directly using ImageTk.PhotoImage(file='...')
So you can use the following code to display your image in the menu:
img = ImageTk.PhotoImage(file="help.png")
menubar.add_cascade(label="Help", menu=helpmenu, image=img)
Related
Code:
import turtle as trtl
wn = trtl.Screen()
wn.setup(width = 1.0, height = 1.0)
wn.bgpic("Underseav2_BG.png")
wn.mainloop()
Error Message:
Traceback (most recent call last):
File "c:\Users\Natha\Desktop\ATCS Create Task\main_game.py", line 7, in <module>
wn.bgpic("Underseav2_BG.png")
File "C:\Users\Natha\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 1482, in bgpic
self._bgpics[picname] = self._image(picname)
File "C:\Users\Natha\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 478, in _image return TK.PhotoImage(file=filename, master=self.cv) File "C:\Users\Natha\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4093, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\Natha\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 "Underseav2_BG.png": no such file or directory
This is the Code I wrote and the error message I got. I have the image file in the same folder (shown in this image here:[Code&Folder]
as my code, yet Python says they are not in the same directory. What has happened here, and is there anything I can do to fix it?
Thanks!
If you download the image from the Internet, make sure that its original format is png.
If you download the image as a jpg and modify it to png, I think this will not work.
https://pixabay.com/illustrations/optical-flare-png-5397752/
I downloaded an image from this site in png format, the code worked successfully.
if you're using Mac, right click -> Quick Actions -> Convert Image -> Choose the Format to "PNG"
from tkinter import *
window = Tk()
window.geometry("500x500")
icon = PhotoImage(file="X.png")
window.iconphoto(True,icon)
window.mainloop()
#my code
#error
Traceback (most recent call last):
File "C:\Users\priya\PycharmProjects\pythonProject2\main.py", line 5, in <module>
icon = PhotoImage(file="X.png")
File "C:\Users\priya\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4093, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\priya\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 recognize data in image file "X.png"
It seems that the file X.png is not a valid PNG image file, try viewing it in a image viewer and check its filetype via https://www.checkfiletype.com/ or using the file command via WSL.
Althogh it works perfectly fine with built in turtle shapes it doesn't work with
new registered shapes.The error is pyimage1 doesn't exist and both my program and file are in the same directories
Here is the code
root=Tk()
import turtle
def image():
global img
img='batman.gif'
player=turtle.Turtle()
wn=turtle.Screen()
wn.register_shape(img)
player.shape(img)
B=Button(root,text='click',command=image).pack()```
The error shown is:```Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\dell\OneDrive\Desktop\imagetk.py", line 10, in image
player.shape(img)
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\turtle.py", line 2777, in shape
self.turtle._setshape(name)
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\turtle.py", line 2506, in _setshape
self._item = screen._createimage(screen._shapes["blank"]._data)
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\turtle.py", line 723, in _createimage
return self.cv.create_image(0, 0, image=image)
File "<string>", line 1, in create_image
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 2489, in create_image
return self._create('image', args, kw)
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 2480, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: image "pyimage1" doesn't exist```
The problem is the way you're mixing turtle and tkinter is creating two roots which leads to this error. You're trying to use standalone turtle when you should be using embedded turtle. I.e. you should be using RawTurtle instead of Turtle and RawScreen instead of Screen. But don't just swap the names, look them up in the documentation. Your code should look roughly like:
from tkinter import *
from turtle import TurtleScreen, RawTurtle
IMAGE = 'batman.gif'
def image():
player = RawTurtle(screen)
player.shape(IMAGE)
root = Tk()
Button(root, text='click', command=image).pack()
canvas = Canvas(root)
canvas.pack()
screen = TurtleScreen(canvas)
screen.register_shape(IMAGE)
screen.mainloop()
I'm trying to write a tkinter program, and I came across an error that I've never seen before while working with tkinter. I've searched around everywhere, and tried everything I can think of. This is my code so far:
x=tk.Canvas(top,width=1000,height=750,bg="grey")
x.pack()
y=tk.PhotoImage(file="C:\\Users\\Admin\\Desktop\\images (3)_CnyokaDvJmG1xu.png")
x.create_image(top,0,0,image=y)`
and this is error
Traceback (most recent call last):
File "C:\Users\Admin\Anaconda3\lib\tkinter\__init__.py", line
1705, in __call__
return self.func(*args)
File "C:/Users/Admin/.spyder-py3/temp.py", line 16, in open
x.create_image(top,0,0,image=y).pack()
File "C:\Users\Admin\Anaconda3\lib\tkinter\__init__.py", line
2489, in create_image
return self._create('image', args, kw)
File "C:\Users\Admin\Anaconda3\lib\tkinter\__init__.py", line
2480, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: bad screen distance "."
You don't have to supply containing widget when placing an image in a canvas, coordinates are enough:
x.create_image(0,0,image=y)
The error is because canvas does not accept a widget as coordinates.
I am trying to show an image on screen with python and tkinter, but when I run it, it gives an error in the PhotoImage object.
This is my code:
from tkinter import *
root = Tk()
photo = PhotoImage(file="devil.png")
label = Label(root, image=photo)
label.pack()
root.mainloop()
The image file is in the same folder as the .py file.
And it gives this error:
Traceback (most recent call last):
File "C:/Users/MyUsername/PycharmProjects/GUI test/home.py", line 5, in <module>
photo = PhotoImage(file="devil.png")
File "C:\Users\MyUsername\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\MyUsername\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3498, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: encountered an unsupported criticial chunk type "iDOT"
Does anyone know how to fix this?
The chunk type of image you're using, 'iDOT', is not a registered PNG chunk. So, you should replace the image with an appropriate one.
This can help you understand what actually error is about