How to Add an Image in Tkinter

How to add an image in Tkinter?

There is no "Syntax Error" in the code above - it either ocurred in some other line (the above is not all of your code, as there are no imports, neither the declaration of your path variable) or you got some other error type.

The example above worked fine for me, testing on the interactive interpreter.

How to add an image to a Tkinter Class

change image to self.image and render to self.render. that might do the work.

however, I suggest to use it like this:

        #image_directories
self.images = {
"logo": self.ready_img("logo.png", (120, 130))
}

def ready_img(self, path, size):

return ImageTk.PhotoImage(Image.open(path).resize(size, Image.ANTIALIAS))

then to get the image you can do :
self.images.get("logo")

I was facing the same problem, doing like this solved it.

How to insert an image in a canvas item?

try:
import tkinter as tk
from tkinter.constants import *
except ImportError: # Python 2.x
import Tkinter as tk
from Tkconstants import *

# Create the canvas, size in pixels.
canvas = tk.Canvas(width=300, height=200, bg='black')

# Pack the canvas into the Frame.
canvas.pack(expand=YES, fill=BOTH)

# Load the .gif image file.
gif1 = tk.PhotoImage(file='small_globe.gif')

# Put gif image on canvas.
# Pic's upper-left corner (NW) on the canvas is at x=50 y=10.
canvas.create_image(50, 10, image=gif1, anchor=NW)

# Run it...
tk.mainloop()

Function to add an image in Tkinter

Welcome to StackOverflow! I know that you have already accepted an answer but I want to show you my take on your code:

import tkinter as tk
import tkinter.ttk as ttk
import PIL.ImageTk
import PIL.Image

root = tk.Tk()
root.title("Guess Geek")
root.geometry("1280x720")
root.resizable(0, 0)

def importimg(file_name):
return PIL.ImageTk.PhotoImage(PIL.Image.open(file_name))

bg_img = importimg('main1.jpg')
bg = ttk.Label(root, image=bg_img)
bg.grid(column=0, row=0)
root.mainloop()

So here's what I have changed in your code:

  • Instead of bringing al of tkinter and tkinter.ttk in the global scope, I imported only the module
  • I renamed tkinter as tk and tkinter.ttk as ttk for the convenience of having shorter names to use
  • I imported the PIL.ImageTk module without bringing them in the global scope just to be sure of where it came from
  • I imported the PIL.Image module without bringing them in the global scope because there is a tkinter.Image that would have overrode it if with kept both in the global scope (the way your code was written)
  • PIL.ImageTk.PhotoImage() first argument should an PIL's image (provided easily by Image.open()) and not just the file name
  • importimg() returns the object created from the class PIL.ImageTk.PhotoImage(). This will get assigned to bg_img
  • Instead of using the place() geometry manager, I've used the grid()'s one. It's generally recommended by the TkDocs website

If you have any questions don't hesitate to ask.

How to add images to Tkinter GUI on Python 3.x

So, it seems tkinter doesn't support .jpg anymore.

I solved your problem using PIL.

To install PIL run pip install Pillow.

I managed to make it work on my local machine as:

from tkinter import *
from PIL import ImageTk, Image

game = Tk()
game.wm_title("Snake Collection")
game.config(bg="#EB5E55")

left1 = Frame(game, width=500, height=1000)
left1.grid(row=0, column=0, padx=15, pady=15)

def bonuslvl():

bonusimg1 = ImageTk.PhotoImage(file='snake1.jpg')
Label(left1, image=bonusimg1).grid(row=0, column=0, padx=5, pady=5)

bonuslvl()

game.mainloop()


Related Topics



Leave a reply



Submit