我只是在学习Python。我有一个程序,每次你点击鼠标就会选择一个随机图像!!如何清除窗口以使图像不会堆叠。我确信我问这个问题是错误的,因为谷歌没有帮助:(
谢谢,这是代码:
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()
答案 0 :(得分:1)
根据http://mcsp.wartburg.edu/zelle/python/graphics.py处的代码,Image
类具有undraw
功能。您只需添加image.undraw()
并添加一张支票即可确保它不是None
(因为这是第一次在循环中使用:
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()
(注意,根据cdlane的评论添加了更改 - 将img
初始化为无,因此我们不会将变量定义为未定义。)