hi everyone. i'm try to load an image by clicking on a botton insert imag but the problem thata i having with this is that
the image is not being insert it just appear and disappear immediately.
can anyone tell me what is wrong with my codes and tel me how to do it.
thanks...
here is my code

import Tkinter as tk
from PIL import Image, ImageTk

import Tkinter
from Tkconstants import *




frame=tk.Frame()
frame.pack()



canvas =tk.Canvas(frame,bg = 'white',width=500, height=500)
canvas.pack()

def image():
    imageFile = "map.png"
    image1 = ImageTk.PhotoImage(Image.open(imageFile))

    canvas.create_image(200, 250, image=image1)
    
    canvas.update()







button=tk.Button(frame,fg="blue", text="Insert Image", activebackground='red',font=('verdana', 10, 'bold'),command=image)

button.pack(padx =50,side = "left")






frame.mainloop()

i want when i click on the insert image button the image appear on the canvas.

Please stick to established Python coding rules, you code is hard to read:

import Tkinter as tk
from PIL import Image, ImageTk

def show_image():
    """image object image1 created in main"""
    canvas.create_image(250, 250, image=image1)
    #canvas.update()

frame = tk.Frame()
frame.pack()

canvas = tk.Canvas(frame, bg='white', width=500, height=500)
canvas.pack()

button = tk.Button(frame, fg="blue", text="Insert Image",
    activebackground='red', font=('verdana', 10, 'bold'), command=show_image)
button.pack(padx=50, side="left")

# create the image object here, not in the function
imageFile = "map.png"
image1 = ImageTk.PhotoImage(Image.open(imageFile))

"""
# default is center the image
canvas.create_image(250, 250, image=image1)
canvas.update()
"""

frame.mainloop()

thanks sneekula. sorry for badly writing my codes. i'm just a beginner i hope with time i'll be a master in python

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.