如何在Python中从屏幕上删除Zelle图形文本?
我想删除以图形方式显示的文本,这样我就可以不重叠地编写新文本。我在下面包含了我的代码。再次感谢您抽出时间如何在Python中从屏幕上删除Zelle图形文本?,python,python-3.x,text,zelle-graphics,Python,Python 3.x,Text,Zelle Graphics,我想删除以图形方式显示的文本,这样我就可以不重叠地编写新文本。我在下面包含了我的代码。再次感谢您抽出时间 from graphics import* import random # Function for winning def youwin(): txt = Text(Point(250,100), "You are a Winner!") txt.setTextColor(color_rgb(0,255,200)) txt.setSize(30) txt.
from graphics import*
import random
# Function for winning
def youwin():
txt = Text(Point(250,100), "You are a Winner!")
txt.setTextColor(color_rgb(0,255,200))
txt.setSize(30)
txt.setFace('courier')
txt.draw(win)
# Function for losing
def youlose():
txt= Text(Point(250,100), "You are a Loser!")
txt.setTextColor(color_rgb(0,255,0))
txt.setSize(30)
txt.setFace('courier')
txt.draw(win)
# Sets a Window
win = GraphWin("My Window", 800, 600)
win.setBackground('White')
# loops 10 Times and picks out 3 random gif's.
# Then displays each image on the screen in a row.
for x in range(10):
cards = ["1.png","2.png","3.png"]
rand_card1 = random.choice(cards)
img1 = Image(Point(100, 250), rand_card1)
rand_card2 = random.choice(cards)
img2 = Image(Point(300, 250), rand_card2)
rand_card3 = random.choice(cards)
img3 = Image(Point(500, 250), rand_card3)
img1.draw(win)
img2.draw(win)
img3.draw(win)
# checks to see if you are a winner
if rand_card1 == rand_card2 and rand_card2 == rand_card3:
youwin()
else:
youlose()
# waits for a mouse click. In the future this will be a button.
win.getMouse()
img1.undraw()
img2.undraw()
img3.undraw()
#win.close()#
这是我当前获得的输出:
我想删除以图形方式显示的文本,以便
写新课文
不要。您可以通过setText()
重用文本对象。一个简单的例子:
import time # just for demonstration
from graphics import *
win = GraphWin("My Window", 800, 600)
txt = Text(Point(250,100), "You are a Loser!")
txt.setSize(30)
txt.draw(win)
time.sleep(3)
txt.setText("You are a Winner!")
time.sleep(3)
如果您希望文本消失一段时间,只需
txt.setText(“”)
,直到您再次需要它。我想您可能希望在绘制新文本之前以某种方式清除绘图区域。一种方法是在旧文本上绘制一个与背景颜色相同的矩形。我对您使用的颜色不太熟悉,但我想您可能想在绘制新文本之前清除绘图区域。我会寻找一个屏幕清除功能,或者在旧文本上画一个与背景颜色相同的矩形。也许有比我更博学的人会明白我的意思,并在下面发布所需的确切代码。。。但这就是我的一般做法。谢谢你,克里斯,矩形成功了!!!我确信我的编码效率不高,但作为一个新手,我会接受我能得到的:)你能把你的评论变成一个答案,这样我就可以投票吗?我认为上面的代码不起作用:(我不确定“Zelle”图形是什么。我正在使用graphics.py。谢谢你的回答:)我现在明白了“Zelle”是graphics.py的创建者。好了,回到youtube上看更多介绍性视频:)@Jason,我刚刚复制了上面的代码,粘贴到一个文件中并运行了它。它对我来说运行良好,什么不起作用?请提供错误消息。@Jason,这不是Zelle graphics的好解决方案。考虑倒计时的文本。每次更改数字时,都会将另外两个图形对象与窗口关联,即空白矩形和新文本。相反,您只需在创建新文本之前告诉旧文本对象undraw()
本身,或者更好的是,告诉相同的文本对象setText(“新文本”)
显示新文本,而不删除或创建任何附加内容。