J'ai créé ce programme Python qui représente graphiquement l'utilisation du processeur d'un ordinateur en temps réel à l'aide de Psutil et de la tortue. Mon problème est que lorsque la tortue atteint le bord de la fenêtre, elle continue de se déplacer, hors de vue - mais je veux que la fenêtre défile à droite, afin que la tortue puisse continuer à représenter graphiquement la consommation du processeur tout en restant au bord de la fenêtre. Comment garder la tortue en vue ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
Ce blog de scaler a recommandé d'utiliser un design Flappy Bird pour résoudre le problème. L'oiseau semble voler vers l'avant dans ce jeu, mais la mise en œuvre est telle que les tuyaux se déplacent du côté droit de l'écran vers la gauche et l'oiseau ne bouge pas du tout sur l'axe des x. Est-ce correct?