python: 如何轻松显示闪烁的实心圆圈

python: how to easily display blinking filled circle

我是 python 的新手。如果我的 vpn 隧道已关闭,我试图得到一个闪烁的红色圆圈(指示器),如果我的 vpn 是 up.However,我试图得到一个稳定的绿色圆圈(指示器),我目前被困在构建这个闪烁的红灯中。

我试过:

#!/usr/bin/env python
import turtle

turtle.setup(100,150)

t = turtle.Turtle()
t.speed(0)
while True:
    #Python program to draw color filled circle in turtle programming
    t.begin_fill()
    t.fillcolor('red')
    t.circle(25)
    t.end_fill()
    t.begin_fill()
    t.fillcolor('white')
    t.circle(25)
    t.end_fill()
turtle.done()

几乎就完成了,只是画圆需要 "long" 时间。还有其他更好的方法吗?顺便说一句,有没有可能得到一个透明的背景?

您可以 fiddle 使用 turtle.speed 命令。

设置 t.speed(0) 会导致快速闪烁。

Pygame 做到了:

#!/usr/bin/env python

import pygame
import time

WHITE =     (255, 255, 255)
RED =       (255,   0,   0)
(width, height) = (40, 40)

background_color = WHITE

pygame.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("VPN-Status")
screen.fill(background_color)
pygame.display.update()

while True:
    pygame.draw.circle(screen, RED, (20, 20), 20)
    pygame.display.update()
    time.sleep(0.25)
    pygame.draw.circle(screen, WHITE, (20, 20), 20)
    pygame.display.update()
    time.sleep(0.25)

让我们尝试一种不同的方法,使用 ontimer() 事件来控制闪烁速度并闪烁一个圆形海龟而不是每次都重新绘制:

from turtle import Screen, Turtle

CURSOR_SIZE = 20

def blink():
    pen, fill = turtle.color()
    turtle.color(fill, pen)
    screen.ontimer(blink, 250)  # 1/4 second blink

screen = Screen()

turtle = Turtle()
turtle.hideturtle()
turtle.shape('circle')
turtle.shapesize(50 / CURSOR_SIZE)
turtle.color('red', 'white')
turtle.showturtle()

blink()

screen.exitonclick()