Skip to content Skip to sidebar Skip to footer

How Can You Display An Image On A Gui Using Tk In Python2.7

i need away to be able to show an image when i run the python code in its gui form if possible. Also do i have to type in the folder name and file name in the code if you know the

Solution 1:

Hello if your looking to show an image inside a tk Canvas it needs to be a PPM/PGM or GIF: discussion from python.org

However if your looking to load a PPM, PGM or GIF:

import Tkinter as tk
root = tk.Tk()
root.title("display a website image")
photo = tk.PhotoImage(file= r"C:\Some\Local\location\smile.gif")
cv = tk.Canvas()
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(10, 10, image=photo, anchor='nw')
root.mainloop()

This creates a frame, loads the image using tk.PhotoImage, then makes a canvas and puts the image onto the canvas.

Solution 2:

If I understand, you just want to show an image using Tkinter?

To do this, you can use a Canvas widget filled with your image.

Here is an example:

can = Canvas(window, width=160, height=160, bg='white')
pic = PhotoImage(file='picture.jpg')
item = can.create_image(80, 80, image=pic)

Post a Comment for "How Can You Display An Image On A Gui Using Tk In Python2.7"