Domanda

In my program that I have posted below, the user is suppose to click on a circle that is drawn using a specific color. Depending on whether or not the circle is colored yellow or blue, text will display in the same window as "Correct" or "Incorrect". The problem I am having is after the user click on a circle the text will display, but after the first try, the text that is in the window will remain, leading to the unfortuate problem of each subsequent click causing the new text to write over the previously displayed text. If anyone knows how to get the text to "reset" or "clear" that is in the window, so the window will be blank each time the user clicks, I will appreciate it. Thanks

from graphics import *
import tkinter as tk 
import threading 
import random 


class App(): 

    def __init__(self): 
        self.win = GraphWin('Demo2', 800, 600) # give title and dimensions 
        self.th = threading.Thread(target=self.FlashThread, daemon=False) 


    def FlashThread(self): 
        while not self.win.isClosed(): 
            count = random.randint(0, 8) 
            t = threading.Timer(1.0, self.flash, [count]) 
            t.start() 
            t.join() 


    def flash(self, count): 
        try: 
            diameter = 50
            centers = ((55,55),  (170,55), (285,55),  (55,170), (170,170),  
                   (285,170), (55,285), (170,285), (285,285)) 
            circles = list() 
            for point in centers: 
                c = Circle(Point(point[0], point[1]), diameter) 
                circles.append(c) 
                c.setFill("blue") 
                c.draw(self.win) 
            circles[count].setFill("yellow") 
            mouseClick = self.win.getMouse() 
            correctMessage = Text(Point(self.win.getWidth()/2, 20), 'Correct!') 
            incorrectMessage = Text(Point(self.win.getWidth()/2, 20), 'Incorrect,Try Again') 
            leftX  = centers[count][0] - diameter 
            rightX = centers[count][0] + diameter 
            upperY = centers[count][1] - diameter 
            lowerY = centers[count][1] + diameter 
            if (upperY < mouseClick.y < lowerY) and (leftX < mouseClick.x < rightX): 
                correctMessage.draw(self.win) 
            else: 
                incorrectMessage.draw(self.win) 
        except: 
            self.win.exit(0) 


if __name__ == "__main__": 
    try: 
        app = App() 
        app.th.start() 
        app.win.mainloop() 
        app.th.join() 
    finally: 
        app.th.close() 
        app.close() 
È stato utile?

Soluzione

You can do this, using undraw() method. Change your __init__ to:

def __init__(self):
    self.win = GraphWin('Demo2', 800, 600) # give title and dimensions
    self.th = threading.Thread(target=self.FlashThread)

    self.correctMessage = Text(Point(self.win.getWidth()/2, 20), 'Correct!')
    self.incorrectMessage = Text(Point(self.win.getWidth()/2, 20), 'Incorrect,Try Again')

and flash to

def flash(self, count):
        try:
            self.correctMessage.undraw()
            self.incorrectMessage.undraw()

            diameter = 50
            centers = ((55,55),  (170,55), (285,55),  (55,170), (170,170),
                   (285,170), (55,285), (170,285), (285,285))
            circles = list()
            for point in centers:
                c = Circle(Point(point[0], point[1]), diameter)
                circles.append(c)
                c.setFill("blue")
                c.draw(self.win)
            circles[count].setFill("yellow")
            mouseClick = self.win.getMouse()

            leftX  = centers[count][0] - diameter
            rightX = centers[count][0] + diameter
            upperY = centers[count][1] - diameter
            lowerY = centers[count][1] + diameter
            if (upperY < mouseClick.y < lowerY) and (leftX < mouseClick.x < rightX):
                self.correctMessage.draw(self.win)
            else:
                self.incorrectMessage.draw(self.win)


        except:
            self.win.exit(0)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top