返回列表 发布新帖

用python表白, 程序员的浪漫

3531 0
小K网牛逼 发表于 2026-4-7 00:00:05 | 查看全部 阅读模式 <

马上注册,结交更多好友,享用更多功能,让你轻松玩转小K网。

您需要 登录 才可以下载或查看,没有账号?立即注册 微信登录

×

用python表白, 程序员的浪漫

用python表白, 程序员的浪漫

python用来表白也是很可以的,很浪漫哦。之前弄了一版我这边没有设置分组结果关闭的时候崩溃死了,后来加上了一个关闭操作。一键关闭所有的。
  1. import tkinter as tk
  2. import random
  3. import threading
  4. import time
  5. from queue import Queue
  6. # -------------------------- 弹窗参数 --------------------------
  7. FIRST_WIN_WIDTH = 300
  8. FIRST_WIN_HEIGHT = 140
  9. FIRST_WIN_BG = '#f0f8ff'  # 外层背景色
  10. FIRST_FRAME_BG = '#fff0f5'  # 内层框架色
  11. FIRST_TEXT = 'Hi,有点想你了!'
  12. FIRST_TEXT_FONT = ('微软雅黑', 18, 'bold')
  13. FIRST_TEXT_COLOR = '#d85c8a'
  14. BUTTON_TEXT = '我也想你了'
  15. BUTTON_FONT = ('微软雅黑', 14)
  16. BUTTON_BG = '#ffccd5'
  17. BUTTON_FG = '#a62639'
  18. # 随机提示窗参数
  19. TIP_WIN_WIDTH = 250
  20. TIP_WIN_HEIGHT = 60
  21. TIP_COUNT = 250  # 弹窗总数
  22. TIP_INTERVAL = 0.04  # 弹窗间隔(秒),控制总时长
  23. # 提示文字与颜色列表
  24. TIP_TEXT_LIST = [
  25.     '多喝水哦~', '保持微笑呀', '元气满满', '记得吃水果', '保持好心情',
  26.     '好好爱自己', '我想你了', '偷个小懒', '期待见面', '天冷加衣',
  27.     '顺顺利利', '早点休息', '烦恼消失', '别熬夜', '开心吗',
  28.     '多穿衣服', '你超棒', '别焦虑', '加油', '你很特别',
  29.     '会好的', '要自信', '笑一个', '摸鱼快乐'
  30. ]
  31. TIP_BG_COLORS = [
  32.     'skyblue', 'lightgreen', 'lavender', 'lightyellow', 'plum',
  33.     'coral', 'bisque', 'aquamarine'
  34. ]
  35. # -------------------------- 核心逻辑实现 --------------------------
  36. class TipWindowManager:
  37.     """提示窗口管理器,统一处理窗口创建逻辑"""
  38.     def __init__(self):
  39.         self.queue = Queue()  # 线程通信队列
  40.         self.root = None  # 主线程Tk实例
  41.         self.all_tip_windows = []  # 记录所有创建的提示窗口,用于统一关闭
  42.         self.producer_done = False  # 标记生产者线程是否完成
  43.         self.final_popup_shown = False  # 标记最终弹窗是否已显示
  44.     def create_tip_window(self, params):
  45.         """在主线程创建单个提示窗口"""
  46.         x, y = params
  47.         window = tk.Toplevel(self.root)  # 绑定到主线程TK实例
  48.         window.title('嘻嘻~')
  49.          
  50.         # 将窗口加入列表,方便后续关闭
  51.         self.all_tip_windows.append(window)
  52.         # 确保窗口在屏幕内
  53.         screen_width = window.winfo_screenwidth()
  54.         screen_height = window.winfo_screenheight()
  55.         x = max(0, min(x, screen_width - TIP_WIN_WIDTH))
  56.         y = max(0, min(y, screen_height - TIP_WIN_HEIGHT))
  57.         window.geometry(f"{TIP_WIN_WIDTH}x{TIP_WIN_HEIGHT}+{int(x)}+{int(y)}")
  58.         # 随机内容与样式
  59.         tip_text = random.choice(TIP_TEXT_LIST)
  60.         bg_color = random.choice(TIP_BG_COLORS)
  61.         tk.Label(
  62.             window,
  63.             text=tip_text,
  64.             bg=bg_color,
  65.             font=('微软雅黑', 16),
  66.             width=30,
  67.             height=3
  68.         ).pack()
  69.         window.attributes('-topmost', True)
  70.     def process_queue(self):
  71.         """主线程循环处理队列中的窗口创建请求"""
  72.         # 处理队列中剩余的窗口创建请求
  73.         while not self.queue.empty():
  74.             params = self.queue.get()
  75.             self.create_tip_window(params)
  76.             self.queue.task_done()
  77.          
  78.         # 检查:生产者线程完成 + 队列为空 + 未显示最终弹窗 → 显示最终弹窗
  79.         if self.producer_done and self.queue.empty() and not self.final_popup_shown:
  80.             self.show_final_popup()
  81.             self.final_popup_shown = True
  82.         else:
  83.             # 继续监听,直到满足条件
  84.             self.root.after(100, self.process_queue)
  85.     def start_tip_producer(self, screen_width, screen_height):
  86.         """启动子线程生成随机窗口参数"""
  87.         def producer():
  88.             # 生成所有弹窗参数
  89.             for _ in range(TIP_COUNT):
  90.                 x = random.randrange(0, screen_width - TIP_WIN_WIDTH)
  91.                 y = random.randrange(0, screen_height - TIP_WIN_HEIGHT)
  92.                 self.queue.put((x, y))
  93.                 time.sleep(TIP_INTERVAL)
  94.             
  95.             # 生产者线程完成,标记状态
  96.             self.producer_done = True
  97.         # 启动守护线程(主程序退出时自动结束)
  98.         producer_thread = threading.Thread(target=producer, daemon=True)
  99.         producer_thread.start()
  100.     def start_main_loop(self, screen_width, screen_height):
  101.         """启动主线程TK循环"""
  102.         self.root = tk.Tk()
  103.         self.root.withdraw()  # 隐藏主窗口(仅用于事件循环)
  104.         self.process_queue()  # 启动队列处理
  105.         self.start_tip_producer(screen_width, screen_height)
  106.         self.root.mainloop()
  107.     def show_final_popup(self):
  108.         """显示最终的提示弹窗,点击后关闭所有窗口"""
  109.         # 创建最终弹窗
  110.         final_win = tk.Toplevel(self.root)
  111.         final_win.title('结束啦~')
  112.         final_win.attributes('-topmost', True)  # 置顶显示,确保能看到
  113.          
  114.         # 弹窗居中
  115.         screen_width = final_win.winfo_screenwidth()
  116.         screen_height = final_win.winfo_screenheight()
  117.         win_width = 280
  118.         win_height = 120
  119.         x = int((screen_width - win_width) / 2)
  120.         y = int((screen_height - win_height) / 2)
  121.         final_win.geometry(f"{win_width}x{win_height}+{x}+{y}")
  122.         final_win.configure(bg='#f0f8ff')
  123.         # 内层框架
  124.         frame = tk.Frame(
  125.             final_win,
  126.             bg='#fff0f5',
  127.             relief=tk.RIDGE,
  128.             bd=3
  129.         )
  130.         frame.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
  131.         # 核心提示文字
  132.         tk.Label(
  133.             frame,
  134.             text='我想你了,你可以关闭了',
  135.             bg='#fff0f5',
  136.             font=('微软雅黑', 18, 'bold'),
  137.             fg='#d85c8a',
  138.             pady=10
  139.         ).pack()
  140.         # 关闭所有窗口的函数
  141.         def close_all_windows():
  142.             # 1. 关闭所有随机提示窗口
  143.             for win in self.all_tip_windows:
  144.                 try:
  145.                     win.destroy()
  146.                 except Exception:
  147.                     pass  # 忽略已关闭的窗口
  148.             
  149.             # 2. 关闭最终弹窗
  150.             final_win.destroy()
  151.             
  152.             # 3. 关闭主窗口,退出程序
  153.             self.root.quit()
  154.             self.root.destroy()
  155.         # 关闭按钮(醒目设计)
  156.         tk.Button(
  157.             frame,
  158.             text='关闭所有',
  159.             font=('微软雅黑', 14, 'bold'),
  160.             bg='#ff6b8b',
  161.             fg='white',
  162.             relief=tk.RAISED,
  163.             padx=20,
  164.             pady=5,
  165.             cursor='hand2',
  166.             command=close_all_windows
  167.         ).pack(pady=5)
  168. def create_first_popup(manager):
  169.     """创建第一阶段带淡入淡出的问候弹窗"""
  170.     first_win = tk.Tk()
  171.     first_win.overrideredirect(True)  # 无标题栏
  172.     first_win.attributes('-topmost', True)
  173.     first_win.attributes('-alpha', 0)  # 初始透明
  174.     first_win.configure(bg=FIRST_WIN_BG)
  175.     # 居中定位
  176.     screen_width = first_win.winfo_screenwidth()
  177.     screen_height = first_win.winfo_screenheight()
  178.     x = int((screen_width - FIRST_WIN_WIDTH) / 2)
  179.     y = int((screen_height - FIRST_WIN_HEIGHT) / 2)
  180.     first_win.geometry(f"{FIRST_WIN_WIDTH}x{FIRST_WIN_HEIGHT}+{x}+{y}")
  181.     # 内层框架(模拟边框)
  182.     frame = tk.Frame(
  183.         first_win,
  184.         bg=FIRST_FRAME_BG,
  185.         relief=tk.RIDGE,
  186.         bd=3
  187.     )
  188.     frame.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
  189.     # 问候文字
  190.     tk.Label(
  191.         frame,
  192.         text=FIRST_TEXT,
  193.         bg=FIRST_FRAME_BG,
  194.         font=FIRST_TEXT_FONT,
  195.         fg=FIRST_TEXT_COLOR,
  196.         pady=10
  197.     ).pack()
  198.     # 淡入动画(500ms完成)
  199.     def fade_in():
  200.         current_alpha = first_win.attributes('-alpha')
  201.         if current_alpha < 1.0:
  202.             first_win.attributes('-alpha', current_alpha + 0.1)
  203.             first_win.after(50, fade_in)
  204.     # 淡出动画(500ms完成)
  205.     def fade_out():
  206.         current_alpha = first_win.attributes('-alpha')
  207.         if current_alpha > 0:
  208.             first_win.attributes('-alpha', current_alpha - 0.1)
  209.             first_win.after(50, fade_out)
  210.         else:
  211.             first_win.destroy()
  212.             # 启动提示窗口逻辑
  213.             manager.start_main_loop(screen_width, screen_height)
  214.     # 按钮点击事件
  215.     tk.Button(
  216.         frame,
  217.         text=BUTTON_TEXT,
  218.         font=BUTTON_FONT,
  219.         bg=BUTTON_BG,
  220.         fg=BUTTON_FG,
  221.         relief=tk.FLAT,
  222.         padx=15,
  223.         pady=5,
  224.         cursor='hand2',
  225.         command=fade_out
  226.     ).pack(pady=5)
  227.     fade_in()  # 启动淡入
  228.     first_win.mainloop()
  229. if __name__ == "__main__":
  230.     # 初始化管理器并启动程序
  231.     tip_manager = TipWindowManager()
  232.     create_first_popup(tip_manager)
复制代码

回复

您需要登录后才可以回帖 登录 | 立即注册 微信登录

本版积分规则

您需要 登录 后才可以回复,轻松玩转社区,没有帐号?立即注册
快速回复
关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表