效果图:
完整代码:
import tkinter as tk
import random
import math
from tkinter.constants import *width = 888
height = 500
heartx = width / 2
hearty = height / 2
side = 11class Star:def __init__(self, canvas, x, y, size):self.canvas = canvasself.x = xself.y = yself.size = sizeself.star_id = Nonedef draw(self):if self.star_id is not None:self.canvas.delete(self.star_id)# 计算星星的五个点points = []for i in range(5):angle = math.radians(i * 144) # 星星的角度x = self.x + self.size * math.cos(angle)y = self.y + self.size * math.sin(angle)points.append((x, y))# 创建空心星星self.star_id = self.canvas.create_polygon(points, outline='yellow', fill='', width=2)def update(self):# 随机散列和抖动scatter_x = random.uniform(-5, 5) # 散列范围scatter_y = random.uniform(-5, 5)self.x += scatter_xself.y += scatter_yself.draw()class StarAnimation:def __init__(self, root):self.canvas = tk.Canvas(root, width=800, height=600, bg='black')self.canvas.pack()# 创建一颗大星星self.star = Star(self.canvas, 400, 300, 200) # 在(400, 300)位置绘制大小为100的星星self.animate()tk.Label(root, text="Dynamic Hollow Star Animation", bg="black", fg="#FF99CC", font="Helvetic 25 bold").place(relx=.5, rely=.5,anchor=CENTER)def animate(self):self.star.update()if __name__ == '__main__':root = tk.Tk()root.title("Dynamic Hollow Star Animation")animation = StarAnimation(root)root.resizable(0, 0)root.wm_attributes("-toolwindow", 1)root.mainloop()screenwidth = root.winfo_screenwidth()screenheight = root.winfo_screenheight()widths = 300heights = 100x = (screenwidth - widths) / 2y = (screenheight - heights) / 2 - 66root.destroy()