| Honker.org.cn红盟网讯 很多手机应用经常会弹出一些通知,从而霸占用户使用体验,会时不时让用户看一眼。而其实如果你开发的软件,让Windows也弹一下广告会怎样呢? 
 上代码 | import tkinter as tk from tkinter import font
 import pystray
 from PIL import Image, ImageTk
 import time
 import random
 from threading import Thread
 
 def notify(quote_content):
 icon = pystray.Icon("name", Image.new('RGBA', (100, 100), (255, 255, 255, 0)), "软件名称")
 icon.title = "图标标题"
 icon.menu = pystray.Menu(pystray.MenuItem("关闭", lambda: icon.stop()))
 icon.visible = True
 icon.notify(quote_content)
 time.sleep(5)
 icon.visible = False
 
 def start_notification():
 custom_quote = entry_quote.get().strip()
 
 if custom_quote:
 while True:
 Thread(target=notify, args=(custom_quote,)).start()
 time.sleep(int(entry_time.get()))
 else:
 quotes_list = [
 "生命在于不断探索与追求",
 "保持好奇心,永不止步于学习",
 "选择适合自己的道路,坚定前行",
 "平凡中寻找不凡,活出精彩人生",
 "相信自己,定能实现理想抱负",
 "时光如梭,好好珍惜当下",
 "不被他人期望束缚,做最好的自己",
 "每个人都应认真对待自己的人生",
 "生活不仅有眼前的苟且,还有诗和远方",
 "一个人的故事,就是他的人生轨迹"
 ]
 while True:
 rand_item = random.choice(quotes_list)
 Thread(target=notify, args=(rand_item,)).start()
 time.sleep(int(entry_time.get()))
 
 root = tk.Tk()
 root.title("Windows任务栏消息通知")
 root.geometry("460x300")
 root.configure(bg="#F0F0F0")
 
 
 root.resizable(False, False)
 
 # 设置字体
 title_font = font.Font(family="Microsoft YaHei", size=16, weight="bold")
 label_font = font.Font(family="Microsoft YaHei", size=12)
 button_font = font.Font(family="Microsoft YaHei", size=12, weight="bold")
 
 #标题
 label_title = tk.Label(root, text="Windows任务栏消息通知", font=title_font, bg="#F0F0F0", fg="#333333")
 label_title.pack(pady=20)
 
 #自定义格言输入框
 label_quote = tk.Label(root, text="输入自定义通知,为空则使用默认通知:", font=label_font, bg="#F0F0F0", fg="#333333")
 label_quote.pack(pady=10)
 
 entry_quote = tk.Entry(root, font=label_font, width=30, bg="#FFFFFF", fg="#333333")
 entry_quote.pack(pady=10)
 
 #时间间隔设置
 label_time = tk.Label(root, text="设置时间间隔每", font=label_font, bg="#F0F0F0", fg="#333333")
 label_time.pack(pady=10, side=tk.LEFT)
 
 entry_time = tk.Entry(root, font=label_font, width=10, bg="#FFFFFF", fg="#333333")
 entry_time.insert(0, "20")
 entry_time.pack(pady=10, side=tk.LEFT)
 
 label_seconds = tk.Label(root, text="(秒)提醒一次", font=label_font, bg="#F0F0F0", fg="#333333")
 label_seconds.pack(pady=10, side=tk.LEFT)
 
 # 开始按钮
 button_start = tk.Button(root, text="启动程序", font=button_font, bg="#4CAF50", fg="white", command=start_notification)
 button_start.pack(pady=20, side=tk.LEFT)
 
 root.mainloop()
 | 
   首先,我们导入了必要的库和模块: 1.tkinter:用于创建GUI界面的库。 2.font:用于设置字体样式。 3.pystray:用于在任务栏中显示通知的库。 4.PIL:用于处理图像的库。 5.time:用于进行时间相关的操作。 6.random:用于生成随机数。 7.threading:用于创建多线程。 然后,我们定义了一个名为notify的函数,用于显示通知: 1.首先,我们创建了一个pystray.Icon对象,用于在任务栏中显示图标和通知。 2.我们设置了图标的标题和菜单,并将图标设置为可见。 3.使用notify方法显示通知,其中包括通知的内容。 4.然后,我们使用time.sleep方法暂停程序执行一段时间,以确保通知显示完整。 5.最后,我们将图标设置为不可见。 接下来,我们定义了一个名为start_notification的函数,用于启动通知程序: 1.首先,我们从输入框中获取用户输入的自定义通知内容。 2.如果用户输入了自定义内容,我们创建一个无限循环,并在每次循环中创建一个新的线程,将自定义内容作为参数传递给notify函数,并使用time.sleep方法暂停一段时间。 3.如果用户没有输入自定义内容,我们定义了一个包含多个引用的列表,并从中随机选择一个引用作为通知内容。然后,我们创建一个无限循环,并在每次循环中创建一个新的线程,将随机选择的引用作为参数传递给notify函数,并使用time.sleep方法暂停一段时间。 接下来,我们使用Tkinter创建了一个窗口,并设置了窗口的标题、大小和背景颜色。 然后,我们定义了一些字体样式,包括标题字体、标签字体和按钮字体。 接着,我们添加了标题标签和自定义通知输入框。 然后,我们添加了时间间隔设置的标签、输入框和单位标签。 接着,我们添加了一个启动按钮,并为其绑定了点击事件的处理函数。 最后,我们启动了Tkinter的事件循环,以等待用户与程序进行交互。   |