| | |
Tkinter Image Problem
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 16
Reputation:
Solved Threads: 0
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?
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?
python Syntax (Toggle Plain Text)
import Tkinter import random import Image, ImageTk def changeImage(): listOfImages = ["r7", "A", "C", "B", "b7", "f", "P", "G", "O"] im = listOfImages[random.randint(0,8)] box1Label.configure(image=im) top = Tkinter.Tk() r7 = ImageTk.PhotoImage(file="gfx/r7.jpg") b7 = ImageTk.PhotoImage(file="gfx/b7.jpg") A = ImageTk.PhotoImage(file="gfx/A.jpg") B = ImageTk.PhotoImage(file="gfx/B.jpg") C = ImageTk.PhotoImage(file="gfx/C.jpg") f = ImageTk.PhotoImage(file="gfx/f.jpg") G = ImageTk.PhotoImage(file="gfx/G.jpg") P = ImageTk.PhotoImage(file="gfx/P.jpg") O = ImageTk.PhotoImage(file="gfx/O.jpg") box1Label = Tkinter.Label(top, image=r7) box1Label.grid(row=3, column=2) changeButton = Tkinter.Button(top, text="Change", command=changeImage) changeButton.grid(row=9, column=7) Tkinter.mainloop()
Note that the images are objects not strings, so you have to do this ...
python Syntax (Toggle Plain Text)
import Tkinter import random import Image, ImageTk def changeImage(): global listOfImages im = listOfImages[random.randint(0,8)] box1Label.configure(image=im) top = Tkinter.Tk() r7 = ImageTk.PhotoImage(file="gfx/r7.jpg") b7 = ImageTk.PhotoImage(file="gfx/b7.jpg") A = ImageTk.PhotoImage(file="gfx/A.jpg") B = ImageTk.PhotoImage(file="gfx/B.jpg") C = ImageTk.PhotoImage(file="gfx/C.jpg") f = ImageTk.PhotoImage(file="gfx/f.jpg") G = ImageTk.PhotoImage(file="gfx/G.jpg") P = ImageTk.PhotoImage(file="gfx/P.jpg") O = ImageTk.PhotoImage(file="gfx/O.jpg") # images are objects not strings listOfImages = [r7, A, C, B, b7, f, P, G, O] box1Label = Tkinter.Label(top, image=r7) box1Label.grid(row=3, column=2) changeButton = Tkinter.Button(top, text="Change", command=changeImage) changeButton.grid(row=9, column=7) Tkinter.mainloop()
May 'the Google' be with you!
![]() |
Similar Threads
- Putting an image into a Tkinter thingy (Python)
- Image and ImageTk weirdness (Python)
- Tkinter GUI Issues: (Python)
- Preview Upload Image Problem (PHP)
- image editing/ independent foreground (Graphics and Multimedia)
- Image appears on some browsers, doesn't on others (HTML and CSS)
- adobe image problem (Graphics and Multimedia)
Other Threads in the Python Forum
- Previous Thread: web programming question
- Next Thread: Calling a Script
| Thread Tools | Search this Thread |
address alarm app beginner cipher cmd coordinates cx-freeze data decimals development dictionary directory dynamic error examples feet file float format function generator getvalue gui halp homework http images import input ip itunes java keycontrol leftmouse line linux list lists loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pymailer pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation split sqlite ssh string strings sudokusolver terminal text thread threading time tlapse tooltip tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo vigenere web webservice wikipedia wx.wizard wxpython xlwt






