I am trying to build my first gut using tkinter (still super new to python). I have no tried different ways to insert an image but none of them worked. I think I don't even get ImageTk imported... The error message in this case is "TclError: cannot use geometry manager pack inside . which already has slaves managed by grid". Can someone help me do it properly? The image file is in the same directory as the .py file
import tkinter as tk
from tkinter import *
from PIL import ImageTk,Image
root = tk.Tk()
photo = PhotoImage(file = "6.gif")
tk.Label(root, image = photo).pack()
root.mainloop()
traceback:
runfile('/Users/maj-brittbuchholz/Desktop/image practice tkinter.py', wdir='/Users/maj-brittbuchholz/Desktop')
Traceback (most recent call last):
File "", line 1, in
runfile('/Users/maj-brittbuchholz/Desktop/image practice tkinter.py', wdir='/Users/maj-brittbuchholz/Desktop')
File "/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/Users/maj-brittbuchholz/Desktop/image practice tkinter.py", line 13, in
tk.Label(root, image = photo).pack()
File "/opt/anaconda3/lib/python3.7/tkinter/init.py", line 2766, in init
Widget.init(self, master, 'label', cnf, kw)
File "/opt/anaconda3/lib/python3.7/tkinter/init.py", line 2299, in init
(widgetName, self._w) + extra + self._options(cnf))
TclError: image "pyimage22" doesn't exist
There are several things wrong:
The tk.label has a master called win. Either you are not showing us the entire code, or it is undefined. If it is defined somewhere else, then this is fine.
The error says you can't pack it because something is using grid. This means that there is something else that has grided slaves.
Other than that, your program is fine. You may be wrongly assumong that this is where the error is coming from, or maybe not.
Related
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.
I am attempting to make a tkinter project but as I tried to run for like the third time it gives me this error:
Traceback (most recent call last):
File "/Users/cool/Documents/STM Wisepay Service.py", line 63, in <module>
app = App(root)
File "/Users/cool/Documents/STM Wisepay Service.py", line 20, in __init__
self.create_buttons()
File "/Users/cool/Documents/STM Wisepay Service.py", line 30, in create_buttons
tk.Button(button_frame, text = "Add to Debt", commmand = self.debt).grid(column = 6, row = 5)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 2366, in __init__
Widget.__init__(self, master, 'button', cnf, kw
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 2296, in __init__
(widgetName, self._w) + extra + self._options(cnf)
_tkinter.TclError: unknown option "-commmand"
What can I do to fix this? What are the problems with my code?
Here is my code:
https://pastebin.com/mWPEFbpz
You are spelling command wrong (three m's)
You are passing it a variable called commmand and it doesn't know how to use that variable.
tk.Button(... commmand = ...
You can see it in the error message right here
_tkinter.TclError: unknown option "-commmand"
These python error messages are actually very helpful. I would encourage you to read them carefully to catch stuff like this.
I am creating artificial intelligence to play Chinese checkers with Python, but I can't even get an image of a board to show up!
I am using this code:
from tkinter import *
root = Tk()
board = PhotoImage(file="board.ppm")
root.mainloop()
I get the following error:
Traceback (most recent call last):
File "/Users/GAMEKNIGHT7/Desktop/genius hour/chineseCheckersAI(genius hour).py", line 3, in <module>
board = PhotoImage(file="board.ppm")
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 3539, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 3495, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "board.ppm"
I placed the code file in the same file as the image. What happened? How can I fix it?
Try running the following in Terminal:
file board.ppm
If it says anything with the word ASCII in it, that means your image is uncompressed ASCII (NetPBM type=3) and Tkinter will not like it:
board.ppm: Netpbm image data, size = 10 x 10, pixmap, ASCII text
if your file is correct, it will report rawbits (NetPBM type=6):
board.ppm: Netpbm image data, size = 10 x 10, rawbits, pixmap
If you want to convert from ASCII/P3 to binary/rawbits/P6, you could install ImageMagick with homebrew like this:
brew install imagemagick
Then convert like this:
convert board.ppm -compress lossless board.ppm
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
I have a label object that needs to display a variety of images. Based on my research, the following script (below) should successfully update the image displayed in my label object. However, when I call self.ImageViewer.configure(image=self.imgToDisp), I get a TypeError (seen below).
I'm not entirely sure what the error means, but it looks like .configure is expecting a string? Nobody else seems to encounter this problem so I must be doing something syntactically wrong with the script below. Any input is appreciated.
Script:
def getImgs(self):
folderPath = filedialog.askdirectory()
self.imgArray, self.imagePaths = batchImpt.importAllImgs(folderPath)
halThrThsnd.saveAllData(self.imgArray)
im = Image.open(self.imagePaths[0])
self.imgToDisp = PhotoImage(im)
self.imageViewer.configure(image = self.imgToDisp)
self.imageViewer.image = self.imgToDisp
Error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "//mspm1bnas50s/home58/biegad1/Python Scripts/GUI_0_1.py", line 40, in getImgs
self.imageViewer.configure(image = self.imgToDisp)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\tkinter\__init__.py", line 1330, in configure
return self._configure('configure', cnf, kw)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\tkinter\__init__.py", line 1321, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
TypeError: __str__ returned non-string (type TiffImageFile)
You need to change self.imgToDisp = PhotoImage(im) to self.imgToDisp = ImageTk.PhotoImage(im)
Of course, you must add ImageTk to your import statements. I think you already did: from PIL import Image. If So, modify it to: from PIL import Image, ImageTk.