You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现turtle触达窗口边缘时窗口自动右滚,保持绘图可见?

问题

我用Python的psutil库和turtle库写了一个CPU使用率实时绘图程序,现在遇到个问题:当绘图的turtle碰到窗口边缘时,会继续跑到视野外面去。我想实现窗口自动向右滚动,让turtle保持在窗口边缘的同时继续完成CPU使用率的绘图,请问怎么让turtle始终保持在视野里?

原代码:

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
解决方案

核心思路是通过动态调整turtle的世界坐标系,让屏幕的可视范围跟着turtle的位置平移,从而实现自动滚动效果。以下是修改后的完整代码:

import turtle
import psutil

# 窗口基础设置
screen = turtle.Screen()
screen.setup(width=500, height=125)
screen.tracer(0)  # 关闭自动刷新,避免滚动时画面闪烁

# 计算带缓冲的有效绘图区域
width = screen.window_width() - (screen.window_width() / 20)
height = screen.window_height() - (screen.window_height() / 10)

# 初始化世界坐标系:左、下、右、上边界
left_bound = -width / 2
right_bound = width / 2
bottom_bound = -height / 2
top_bound = height / 2
screen.setworldcoordinates(left_bound, bottom_bound, right_bound, top_bound)

# 初始化绘图turtle
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.penup()
x_pos = left_bound
y_pos = bottom_bound
t.goto(x_pos, y_pos)
t.pendown()

# 滚动触发阈值:当turtle距离右边缘小于50像素时启动滚动
scroll_threshold = 50

while True:
    cpu_percent = psutil.cpu_percent(interval=None)
    screen.title(f"CPU %: {cpu_percent}%")
    
    # 计算当前CPU使用率对应的y坐标
    y_pos = bottom_bound + ((height / 100) * cpu_percent)
    t.goto(x_pos, y_pos)
    t.dot(4, "Red")
    
    # 判断是否需要滚动屏幕
    if x_pos >= right_bound - scroll_threshold:
        # 向右平移坐标系,平移步长和x_pos每次增加的步长一致
        shift = 5
        left_bound += shift
        right_bound += shift
        screen.setworldcoordinates(left_bound, bottom_bound, right_bound, top_bound)
    
    x_pos += 5
    screen.update()  # 手动刷新屏幕,保证画面流畅

关键改动说明

  • 用screen.setworldcoordinates()动态修改坐标系,让可视区域跟随turtle右移
  • 开启screen.tracer(0)并手动调用screen.update(),解决滚动时的画面闪烁问题
  • 设置scroll_threshold阈值,提前触发滚动,确保turtle始终处于窗口可视范围内

内容的提问来源于stack exchange,提问作者Kovy Jacob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 15:22:48