0

我正在使用Zelle 图形库,但无法替换图形对象(在这种情况下,恰好是文本对象)。

这是代码:

from Graphics import *

winName = "Window"
win = Window(winName,600,500)
win.setBackground(Color('silver'))


title = Text((300,20),"Zack's Flash Card Maker")
title.draw(win)

p1 = Rectangle((50, 100),(550,400))
p1.setFill(Color("black"))
p1.draw(win)




class FlashCard:
    def __init__(self):
        self.commands = {'addQuestion':self.addQuestion,'startGame':self.startGame}
        self.stack = []
        self.questions = {}
        self.questionAnswered = False
        self.questionsCorrect = 0
        self.questionsIncorrect = 0

    def addQuestion(self):
        question = ' '.join(self.stack)
        self.stack = []
        answer = input(question)
        self.questions[question] = answer

    def startGame(self):
        for question in self.questions:
            if(self.questionAnswered == False):
                answer=input(question)
                questionText = Text((300,150),question)
                questionText.setFill(Color("white"))
                questionText.draw(win)

                if(answer == self.questions[question]):
                    questionAnswer = Text((300,200),answer + " is correct!")
                    questionAnswer.setFill(Color("green"))
                    questionAnswer.draw(win)

                    self.questionsCorrect = self.questionsCorrect + 1


                    continue

                else:
                    questionAnswer = Text((300,200),answer + " is incorrect. Study this one.")
                    questionAnswer.setFill(Color("red"))
                    questionAnswer.draw(win)
                    self.questionsIncorrect = self.questionsIncorrect + 1

                    continue




    def interpret(self,expression):
        for token in expression.split():
            if token in self.commands:
                operator = self.commands[token]
                operator()
            else:
                self.stack.append(token)



i = FlashCard()
i.interpret('What is your dog\'s name? addQuestion')
i.interpret('What is your favorite thing to do? addQuestion')
i.interpret('startGame')

这本质上是我正在制作的迷你闪存卡程序。它采用底部的解释命令并根据 FlashCard 类中的字典执行它们。它基本上可以工作:它执行正确的文本对象。但是,由于重新绘制,文本开始与其他文本对象重叠。我尝试过移动 .draw 函数,但它要么根本不出现,要么重叠。

有人有什么建议吗?我希望为每个新的抽认卡问题替换文本。

谢谢!

4

2 回答 2

0

如果要使某些东西不可见,则需要使用 undraw() 命令。我建议将它放在您的 continue 语句之前。它像

questionText.undraw()
questionAnswer.undraw()

或者,您可以使用 del 命令在完成后删除每个 questionText/questionAnswer 实例。这可能是一个更好的选择,因为您实际上是在以这种方式释放内存,而不是存储数据并且不对其进行任何操作。

于 2011-12-11T20:20:43.737 回答
0

您可以使用 setText 方法来更改文本。

例子:

string = Text(Point(1, 1), 'original string') 
sting.setText('new string')
于 2017-02-28T02:23:54.497 回答