I"m confused as to why I'm getting this error. I have looked into the file specified in the error as well as done some research on PIL and the actual error. Any help would be appreciated. This code is an example code, it doesn't belong to me. I'm following a tutorial I'm trying to learn a new gui module for python.
Code:
from PIL import Image, ImageTk
from Tkinter import Tk, Label, BOTH
from ttk import Frame, Style
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Picture")
self.pack(fill=BOTH, expand=1)
Style().configure("TFrame", background="#333")
bard = Image.open("test.jpg")
bardejov = ImageTk.PhotoImage(bard)
label1 = Label(self, image=bardejov)
label1.image = bardejov
label1.place(x=20, y=20)
def main():
root = Tk()
root.geometry("300x280+300+300")
app = Example(root)
root.mainloop()
if __name__ == '__main__':
enter code heremain()
Error:
Traceback (most recent call last):
File "C:/Python27/pics.py", line 36, in <module>
main()
File "C:/Python27/pics.py", line 31, in main
app = Example(root)
File "C:/Python27/pics.py", line 12, in __init__
self.initUI()
File "C:/Python27/pics.py", line 22, in initUI
bardejov = ImageTk.PhotoImage(bard)
File "C:\Python27\lib\site-packages\PIL\ImageTk.py", line 116, in __init__
self.paste(image)
File "C:\Python27\lib\site-packages\PIL\ImageTk.py", line 181, in paste
import _imagingtk
ImportError: DLL load failed: %1 is not a valid Win32 application.
"ImportError: DLL load failed: %1 is not a valid Win32 application." is from Windows itself, and means that your PIL or Tkinter install doesn't work on your Windows version.
One potential cause for this is that you're using a version built with VS 2012 on Windows XP; see:
http://blogs.msdn.com/b/vcblog/archive/2012/06/15/10320645.aspx
Related
When I try to add an image and set it as my GUI window logo it gives me these errors
Traceback (most recent call last):
File "C:\Users\Meina Jia\PycharmProjects\guwindow\main.py", line 7, in <module>
icon = PhotoImage(file='logo.jpg')
File "C:\Users\Meina Jia\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4093, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\Meina Jia\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 "logo.jpg"
Process finished with exit code 1
I already changed the file type using code.
from tkinter import *
window = Tk()
window.geometry("420x420")
window.title("Backrooms in A Nutshell")
icon = PhotoImage(file='logo.jpg')
window.iconphoto(True, icon)
window.mainloop()
For this, you will need the Pillow Library.
import tkinter as tk
from PIL import ImageTk, Image
window = tk.Tk()
window.geometry("420x420")
window.title("Backrooms in A Nutshell")
icon = ImageTk.PhotoImage(Image.open('logo.jpg'))
window.iconphoto(True, icon)
window.mainloop()
Note: It is good practice to import a library directly:
Run
import tkinter
instead of
from tkinter import *
My code stopped working quite literally overnight, due to an issue with Tkinter.
I'm using PySimpleGui for my project, which was working the previous day.
When I run my program it showed the bellow error:
File "E:/Projekty Python/dcs_assistant/gui_and_video.py", line 1, in <module>
import PySimpleGUI as sg
File "C:\Users\pawni\Miniconda3\envs\dcs_assistant\lib\site-packages\PySimpleGUI\__init__.py", line 2, in <module>
from .PySimpleGUI import *
File "C:\Users\pawni\Miniconda3\envs\dcs_assistant\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 125, in <module>
tclversion_detailed = tkinter.Tcl().eval('info patchlevel')
File "C:\Users\pawni\Miniconda3\envs\dcs_assistant\lib\tkinter\__init__.py", line 2354, in __getattr__
return getattr(self.tk, attr)
AttributeError: module '_tkinter' has no attribute 'eval'
Process finished with exit code 1
Line 125 is this:
tclversion_detailed = tkinter.Tcl().eval('info patchlevel')
This, as per Tkinter documentation, should simply return the version of Tkinter library (https://tkdocs.com/tutorial/install.html).
When I ran a dummy program using just Tkinter to test if Tkinter alone works. I got another error:
Traceback (most recent call last):
File "E:/Projekty Python/dcs_assistant/test.py", line 3, in <module>
tk = tkinter.Tk()
File "C:\Users\pawni\Miniconda3\envs\dcs_assistant\lib\tkinter\__init__.py", line 2272, in __init__
self._loadtk()
File "C:\Users\pawni\Miniconda3\envs\dcs_assistant\lib\tkinter\__init__.py", line 2286, in _loadtk
tk_version = self.tk.getvar('tk_version')
AttributeError: module '_tkinter' has no attribute 'getvar'
Process finished with exit code 1
The code for the dummy program was:
import tkinter
from tkinter.constants import *
tk = tkinter.Tk()
frame = tkinter.Frame(tk, relief=RIDGE, borderwidth=2)
frame.pack(fill=BOTH,expand=1)
label = tkinter.Label(frame, text="Hello, World")
label.pack(fill=X, expand=1)
button = tkinter.Button(frame, text="Exit", command=tk.destroy)
button.pack(side=BOTTOM)
tk.mainloop()
I tried to uninstall and re-install Tkinter again using both pip and conda. No joy.
I am trying to display an image in a GUI, and don't understand what is wrong. I keep getting this error:
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
What should my code (especially the line with my_img=...) look like?
My Code:
from tkinter import *
from PIL import ImageTk,Image
my_img = ImageTk.PhotoImage(Image.open("iu.jpeg"))
my_label = Label(image=my_img)
my_label.pack()
root = Tk()
root.title("ICON PRACTICE")
root.iconbitmap('iu.ico')
button_quit = Button(root, text = "EXIT", command=root.quit)
button_quit.pack()
root.mainloop()
The full error
Traceback (most recent call last):
File "main.py", line 4, in <module>
my_img = ImageTk.PhotoImage(Image.open("test.png"))
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/PIL/ImageTk.py", line 112, in __init__
self.__photo = tkinter.PhotoImage(**kw)
File "/usr/lib/python3.8/tkinter/__init__.py", line 4064, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/usr/lib/python3.8/tkinter/__init__.py", line 3997, in __init__
raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image
Exception ignored in: <function PhotoImage.__del__ at 0x7f7148fadc10>
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/PIL/ImageTk.py", line 118, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
Try doing this, its probably because you create the root object after opening the image and creating the photo object.
import os
from tkinter import *
from PIL import ImageTk,Image
root= Tk()
i = Image.open("C:/path/to/the/image/directory/image.png")
photo = ImageTk.PhotoImage(i)
root.mainloop()
The Label widget will only work after root = Tk() (Tk() starts the underlying Tcl interpreter) is declared. Then, all child widgets must have root as their first parameter (e.g., Label(root, text='hi')). You started the interpreter after you tried to use it, so Python raised an exception.
I am trying to use Rviz (ROS Visualization tool) in my own application written in PyQt4. I am using QTcreator IDE to design the forms and convert it into python code using pyuic4.
Rviz library uses python_qt_binding which inturn uses PyQT or PySide (in my case I have only installed PyQt).
I am trying to initialize Rviz as a QWidget and display it in my application. My application and Rviz widget work fine when run individually, but shows the following error when I try to Import Rviz QWidget into the application.
Traceback (most recent call last):
File "./mainwindow.py", line 38, in <module>
from RvizInitializer import RViz
File "/home/parallels/Documents/eclipse/workspace/GUI/src/RvizInitializer.py", line 8, in <module>
from python_qt_binding.QtGui import *
File "/opt/ros/kinetic/lib/python2.7/dist-packages/python_qt_binding/__init__.py", line 55, in <module>
from .binding_helper import loadUi, QT_BINDING, QT_BINDING_MODULES, QT_BINDING_VERSION # #UnusedImport
File "/opt/ros/kinetic/lib/python2.7/dist-packages/python_qt_binding/binding_helper.py", line 252, in <module>
getattr(sys, 'SELECT_QT_BINDING_ORDER', None),
File "/opt/ros/kinetic/lib/python2.7/dist-packages/python_qt_binding/binding_helper.py", line 89, in _select_qt_binding
QT_BINDING_VERSION = binding_loader(required_modules, optional_modules)
File "/opt/ros/kinetic/lib/python2.7/dist-packages/python_qt_binding/binding_helper.py", line 131, in _load_pyqt
_named_import('PyQt5.%s' % module_name)
File "/opt/ros/kinetic/lib/python2.7/dist-packages/python_qt_binding/binding_helper.py", line 111, in _named_import
module = builtins.__import__(name)
RuntimeError: the PyQt5.QtCore and PyQt4.QtCore modules both wrap the QObject class
code snippets:
Rviz initializer
class RViz():
def __init__(self):
self.Mainwidget = QWidget()
self.frame = rviz.VisualizationFrame()
self.frame.setSplashPath( "" )
self.frame.initialize()
reader = rviz.YamlConfigReader()
config = rviz.Config()
reader.readFile( config, "/home/parallels/catkin_ws/src/map-toddler/configuration_files/rviz_config_files/karto.rviz" )
self.frame.load( config )
self.Mainwidget.setWindowTitle( config.mapGetChild( "Title" ).getValue() )
self.frame.setMenuBar( None )
self.frame.setStatusBar( None )
self.frame.setHideButtonVisibility( False )
## Here we create the layout and other widgets in the usual Qt way.
layout = QVBoxLayout()
layout.addWidget( self.frame )
self.Mainwidget.setLayout(layout)
#self.Mainwidget.resize(500,500)
#self.Mainwidget.show()
Importing Rviz Qwidget:
from RvizInitializer import RViz
class mainwindow():
def __init__(self):
.
.
other code
.
.
self.RviZ = RViz()
self.MappingFrameHndl.SLAMLayout.addWidget(self.RviZ.Mainwidget)
Any help is greatly appreciated
I'm having a weird problem when trying to use Pillow to display images in tkinter.
I tried originally displaying images in the default tkinter way, which worked fine for gifs:
import tkinter as tk
root = tk.Tk()
src = tk.PhotoImage(file = "C:\\Users\\Matt\\Desktop\\K8pnR.gif")
label = tk.Label(root, image = src)
label.pack()
(K8pnR is just a random gif I found on imgur)
This works great but the only problem is I want to display other file types. This lead me to Pillow, as I am working in Python 3.4. I tried to start with displaying the same file, but using Pillow:
import tkinter as tk
from PIL import Image
root = tk.Tk()
src = Image.open("C:\\Users\\Matt\\Desktop\\K8pnR.gif")
img = tk.PhotoImage(file = src)
label = tk.Label(image = img, master = root)
label.pack()
This leads to a very weird and ugly no such file or directory error:
Traceback (most recent call last):
File "C:\Users\Matt\Desktop\pil test.py", line 7, in <module>
img = tk.PhotoImage(file = src)
File "C:\Python34\lib\tkinter\__init__.py", line 3416, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Python34\lib\tkinter\__init__.py", line 3372, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "<PIL.GifImagePlugin.GifImageFile image mode=P size=494x260 at 0x26A6CD0>": no such file or directory
I tried different files, different filetypes, and even reinstalling Pillow, but I still get the error.
Does anyone know what's going on here? Did I miss something totally obvious?
Edit:
When I try the suggested fix, I get this spooky error:
Traceback (most recent call last):
File "C:\Users\Matt\Desktop\pil test.py", line 6, in <module>
img = ImageTk.PhotoImage(file = src)
File "C:\Python34\lib\site-packages\PIL\ImageTk.py", line 84, in __init__
image = Image.open(kw["file"])
File "C:\Python34\lib\site-packages\PIL\Image.py", line 2297, in open
prefix = fp.read(16)
File "C:\Python34\lib\site-packages\PIL\Image.py", line 632, in __getattr__
raise AttributeError(name)
AttributeError: read
The problem lies in this line:
img = tk.PhotoImage(file = src)
You are using stock PhotoImage from tkinter. It is not compatible with PIL you want to use ImageTk from PIL.
import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
src = Image.open("C:\\Users\\Matt\\Desktop\\K8pnR.gif")
img = ImageTk.PhotoImage(file = src)
label = tk.Label(image = img, master = root)
label.pack()
Here is documentation of stock PhotoImage class: http://effbot.org/tkinterbook/photoimage.htm , it accepts only path in constructor.