uplaoding an image using a button

Thread Solved

Join Date: Nov 2007
Posts: 46
Reputation: nish88 is an unknown quantity at this point 
Solved Threads: 0
nish88's Avatar
nish88 nish88 is offline Offline
Light Poster

uplaoding an image using a button

 
0
  #1
Mar 18th, 2008
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
  1.  
  2. import Tkinter as tk
  3. from PIL import Image, ImageTk
  4.  
  5. import Tkinter
  6. from Tkconstants import *
  7.  
  8.  
  9.  
  10.  
  11. frame=tk.Frame()
  12. frame.pack()
  13.  
  14.  
  15.  
  16. canvas =tk.Canvas(frame,bg = 'white',width=500, height=500)
  17. canvas.pack()
  18.  
  19. def image():
  20. imageFile = "map.png"
  21. image1 = ImageTk.PhotoImage(Image.open(imageFile))
  22.  
  23. canvas.create_image(200, 250, image=image1)
  24.  
  25. canvas.update()
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. button=tk.Button(frame,fg="blue", text="Insert Image", activebackground='red',font=('verdana', 10, 'bold'),command=image)
  34.  
  35. button.pack(padx =50,side = "left")
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. frame.mainloop()

i want when i click on the insert image button the image appear on the canvas.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,276
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: uplaoding an image using a button

 
0
  #2
Mar 18th, 2008
Please stick to established Python coding rules, you code is hard to read:
  1. import Tkinter as tk
  2. from PIL import Image, ImageTk
  3.  
  4. def show_image():
  5. """image object image1 created in main"""
  6. canvas.create_image(250, 250, image=image1)
  7. #canvas.update()
  8.  
  9. frame = tk.Frame()
  10. frame.pack()
  11.  
  12. canvas = tk.Canvas(frame, bg='white', width=500, height=500)
  13. canvas.pack()
  14.  
  15. button = tk.Button(frame, fg="blue", text="Insert Image",
  16. activebackground='red', font=('verdana', 10, 'bold'), command=show_image)
  17. button.pack(padx=50, side="left")
  18.  
  19. # create the image object here, not in the function
  20. imageFile = "map.png"
  21. image1 = ImageTk.PhotoImage(Image.open(imageFile))
  22.  
  23. """
  24. # default is center the image
  25. canvas.create_image(250, 250, image=image1)
  26. canvas.update()
  27. """
  28.  
  29. frame.mainloop()
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 46
Reputation: nish88 is an unknown quantity at this point 
Solved Threads: 0
nish88's Avatar
nish88 nish88 is offline Offline
Light Poster

Re: uplaoding an image using a button

 
0
  #3
Mar 19th, 2008
thanks sneekula. sorry for badly writing my codes. i'm just a beginner i hope with time i'll be a master in python
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC