生活, 其他记录

Python弹窗小应用源码

这里根据网上的资料和 AI 整理,给出两个 Python 病毒式弹窗的小应用源码。

版本一(关怀信息)

"""
This code is supported by the website: https://www.guanjihuan.com
The newest version of this code is on the web page: https://www.guanjihuan.com/archives/47900
"""

import tkinter as tk
import random
import threading
import time

class WarmTipApp:
    def __init__(self):
        self.total_windows = 150
        self.created_count = 0
        self.windows = []
        self.root = tk.Tk()
        self.root.withdraw()
        self.lock = threading.Lock()

    def create_window(self, is_final=False):
        def _create():
            window = tk.Toplevel(self.root)
            screen_width = self.root.winfo_screenwidth()
            screen_height = self.root.winfo_screenheight()
            
            if is_final:
                window_width, window_height = 300, 100
                x = (screen_width - window_width) // 2
                y = (screen_height - window_height) // 2
                bg = 'lightpink'
                
                frame = tk.Frame(window, bg=bg)
                frame.pack(fill='both', expand=True)
                
                tk.Label(frame, text='我想你了', bg=bg, font=('微软雅黑', 20, 'bold'),
                        fg='red').place(relx=0.5, rely=0.5, anchor='center')
                
                window.protocol("WM_DELETE_WINDOW", lambda: self.quit_app(window))
                window.title('特别提示')
            else:
                window_width, window_height = 300, 100
                x = random.randint(0, max(0, screen_width - window_width))
                y = random.randint(0, max(0, screen_height - window_height))
                
                tips = [
                    # 健康相关
                    '多喝水哦,补充水分很重要',
                    '记得按时吃饭,别饿肚子啦',
                    '久坐了要起来活动活动哦',
                    '晚上别熬夜,早点休息呀',
                    '今天也要记得吃水果呀',
                    '天气变凉了,注意添衣服',
                    '保持良好作息,身体才会棒',
                    '累了就歇一歇,别硬撑呀',
                    
                    # 情绪与心态
                    '保持微笑呀,你笑起来很好看',
                    '每天都要元气满满哦',
                    '保持好心情,好运会降临',
                    '不管怎样,好好爱自己最重要',
                    '别给自己太大压力,慢慢来',
                    '遇到不开心的事,记得跟我说',
                    '今天过得开心吗?要多笑笑呀',
                    '烦恼都会过去的,别太在意',
                    
                    # 祝福与期待
                    '愿你每天都有小确幸',
                    '梦想一定会成真的',
                    '期待下一次见面呀',
                    '祝你事事顺顺利利',
                    '愿你被世界温柔以待',
                    '今天也要加油呀,你最棒',
                    '无论在哪,都有人惦记着你',
                    '愿所有美好都如期而至',
                    
                    # 生活细节
                    '出门记得带钥匙和手机呀',
                    '雨天记得带伞,别淋湿了',
                    '开车要注意安全,慢慢来',
                    '记得给家里打个电话呀',
                    '有空多出去走走,晒晒太阳',
                    '今天也要认真生活呀',
                    '记得整理房间,心情会变好'
                ]
                bg_colors = ['lightpink', 'skyblue', 'lightgreen', 'lavender', 'lightyellow', 'plum', 'coral', 'bisque', 'aquamarine', 'mistyrose']
                
                tip = random.choice(tips)
                bg = random.choice(bg_colors)
                
                frame = tk.Frame(window, bg=bg)
                frame.pack(fill='both', expand=True)
                
                tk.Label(frame, text=tip, bg=bg, font=('微软雅黑', 16),
                        fg='black').place(relx=0.5, rely=0.5, anchor='center')
                
                window.title('温馨提示')
                self.windows.append(window)

            window.geometry(f'{window_width}x{window_height}+{x}+{y}')
            window.attributes('-topmost', True)
            
            if not is_final:
                with self.lock:
                    self.created_count += 1
                    if self.created_count == self.total_windows:
                        threading.Thread(target=self.close_all_windows, daemon=True).start()

        self.root.after(0, _create)

    def close_all_windows(self):
        time.sleep(0.5)
        for window in self.windows[:]:
            self.root.after(0, window.destroy)
            time.sleep(0.01)
        self.windows.clear()
        self.root.after(0, lambda: self.create_window(is_final=True))

    def quit_app(self, window):
        window.destroy()
        self.root.quit()

    def start(self):
        def _create_all():
            for _ in range(self.total_windows):
                self.create_window()
                time.sleep(0.1)

        threading.Thread(target=_create_all, daemon=True).start()
        self.root.mainloop()

if __name__ == "__main__":
    app = WarmTipApp()
    app.start()

版本二(主席语录)

"""
This code is supported by the website: https://www.guanjihuan.com
The newest version of this code is on the web page: https://www.guanjihuan.com/archives/47900
"""

import tkinter as tk
import random
import threading
import time

class WarmTipApp:
    def __init__(self):
        self.total_windows = 150
        self.created_count = 0
        self.windows = []
        self.root = tk.Tk()
        self.root.withdraw()
        self.lock = threading.Lock()

    def create_window(self, is_final=False):
        def _create():
            window = tk.Toplevel(self.root)
            screen_width = self.root.winfo_screenwidth()
            screen_height = self.root.winfo_screenheight()
            
            if is_final:
                window_width, window_height = 350, 150
                x = (screen_width - window_width) // 2
                y = (screen_height - window_height) // 2
                bg = 'lightpink'
                
                frame = tk.Frame(window, bg=bg)
                frame.pack(fill='both', expand=True)
                
                tk.Label(frame, text='人民万岁!', bg=bg, font=('微软雅黑', 28, 'bold'),
                        fg='red').place(relx=0.5, rely=0.5, anchor='center')
                
                window.protocol("WM_DELETE_WINDOW", lambda: self.quit_app(window))
                window.title('人民万岁')
            else:
                window_width, window_height = 350, 150
                x = random.randint(0, max(0, screen_width - window_width))
                y = random.randint(0, max(0, screen_height - window_height))
                
                # 毛主席语录
                quotes = [
                    # 革命与斗争
                    "枪杆子里面出政权。",
                    "星星之火,可以燎原。",
                    "下定决心,不怕牺牲,排除万难,去争取胜利。",
                    "人不犯我,我不犯人;人若犯我,我必犯人。",
                    "一切反动派都是纸老虎。",
                    "宜将剩勇追穷寇,不可沽名学霸王。",

                    # 政治与领导
                    "我们的原则是党指挥枪,而决不容许枪指挥党。",
                    "谁是我们的敌人?谁是我们的朋友?这个问题是革命的首要问题。",
                    "统一战线,武装斗争,党的建设,是中国共产党在中国革命中战胜敌人的三个法宝。",
                    "政策和策略是党的生命,各级领导同志务必充分注意,万万不可粗心大意。",
                    "我们应该谦虚、谨慎、戒骄、戒躁,全心全意地为中国人民服务。",

                    # 军事与战略
                    "敌进我退,敌驻我扰,敌疲我打,敌退我追。",
                    "打得赢就打,打不赢就走。",
                    "战争的伟力之最深厚的根源,存在于民众之中。",
                    "集中优势兵力,各个歼灭敌人。",
                    "伤其十指,不如断其一指。",
                    "兵民是胜利之本。",
                    "战略上藐视敌人,战术上重视敌人。",

                    # 思想与方法
                    "没有调查,就没有发言权。",
                    "实践、认识、再实践、再认识,这种形式,循环往复以至无穷,而实践和认识之每一循环的内容,都比较地进到了高一级的程度。",
                    "矛盾存在于一切事物的发展过程中;每一事物的发展过程中存在着自始至终的矛盾运动。",
                    "马克思主义的哲学认为十分重要的问题,不在于懂得了客观世界的规律性,因而能够解释世界,而在于拿了这种对于客观规律性的认识去能动地改造世界。",
                    "世上无难事,只要肯登攀。",
                    "人的正确思想,只能从社会实践中来,只能从社会的生产斗争、阶级斗争和科学实验这三项实践中来。",
                    "一切结论产生于调查情况的末尾,而不是在它的先头。",
                    "知识的问题是一个科学问题,来不得半点的虚伪和骄傲,决定地需要的倒是其反面——诚实和谦逊的态度。",

                    # 群众与服务
                    "为人民服务。",
                    "军民团结如一人,试看天下谁能敌。",
                    "我们共产党人好比种子,人民好比土地。我们到了一个地方,就要同那里的人民结合起来,在人民中间生根、开花。",
                    "人民,只有人民,才是创造世界历史的动力。",

                    # 文化与教育
                    "古为今用,洋为中用。",
                    "百花齐放,百家争鸣。",
                    "好好学习,天天向上。",

                    # 经济与建设
                    "自己动手,丰衣足食。",
                    "只有社会主义能够救中国。",

                    # 青年与未来
                    "世界是你们的,也是我们的,但是归根结底是你们的。你们青年人朝气蓬勃,正在兴旺时期,好像早晨八九点钟的太阳。希望寄托在你们身上。",
                    "自信人生二百年,会当水击三千里。",

                    # 哲理与励志
                    "不管风吹浪打,胜似闲庭信步。",
                    "要想不经过艰难曲折,不付出极大努力,总是一帆风顺,容易得到成功,这种想法,只是幻想。",
                    "读书是学习,使用也是学习,而且是更重要的学习。",
                    "我们的同志在困难的时候,要看到成绩,要看到光明,要提高我们的勇气。",
                    "错误和挫折教训了我们,使我们比较地聪明起来了,我们的事情就会办得好一些。"
                ]
                bg_colors = ['lightyellow', 'lightblue', 'lightgreen', 'lavender', 'mistyrose', 'bisque', 'aquamarine']
                
                tip = random.choice(quotes)
                bg = random.choice(bg_colors)
                
                frame = tk.Frame(window, bg=bg)
                frame.pack(fill='both', expand=True)
                
                tk.Label(frame, text=tip, bg=bg, font=('微软雅黑', 14),
                        fg='black', wraplength=320, justify='center').place(relx=0.5, rely=0.5, anchor='center')
                
                window.title('毛主席语录')
                self.windows.append(window)

            window.geometry(f'{window_width}x{window_height}+{x}+{y}')
            window.attributes('-topmost', True)
            
            if not is_final:
                with self.lock:
                    self.created_count += 1
                    if self.created_count == self.total_windows:
                        threading.Thread(target=self.close_all_windows, daemon=True).start()

        self.root.after(0, _create)

    def close_all_windows(self):
        time.sleep(0.5)
        for window in self.windows[:]:
            self.root.after(0, window.destroy)
            time.sleep(0.01)
        self.windows.clear()
        self.root.after(0, lambda: self.create_window(is_final=True))

    def quit_app(self, window):
        window.destroy()
        self.root.quit()

    def start(self):
        def _create_all():
            for _ in range(self.total_windows):
                self.create_window()
                time.sleep(0.1)

        threading.Thread(target=_create_all, daemon=True).start()
        self.root.mainloop()

if __name__ == "__main__":
    app = WarmTipApp()
    app.start()
32 次浏览

【说明:本站主要是个人的一些笔记和代码分享,内容可能会不定期修改。为了使全网显示的始终是最新版本,这里的文章未经同意请勿转载。引用请注明出处:https://www.guanjihuan.com

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

Captcha Code