编辑代码

import tkinter as tk
import math

class FlashingHeart:
    def _init_ (self,root):
        self.root = root
        self.root.title("闪闪发光的爱心")
        self.width = 400
        self.height = 400
        self.canvas =tk.Canvas(root,
width=self.with,height=self.height,bg='black')
        self.canvas.pack()
        self.heart_color = "pink"
        self.glow_color = "red"
        self.glow_radius = 10
        self.animation_speed = 500 # in milliseconds
        self.draw_heart()
        self.animate()

    def draw_heart(self):
        self.canvas.delate("all")
        points = []
        for t in  range(0,360,5) 
            angle = math.radians(t)
            x=16*math.sin(angle)**3
            y=13*math.cos(angle)- 5*math.cos(2*angle)- 2*math.cos(3*angle) - math.cos(4*angle)
            # Scale and translate the heart 
            x=self.width / 2+x*10
            y=self.height /2-y*10
            points.append((x,y))
        #Draw the heart 
        for i in range(len(points)):
            if i == 0
                self.canvas.create_polygon(points[i],fill=self.heart_color, outline='white')
            else:
                 self.canvas.create_polygon(points[i],fill=self.heart_color,outline='white',joinstyle=tk.ROUND)
        #Create a glow effect
        self.glow=self.canvas.create_oval(
             self.width/2- 50 - self .glow_radius,
             self.height/2 - 50 - self.glow_radius,
             self.width/2+ 50+self.glow_radius,
             self.height/2 + 50+self.glow_radius,
             outline=self.glow_color,
             width=2
       )

    def animate(self):
        # Change the glow radius to create a blinking effect 
        self.glow_radius=10 if self.glow_radius == 20 else 20
        self.canvas.delate(self.glow)
        self.glow = self.canvas,create_oval(
             self.width/2- 50- self.glow_radius,
             self.height/2- 50-self.glow_radius'
             self.width/2+ 50+self.glow_radius,
             self.height/2+50 +self.glow_radius,
             outline=self.glow_color,
             width=2
        )
        self.root,after(self.animation_speed,self.animate)

if _name_=="_main_":
    root=tk.Tk()
    heart=FlashingHeart(root)
    root.mainloop()