效果图:
完整代码:
import tkinter as tk
import random
import math
from tkinter.constants import *
width = 888
height = 500
heartx = width / 2
hearty = height / 2
side = 11
class Star:
def __init__(self, canvas, x, y, size):
self.canvas = canvas
self.x = x
self.y = y
self.size = size
self.star_id = None
def 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_x
self.y += scatter_y
self.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 = 300
heights = 100
x = (screenwidth - widths) / 2
y = (screenheight - heights) / 2 - 66
root.destroy()