[英]How do I delete Zelle graphics text from the screen in Python?
我想刪除圖形顯示的文本,這樣我就可以寫新的文本而不會重疊。 我在下面包含了我的代碼。 再次感謝您的寶貴時間。
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('')
直到再次需要它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.