抖音上的用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 即可。。。

相关推荐
Serendipity_Carl6 小时前
爬虫数据清洗可视化链家房源
python·pandas·matplotlib
行走在电子领域的工匠6 小时前
2.2 常用控件
开发语言·python
天才测试猿6 小时前
Selenium三大等待详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
散峰而望6 小时前
Dev-C++一些问题的处理
c语言·开发语言·数据库·c++·编辑器
进击的大海贼7 小时前
QT/C++ 消息定时管理器
开发语言·c++·qt
lly2024067 小时前
TypeScript 基础类型
开发语言
whm27777 小时前
Visual Basic 菜单编辑器
开发语言·visual studio
梨轻巧7 小时前
pyside6安装:下载python、配置环境变量、vscode安装和测试pyside6、可能遇到的错误、pycharm 安装pyside6
python
kyle~7 小时前
原子性与原子操作
运维·服务器·开发语言·c++