0

I am trying to display an image in a window with tkinter, by clicking a button.

I created this function :

def tabuim():
    tabu = Frame(win, width=600, height=400)
    tabu.pack()
    tabu.place(anchor='center', relx=0.8, rely=0.35)
    img2 = ImageTk.PhotoImage(Image.open("Tabu.jpg"), master=win)
    label2 = Label(tabu, image = img2, borderwidth=3, relief="solid")
    label2.pack()

with this button :

button2 = Button(win, text = 'Tabu Route Solution', command = tabuim)
button2.place(relx = 0.5, rely = 0.5)

But it partially works. It only show a border without my image...

Thanks for you help

VoidAlpha
  • 1
  • 1

1 Answers1

0

The following example displays a background image in a frame:

Import tkinter
from tkinter import *
from PIL import Image, ImageTk

root = Tk()

Create a photoimage object of the image in the path

image1 = Image.open("<path/image_name>")
test = ImageTk.PhotoImage(image1)

label1 = tkinter.Label(image=test)
label1.image = test

Position image

label1.place(x=<x_coordinate>, y=<y_coordinate>)
root.mainloop()

This an example you can do as instead of resizing before loading the page you could resize after the image is loaded.