0

I am just learning Python. I have a program that will choose a random image each time you click the mouse!! How do I clear the window so images don't stack. I am sure I am asking this question wrong because google is no help :(

Thank you and here is the code:

from graphics import*
import random
import os

win = GraphWin("My Window", 500, 500)

for x in range(5):

    cards = ["1.gif","2.gif","3.gif"]
    rand_card = random.choice(cards)
    img = Image(Point(250, 250), rand_card)

    win.setBackground('Black')
    img.draw(win)

    win.getMouse()
martineau
  • 119,623
  • 25
  • 170
  • 301
Jason
  • 57
  • 8

1 Answers1

1

Based on the code at http://mcsp.wartburg.edu/zelle/python/graphics.py, the Image class has an undraw function. You can simply add image.undraw() with a check to make sure it's not None (as it would be for the first time in the loop:

img = None
win = GraphWin("My Window", 500, 500)

for x in range(5):

    cards = ["1.gif","2.gif","3.gif"]
    rand_card = random.choice(cards)
    if img:
        img.undraw()
    img = Image(Point(250, 250), rand_card)

    win.setBackground('Black')
    img.draw(win)

    win.getMouse()

(Note, change added based on cdlane's comment - initialize img to None so we don't get variable undefined.)

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
  • Scott, it worked!!! Thank you so very much!! Thank you to everyone who took time out of their day to answer. I am so happy – Jason Jun 07 '17 at 18:54
  • Happy to help. Like Mark said the [last time you asked a question](https://stackoverflow.com/a/33816216/1404311), feel free to upvote and/or accept any answers that you find useful. – Scott Mermelstein Jun 07 '17 at 19:33
  • I guess I need to turn "15" to upvote or like lol. At the rate I ask questions It will be 2021 :) – Jason Jun 07 '17 at 23:22
  • You can always upvite (or downvote) on answers to your own questions. And you actually earn points for accepting an answer (by clicking on the checkmark). – Scott Mermelstein Jun 07 '17 at 23:50
  • When I run this example, I get "NameError: name 'img' is not defined". Also, why is `win.setBackground('Black')` still inside the loop instead of simply before the loop as it's only needed once as the card undraws itself. Ditto with respect to why is `cards = ["1.gif","2.gif","3.gif"]` inside the loop? – cdlane Jun 08 '17 at 06:34
  • @cdlane You bring up good points - both of those could be removed from the loop. I tend to just copy what the OP gives and give them a minimal change needed to do what they ask. Regarding `img` undefined, that would be part of the minimal change I need to include, so I'll edit the fix for that in. Thanks! – Scott Mermelstein Jun 08 '17 at 06:51
  • Hi, I am sorry for the confusion. The "img" variable is local to my computer. This is a stub of a card matching game that I am trying to make. "img1", "img2" and "img3" are pictures of different cute animals in the same folder as the program :) – Jason Jun 08 '17 at 13:35