Tkinter Image Problem

Thread Solved

Join Date: Mar 2007
Posts: 16
Reputation: Logi. is an unknown quantity at this point 
Solved Threads: 0
Logi. Logi. is offline Offline
Newbie Poster

Tkinter Image Problem

 
0
  #1
Mar 13th, 2007
Hi,

Im having trouble with the piece of code below, i want to have the image change every time you press the button. This code is from a larger project but this is the bit that i cant get to work.

Any ideas?



  1. import Tkinter
  2. import random
  3. import Image, ImageTk
  4.  
  5. def changeImage():
  6.  
  7. listOfImages = ["r7", "A", "C", "B", "b7", "f", "P", "G", "O"]
  8. im = listOfImages[random.randint(0,8)]
  9. box1Label.configure(image=im)
  10.  
  11. top = Tkinter.Tk()
  12.  
  13. r7 = ImageTk.PhotoImage(file="gfx/r7.jpg")
  14. b7 = ImageTk.PhotoImage(file="gfx/b7.jpg")
  15. A = ImageTk.PhotoImage(file="gfx/A.jpg")
  16. B = ImageTk.PhotoImage(file="gfx/B.jpg")
  17. C = ImageTk.PhotoImage(file="gfx/C.jpg")
  18. f = ImageTk.PhotoImage(file="gfx/f.jpg")
  19. G = ImageTk.PhotoImage(file="gfx/G.jpg")
  20. P = ImageTk.PhotoImage(file="gfx/P.jpg")
  21. O = ImageTk.PhotoImage(file="gfx/O.jpg")
  22.  
  23.  
  24. box1Label = Tkinter.Label(top, image=r7)
  25. box1Label.grid(row=3, column=2)
  26.  
  27. changeButton = Tkinter.Button(top, text="Change", command=changeImage)
  28. changeButton.grid(row=9, column=7)
  29.  
  30. Tkinter.mainloop()
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,013
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 929
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Tkinter Image Problem

 
0
  #2
Mar 13th, 2007
Note that the images are objects not strings, so you have to do this ...
  1. import Tkinter
  2. import random
  3. import Image, ImageTk
  4.  
  5. def changeImage():
  6. global listOfImages
  7. im = listOfImages[random.randint(0,8)]
  8. box1Label.configure(image=im)
  9.  
  10. top = Tkinter.Tk()
  11. r7 = ImageTk.PhotoImage(file="gfx/r7.jpg")
  12. b7 = ImageTk.PhotoImage(file="gfx/b7.jpg")
  13. A = ImageTk.PhotoImage(file="gfx/A.jpg")
  14. B = ImageTk.PhotoImage(file="gfx/B.jpg")
  15. C = ImageTk.PhotoImage(file="gfx/C.jpg")
  16. f = ImageTk.PhotoImage(file="gfx/f.jpg")
  17. G = ImageTk.PhotoImage(file="gfx/G.jpg")
  18. P = ImageTk.PhotoImage(file="gfx/P.jpg")
  19. O = ImageTk.PhotoImage(file="gfx/O.jpg")
  20. # images are objects not strings
  21. listOfImages = [r7, A, C, B, b7, f, P, G, O]
  22.  
  23. box1Label = Tkinter.Label(top, image=r7)
  24. box1Label.grid(row=3, column=2)
  25. changeButton = Tkinter.Button(top, text="Change", command=changeImage)
  26. changeButton.grid(row=9, column=7)
  27.  
  28. Tkinter.mainloop()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC