我制作了这个 Python 程序,它使用 psutil 和 turtle 实时绘制计算机的 CPU 使用情况图。我的问题是,当乌龟撞到窗口边缘时,它会继续前进,超出视野 - 但我想让窗口向右滚动,所以乌龟会继续绘制 CPU 使用率图形,同时停留在窗口边缘。我怎样才能让乌龟保持在视线范围内?
import turtle
import psutil
import time
# HOW TO MAKE THE DOTS THAT WHEN YOU HOVER OVER THEM IT SHOWS THE PERCENT
# HOW TO MAKE IT CONTINUE SCROLLING ONCE THE LINE HITS THE END
# Set up the turtle
screen = turtle.Screen()
screen.setup(width=500, height=125)
# Set the width to the actual width, -20% for a buffer
width = screen.window_width()-(screen.window_width()/20)
# Set the height to the actual height, -10% for a buffer
height = screen.window_height()-(screen.window_height()/10)
# Create a turtle
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.penup()
# Set x_pos to the width of the window/2 (on the left edge of the window)
x_pos = -(width/2)
# Set y_pos to the height of the window/2 (on the bottom of the window)
y_pos = -(height/2)
# Goto the bottom left corner
t.goto(x_pos, y_pos)
t.pendown()
while True:
# Get the CPU %
cpu_percent = psutil.cpu_percent(interval=None)
#Make the title of the Turtle screen the CPU %
screen.title(f"CPU %: {cpu_percent}%")
#Set y_pos as the bottom of the screen, +1% of the height of the screen for each CPU %
y_pos = (-height/2)+((height/100)*cpu_percent)
# Goto the point corresponding with the CPU %
t.goto(x_pos, y_pos)
# Make a dot
t.dot(4, "Red")
# Make add 5 to x_pos, so the next time it is farther to the left
x_pos = x_pos+5