当前位置:首页 / 文章测试 /

开始打字练习

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)

声明:以上文章均为用户自行发布,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。