抖音上的用python实现激励弹窗

用python实现激励弹窗

最近抖音上这种弹窗很火,简单的来说这是一个python 实现激励弹窗的代码。

它有以下特点:

运行时会播放背景音乐

有不同的颜色与激励语

淡入淡出显示动画

会先显示为心形形状

在控制台ctrl + c 可退出

如图:

源码:

复制代码
import random

import time

import tkinter as tk

import os

import threading

from pygame import mixer

import math



# 抑制libpng警告

os.environ['TK_SILENCE_DEPRECATION'] = '1'



# 播放背景音乐

def play_music():

    mixer.init()

    music_file = "background.mp3"

    if os.path.exists(music_file):

        mixer.music.load(music_file)

        mixer.music.play(-1)

    else:

        print("未找到背景音乐文件 background.mp3(可选)")



def stop_music():

    try:

        mixer.music.stop()

    except:

        pass



# 创建淡入淡出的窗口

def create_beautiful_tip(root, x=None, y=None):

    window = tk.Toplevel(root)

    window_width, window_height = 300, 120

    screen_width, screen_height = window.winfo_screenwidth(), window.winfo_screenheight()



    if x is None or y is None:

        x = random.randint(100, screen_width - window_width - 100)

        y = random.randint(100, screen_height - window_height - 100)

    window.geometry(f"{window_width}x{window_height}+{x}+{y}")

    window.resizable(False, False)

    window.attributes('-topmost', True)

    window.attributes('-alpha', 0.0)



    schemes = [

        {'bg': '#FFE4E6', 'fg': '#BE123C', 'accent': '#FB7185'},

        {'bg': '#F0F9FF', 'fg': '#0369A1', 'accent': '#0EA5E9'},

        {'bg': '#F0FDF4', 'fg': '#15803D', 'accent': '#22C55E'},

        {'bg': '#FEF7CD', 'fg': '#854D0E', 'accent': '#EAB308'},

        {'bg': '#FAF5FF', 'fg': '#7C3AED', 'accent': '#A855F7'},

    ]

    s = random.choice(schemes)

    window.configure(bg=s['bg'])



    tips = [

        '💧 记得多喝水哦~', '😊 保持微笑,好运自然来', '✨ 今天也要元气满满!',

        '🌙 早点休息别熬夜', '🍀 今天过得开心吗?', '💪 你是最棒的!',

        '🎶 听首喜欢的歌吧', '🌈 一切都会好起来', '🍫 奖励一下自己吧~'

    ]

    icons = ['💕', '✨', '🌟', '🎀', '🌷', '🌸', '🍀']



    tk.Label(window, text=random.choice(icons), bg=s['bg'], fg=s['accent'], font=('Arial', 22)).place(x=20, y=30)

    tk.Label(window, text=random.choice(tips), bg=s['bg'], fg=s['fg'],

             font=('微软雅黑', 12), wraplength=200, justify='left').place(x=60, y=35)



    tk.Button(window, text='关闭', bg=s['accent'], fg='white', font=('微软雅黑', 9),

              command=window.destroy, relief='flat').place(

        x=window_width - 60, y=window_height - 30, width=50, height=25

    )



    fade_in(window)



def fade_in(window):

    alpha = 0.0

    while alpha < 1.0:

        alpha += 0.05

        window.attributes('-alpha', alpha)

        window.update()

        time.sleep(0.03)



def heart_positions(center_x, center_y, scale=25, num_points=20):



    positions = []

    for t in [i * 2*math.pi/num_points for i in range(num_points)]:

        x = scale * 16 * math.sin(t)**3 + center_x

        y = -scale * (13*math.cos(t) - 5*math.cos(2*t) - 2*math.cos(3*t) - math.cos(4*t)) + center_y

        positions.append((int(x), int(y)))

    return positions



def main():

    root = tk.Tk()

    root.withdraw()

    root.title("温馨提示程序")



    def quit_all(event=None):

        print("🎈 程序结束,再见!")

        stop_music()

        for w in root.winfo_children():

            if isinstance(w, tk.Toplevel):

                w.destroy()

        root.destroy()



    root.bind("<space>", quit_all)



    threading.Thread(target=play_music, daemon=True).start()



    screen_width, screen_height = root.winfo_screenwidth(), root.winfo_screenheight()

    # 心形窗口先排列

    heart_pos = heart_positions(screen_width//2 - 150, screen_height//2 - 100, scale=25, num_points=20)

    for x, y in heart_pos:

        create_beautiful_tip(root, x, y)

        root.update()

        time.sleep(0.05)



    # 再随机出现窗口

    MAX_WINDOWS = 8

    for i in range(100):

        create_beautiful_tip(root)

        root.update()

        time.sleep(0.8)

        windows = [w for w in root.winfo_children() if isinstance(w, tk.Toplevel)]

        if len(windows) > MAX_WINDOWS:

            windows[0].destroy()



    root.mainloop()



if __name__ == "__main__":

    main()

运行方式:

将音频文件放到与源码同一级文件夹内,

Py 文件名 运行

如果想省事的话

可以去仓库下载YoyuDev/python-: python实现美观的抖音激励弹窗

进入文件夹直接

py .\pyTC.py 即可。。。

相关推荐
hyswl66618 小时前
2025年郑州开发小程序公司推荐
python·小程序
B站计算机毕业设计之家18 小时前
基于Python音乐推荐系统 数据分析可视化 协同过滤推荐算法 大数据(全套源码+文档)建议收藏✅
python·数据分析·推荐算法
m***D28618 小时前
JavaScript在Node.js中的内存管理
开发语言·javascript·node.js
我叫张小白。18 小时前
JavaScript现代语法梳理:ES6+核心特性详解
开发语言·javascript·typescript·es6
-森屿安年-19 小时前
LeetCode 283. 移动零
开发语言·c++·算法·leetcode
寻找华年的锦瑟19 小时前
Qt-FFmpeg案例(0基础,包含环境配置)
开发语言·qt·ffmpeg
用户7851278147019 小时前
实战解析:淘宝/天猫商品描述API(taobao.item_get_desc)接口
python
codists19 小时前
Pycharm错误:JetBrains AI URL resolution failure
python
tanxiaomi19 小时前
Spring、Spring MVC 和 Spring Boot ,mybatis 相关面试题
java·开发语言·mybatis
浮尘笔记19 小时前
Go并发编程核心:Mutex和RWMutex的用法
开发语言·后端·golang