简体   繁体   中英

How to make Python turtle window respond while waiting for further data

My code takes laptop's battery data and makes a graph with the data in Python turtle. The code takes a datapoint every 2 minutes and prints it on the turtle screen. The problem is, while waiting for another datapoint, the turtle window doesn't respond and I can't make it active.

The turtle window works perfectly when I add turtle.done() in the end but the code doesn't continue after I close the turtle window, which I don't want, I want the window to be visible all the time.

Is there any good way to make the window "act" like with turtle.done() but it continues the loop?

If I'm understanding your needs correctly, then you might be able to use the turtle screen's ontimer() method. You want to do done() which turns control over to tkinter's event loop. But by having an ontimer() event, which reinvokes itself as the last thing it does, you can have event-friendly code constantly checking for more datapoints. Roughly:

from turtle import Turtle, Screen

def my_update():
    #  get the new data
    # ...

    # draw the new data
    turtle.forward(...)  

    # reinvoke one-shot (5 secs. from now)
    screen.ontimer(my_update, 5000)

screen = Screen()

turtle = Turtle()

# invoke one-shot (5 secs. from now)
screen.ontimer(my_update, 5000)

screen.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM