写在前面:

最近不是有一个很火的就是用python实现各种很奈斯的画面和视频嘛 (结尾附上

比如如下这种:

所以今天碰见我算是有福了 

其实原理就是 用Python 自带的各种库来实现各种 功能

比如第二个 就是用 turtle (海龟  一个经常用来画图的库)

有些童鞋可能要问了 煮包煮包 是不是你把代码复制给我我就能运行了啊

nnno这肯定是不行的 但是也不难 要实现如上功能

首先 你电脑上要有 python的编译器   比如 pythonPycharm 等

其次还要有python运行的环境

这样应该就差不多了

代码如下(第一个是爱心的代码、第二个是库洛米的):

import threading
import time
import random
import math
import tkinter as tk
from tkinter import ttk
import ctypes
# 启用DPI感知(Windows 8.1及以上)
ctypes.windll.shcore.SetProcessDpiAwareness(1)
# 屏幕尺寸
SCREEN_W, SCREEN_H = 0, 0
try:
    root = tk.Tk()
    root.withdraw()
    SCREEN_W = root.winfo_screenwidth()
    SCREEN_H = root.winfo_screenheight()
    root.destroy()
except:
    SCREEN_W, SCREEN_H = 1920, 1080

# 窗口大小
WINDOW_W, WINDOW_H = 300, 100
desired_points = 135  # 心形点数,可调整
POPUP_DURATION = 30000  # 弹窗持续时间:30000毫秒 = 30秒(可自定义修改)


def generate_heart_points(num_points, screen_w, screen_h, window_w, window_h):
    points = []
    center_x = screen_w // 2
    center_y = screen_h // 2
    for i in range(num_points):
        t = i / num_points * 2 * 3.14159
        x = 16 * (pow(math.sin(t), 3))
        y = 13 * math.cos(t) - 5 * math.cos(2 * t) - 2 * math.cos(3 * t) - math.cos(4 * t)
        y = -y  # 倒爱心:翻转y轴
        scale = min(screen_w // 40, screen_h // 40)
        x = center_x + x * scale
        y = center_y + y * scale
        x = max(window_w // 2, min(x, screen_w - window_w // 2))
        y = max(window_h // 2, min(y, screen_h - window_h // 2))
        points.append((int(x), int(y)))
    return points


def show_warn_tip(x, y, w, h):
    root = tk.Tk()
    root.overrideredirect(False)  # 隐藏标题栏
    root.geometry(f"{w}x{h}+{x - w // 2}+{y - h // 2}")
    root.attributes('-topmost', True)  # 窗口置顶

    # 随机暖色系背景
    r = random.randint(255, 255)
    g = random.randint(100, 220)
    b = random.randint(180, 255)
    bg_color = f'#{r:02x}{g:02x}{b:02x}'
    root.configure(bg=bg_color)

    # 随机提示语
    tips = [
        "万事顺遂", "平安喜乐", "前程似锦", "岁岁长安康", "皆如意",
        "岁岁无忧", "好运常在", "如愿以偿", "喜乐常伴",'顺心遂意',
        "温暖长存", "星辰予你", "人间值得", "爱意常伴", "浪漫常在",
        '温暖向阳','日日开心','平安胜意'
    ]
    tip = random.choice(tips)
    label = ttk.Label(root, text=tip, font=('楷体', 15), background=bg_color,anchor=tk.CENTER)
    label.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)

    # 1. 自动关闭:持续时间由 POPUP_DURATION 控制(默认30秒)
    root.after(POPUP_DURATION, root.destroy)

    # 2. 手动关闭:点击弹窗任意位置立即关闭(保留灵活操作)
    def close_window(event):
        root.destroy()

    root.bind('<Button-1>', close_window)
    label.bind('<Button-1>', close_window)

    root.mainloop()


if __name__ == "__main__":
    points = generate_heart_points(desired_points, SCREEN_W, SCREEN_H, WINDOW_W, WINDOW_H)
    threads = []
    for (x, y) in points:
        t = threading.Thread(target=show_warn_tip, args=(x, y, WINDOW_W, WINDOW_H))
        threads.append(t)
        t.start()
        time.sleep(0.10)  # 弹窗启动间隔

    # 主线程等待:确保所有弹窗都能完整显示(可根据弹窗持续时间调整)
    hold_seconds = POPUP_DURATION / 1000  # 与弹窗持续时间同步
    time.sleep(hold_seconds)
import turtle
t = turtle.Turtle()
t.pensize(6)
t.shape("turtle")
def go(x, y):
   t.penup()
   t.goto(x, y)
   t.pendown()
def arco(direc, radio, ang):
   t.seth(direc)
   t.circle(radio, ang)
def linea(direc, longitud):
   t.seth(direc)
   t.forward(longitud)
t.fillcolor("#FCCEE8")
go(0, -247.1)
t.begin_fill()
arco(20.36, 691.05, 11.95)
arco(32.32, 449.10, 21.35)
arco(53.67, 189.23, 33.86)
arco(87.53, 166.84, 43.91)
arco(131.44, 157.09, 55.54)
arco(186.98, 170.49, 23.54)
arco(210.52, 168.51, 19.4)
arco(126.98, 145.8, 50.92)
arco(173.26, 150.22, 30.06)
arco(201.03, 149.74, 34.63)
arco(235.67, 171.19, 45.49)
arco(281.16, 207.82, 33.32)
arco(314.49, 794.09, 7.75)
arco(322.24, 459.17, 21.06)
t.end_fill()
t.fillcolor("White")
go(73.23, -214.99)
t.begin_fill()
arco(163.49, 627.97, 18.11)
arco(326.31, 459.17, 16.99)
arco(20.36, 691.05, 6.63)
t.end_fill()
t.fillcolor("DimGray")
go(99.40, -226.50)
t.begin_fill()
arco(139, -48.32, 50.15)
arco(14.46, 128.19, 37.1)
arco(51.56, 159.44, 18.32)
linea(71.56, 247.65)
linea(195.96, 207.19)
arco(166.06, 254.29, 24.46)
linea(72.02, 30.06)
linea(143.9, 163.79)
linea(264.99, 169.15)
linea(342.02, 47.17)
arco(228.53, 120.63, 38.68)
arco(268.69, 121.66, 42.52)
arco(309.84, 116.09, 34.63)
arco(207.82, -110.52, 21.35)
arco(318.27, 105.68, 63.69)
arco(308.98, 92.44, 38.37)
arco(338.61, 15.57, 131.99)
arco(329.81, 85.95, 30.88)
t.end_fill()
go(-158.49, 215.53)
t.begin_fill()
arco(90, 20.60, 360)
t.end_fill()
go(279.24, 139.34)
t.begin_fill()
arco(90, 20.02, 360)
t.end_fill()
go(-52.55, 80.83)
arco(191.8, 159.09, 40.28)
t.fillcolor("White")
go(82.79, -189.06)
t.begin_fill()
arco(35.09, 79.28, 38.6)
arco(73.69, 64.9, 44.09)
arco(117.78, 113.74, 29.67)
linea(190.87, 70.26)
linea(146.15, 64.93)
arco(181.27, 88.94, 58.4)
arco(239.67, 71.35, 64.82)
arco(323.19, 116.09, 21.28)
arco(338.52, 354.68, 18.88)
arco(357.41, 173.46, 18.86)
t.end_fill()
go(69.32, -208.48)
t.begin_fill()
arco(13.63, 22.14, 28.31)
arco(41.94, 16.48, 62.87)
arco(104.82, 26.94, 79.99)
arco(184.8, 23.49, 62.17)
arco(246.98, 10.94, 37)
arco(219.19, 35.9, 56.76)
arco(275.95, 21.42, 107.07)
arco(23.02, 28.65, 71.65)
t.end_fill()
go(-53.12, -197.84)
t.begin_fill()
arco(132.47, 36.97, 68.64)
arco(201.11, 20.81, 66.42)
arco(267.52, 23.96, 54.71)
arco(322.24, 39.92, 38.52)
arco(0.75, 44.02, 11.68)
arco(305.21, 23.97, 99.62)
arco(44.83, 25.60, 75.76)
arco(120.59, 21.51, 71.12)
arco(191.71, 61.90, 6.56)
t.end_fill()
t.pensize(4)
t.fillcolor("HotPink")
go(112.62, -227.01)
t.begin_fill()
arco(90, 12.68, 360)
t.end_fill()
go(-112.52, -181.80)
t.begin_fill()
arco(90, 13.31, 360)
t.end_fill()
go(170.48, 9.50)
t.begin_fill()
arco(102.64, 33.95, 52.5)
arco(68.94, 87.55, 68.95)
arco(194.31, 147.68, 49.77)
arco(307.13, 123.37, 49.17)
arco(308.03, 35.71, 32.22)
arco(256.48, 102.57, 65.72)
arco(22.18, 197.39, 37.53)
arco(129.16, 112.97, 57.69)
t.end_fill()
go(168.96, 1.79)
t.begin_fill()
arco(35.66, 9.23, 100.96)
linea(136.62, 19.47)
arco(136.62, 9.93, 86.96)
linea(223.59, 27.30)
arco(223.59, 8.32, 88.9)
linea(312.49, 18.86)
arco(312.49, 7.18, 83.17)
linea(35.66, 30.54)
t.end_fill()
go(155.77, -7.67)
arco(0.87, -28.57, 51.74)
arco(309.13, -7.26, 184.55)
go(132.21, 19.92)
arco(92.29, 40.59, 33.52)
arco(125.81, 7.59, 185.88)
go(29.08, -53.52)
t.begin_fill()
linea(84.74, 9.98)
arco(3.92, 35.49, 34.2)
arco(38.13, 36, 70.95)
arco(109.07, 41.45, 5.24)
arco(114.32, 51.75, 55.11)
arco(169.42, 60.44, 32.8)
arco(202.22, 39.93, 70.47)
arco(272.7, 27.91, 5.53)
arco(278.22, 36.56, 57.83)
linea(261.83, 11)
linea(351.83, 46.03)
t.end_fill()
go(12.74, -51.17)
linea(77.61, 11.31)
go(-1.04, -49.19)
linea(81.83, 11.10)
t.fillcolor("#FCCEE8")
go(0.40, -141.70)
t.begin_fill()
arco(86.7, 10.56, 149.31)
arco(236, 7.20, 84.79)
arco(320.79, 13.28, 71.66)
arco(32.45, 5.09, 54.24)
t.end_fill()
t.fillcolor("Black")
go(45.54, -30.92)
t.begin_fill()
arco(43.07, 16.28, 96.69)
arco(139.76, 7.80, 101.6)
arco(241.36, 18.74, 71.73)
arco(313.09, 6.88, 89.98)
t.end_fill()
t.pensize(2)
go(-11.21, -22.47)
t.begin_fill()
arco(37.74, 22.11, 55.04)
arco(92.78, 7.59, 117.47)
arco(210.25, 21.72, 56.76)
arco(267, 8.45, 130.74)
t.end_fill()
t.pensize(6)
go(-63.72, -111.51)
arco(131.71, 54.75, 21.68)
arco(153.39, 25.41, 62.04)
t.pensize(3)
go(-31.98, -147.93)
arco(286.4, 21.10, 126.17)
go(80.86, -100.18)
t.begin_fill()
linea(196.62, 29.58)
linea(266.06, 19.99)
arco(266.06, 17.41, 91.11)
arco(357.17, 10.91, 44.8)
arco(41.98, 21.64, 21.44)
arco(63.42, 43.37, 51.67)
t.end_fill()
go(-103.61, -99.39)
arco(135.91, -20.79, 34.61)
go(-95.19, -96.57)
arco(129.2, -20.79, 32.12)
go(84.11, -110.09)
arco(21.92, 14.98, 41.48)
go(81.34, -101.25)
arco(22.65, 21.73, 35.42)
go(-29.87, -147.03)
linea(201.87, 5.22)
go(2.33, -153.22)
linea(328.69, 5.59)
t.hideturtle()
turtle.done()

还想要其他的,比如

或者是这种

(可以三连后 私信找我领取哦~ )

整理不易,头发掉了几根。觉得有收获的话,点个赞关注一下呗? 我保证,我的主页里还有更多让你头发比你掉得还多的好东西!

写在最后:

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐