Python Forum
[Tkinter] png porblem in python3
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] png porblem in python3
#1
Hello:
Sorry for my bad english.
I am a beginner of python.
I have made an application in python3 in windows 10 with tkinter and it works perfectly for me.
In Ubuntu 18.04 with python3 version 3.6.5 I get an error. I have tried with raspberry pi 3 with python3 version 3.5.3 and I have the same error.



This is the script:


import tkinter
from tkinter import *
import os

raiz=tkinter.Tk()
raiz.title("Ver png")
raiz.config(bg="White")

# medida ventana
w = raiz.winfo_screenwidth()
h = raiz.winfo_screenheight()

raiz.geometry("%dx%d+0+0" % (w, h))

# Bloqueo cambio tamaño pantalla
raiz.resizable(width=False, height=False)

directorio=os.path.dirname(__file__)
print(w,h,directorio)

Imagenfondo0=PhotoImage(file=directorio+"/Hydrisgrande.png")
canvas=tkinter.Canvas(raiz,width=w,height=h,bg="white")
canvas.place(x=1,y=1)
image = canvas.create_image(1, 1, anchor=NW, image=Imagenfondo0)



This is the error:

================ RESTART: /home/pi/Hydris/python/verimagen.py ================
1824 984 /home/pi/Hydris/python
Traceback (most recent call last):
File "/home/pi/Hydris/python/verimagen.py", line 21, in <module>
Imagenfondo0=PhotoImage(file=directorio+"/Hydrisgrande.png")
File "/usr/lib/python3.5/tkinter/__init__.py", line 3406, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/usr/lib/python3.5/tkinter/__init__.py", line 3362, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "/home/pi/Hydris/python/Hydrisgrande.png": no such file or directory

Thank you in advance for trying to help me
Reply
#2
Your image must be in the same folder with your script.
Reply
#3
Python says that there is no file named /home/pi/Hydris/python/Hydrisgrande.png. You can check this in the file system. It is not a programming problem.
Reply
#4
I'm sorry .. I forgot to say that the png file is in the same directory where the script is.
Reply
#5
is the extension png or PNG ?

You have to pay attention to upper / lower case.
Reply
#6
It is all written correctly
Reply
#7
I've made a test, saved your script in /tmp, copied an image to tmp (named it Hydrisgrande.png)

and it works from my IDE, but not from Terminal.

You forgot raiz.mainloop() at the end.

import tkinter
from tkinter import *
import os

raiz=tkinter.Tk()
raiz.title("Ver png")
raiz.config(bg="White")

# medida ventana
w = raiz.winfo_screenwidth()
h = raiz.winfo_screenheight()

raiz.geometry("%dx%d+0+0" % (w, h))

# Bloqueo cambio tamaño pantalla
raiz.resizable(width=False, height=False)

directorio=os.path.dirname(__file__)
print(w,h,directorio)

Imagenfondo0=PhotoImage(file=directorio+"/Hydrisgrande.png")
canvas=tkinter.Canvas(raiz,width=w,height=h,bg="white")
canvas.place(x=1,y=1)
image = canvas.create_image(1, 1, anchor=NW, image=Imagenfondo0)
raiz.mainloop()
in Terminal, this works for me

import tkinter
from tkinter import *
import os

raiz=tkinter.Tk()
raiz.title("Ver png")
raiz.config(bg="White")

# medida ventana
w = raiz.winfo_screenwidth()
h = raiz.winfo_screenheight()

raiz.geometry("%dx%d+0+0" % (w, h))

# Bloqueo cambio tamaño pantalla
raiz.resizable(width=False, height=False)

directorio=os.path.dirname(__file__)
print(w,h,directorio)

picFile = "./Hydrisgrande.png"
Imagenfondo0=PhotoImage(file=picFile)
canvas=tkinter.Canvas(raiz,width=w,height=h,bg="white")
canvas.place(x=1,y=1)
image = canvas.create_image(1, 1, anchor=NW, image=Imagenfondo0)
raiz.mainloop()

use
directorio=os.path.dirname(os.path.abspath(__file__))
that works for me in both(IDE and Terminal)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020