简体   繁体   中英

Importing image into Tkinter - Python 3.5

I'm majorly struggling to import an image into Tkinter for Python 3.5 , this is for my A2 project and have hit a brick wall with every method of importing a .JPG file to my window. What I have below is my GUI layer for another window, based off another thread I found but didn't work at all. Any assistance appreciated.

import tkinter 
from tkinter import *

root=Tk()
root.geometry('1000x700')
root.resizable(False, False)
root.title("Skyrim: After Alduin")

photo = PhotoImage(file="Skyrim_Map")
map=Label(root, image=photo)
map.photo=photo
map.pack()

root.mainloop()

Here's the error I receive:

Traceback (most recent call last):
  File "E:\Programming\Text_Adventure_Project\GUI_Interface-S_AA.py", line 14, in <module>
    photo = PhotoImage(file="Skyrim_Map")
  File "C:\Users\Harrison\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 3393, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\Harrison\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 3349, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "Skyrim_Map": no such file or directory

First check the path if with the correct path still persist the error "no such file or directory" please edit and put the newest version of your code. If the error is "couldn't recognize data in image file" you can correct using PIL

 [...]
 from PIL import ImageTk

 [...]
 photo = ImageTk.PhotoImage(file='Skyrim_Map.jpg')
 [...]

试试这个:

from PIL import Image , ImageTk img = Image.open("your_img") image = ImageTk.PhotoImage(img)

An other point of view :

class Application:

    ...
    self.photo=PhotoImage(file='picture.gif')
    self.item=self.can.create_image(200, 100, image=self.photo)
    ...

Hope to help ! ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM