0

i am trying to have python display an image when you request for that image. i have used the following code which works fine:

from tkinter import*

window = Tk()
window.title ('R,P,S')
c=Canvas(window,width=300, height=300, bg='yellow')
c.pack()


user = input("please type rock, paper, scissors: ")

if user == "rock":
    image = PhotoImage(file = 'rock.gif')
    c.create_image(150, 150, image = image)

elif user == "paper":
    image = PhotoImage(file = 'paper.gif')
    c.create_image(150, 150, image = image)

else:
    image = PhotoImage(file = 'scissors.gif')
    c.create_image(150, 150, image = image)

i have the gif images i created in the same folder director as the python program.

however when i try to use a type of loop if ignores the commands to display image when requested from the user.

for example:

for i in range (3): 

or

while True: 

Why does it ignore the image command when in a loop?

what i would like is to allow the user to have the option to select each image so it would be: Please type rock, paper, scissors: rock (displays rock image) Please type rock, paper, scissors: paper (displays paper image) Please type rock, paper, scissors: scissors(displays scissors image)

with out the need to restart the shell.

P_Lee
  • 65
  • 7
  • Is your loop inside a function? If so, you have been visited by the [PhotoImage garbage collection bug](http://stackoverflow.com/questions/27430648/python-tkinter-2-7-issue-with-image), and you must safely store your `image` references somewhere that they won't be prematurely deleted. – Kevin Feb 04 '15 at 18:15
  • If your loop is not inside a function, please provide full copy-pastable code which replicates your problem. – Kevin Feb 04 '15 at 18:16

0 Answers0