我正在使用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函数全部移开,但是它要么根本不出现,要么重叠。

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

谢谢!

===============>>#1 票数:0

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

例:

string = Text(Point(1, 1), 'original string') 
sting.setText('new string')

===============>>#2 票数:0

如果要使某些内容不可见,则需要使用undraw()命令。 我建议将其放在您的Continue语句之前。 就像

questionText.undraw()
questionAnswer.undraw()

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

  ask by Zack Tanner translate from so

未解决问题?本站智能推荐: