การแจ้งเตือนแบบโต้ตอบ windows 10 โดยใช้ python

Sep 13 2020

ฉันต้องการทราบว่าเป็นไปได้หรือไม่ที่จะแสดงการแจ้งเตือนด้วย windows 10 ซึ่งจะดำเนินการเฉพาะเมื่อกด ฉันรู้plyerและwin10toastแต่ในทั้งสองโมดูลนั้นฉันไม่เห็นวิธีกำหนดค่าให้ทำงานกับการคลิก ฉันต้องการเพื่อให้เมื่อฉันกดลิงก์จะเปิดขึ้นหรือฟังก์ชันบางอย่างจะถูกเรียกใช้เพื่อใช้งานtkinterได้ คำถามที่นี่เกี่ยวกับ SO เกี่ยวกับเรื่องนี้ยังไม่มีคำตอบหรือเข้ากันไม่ได้ในขณะนี้ พวกคุณมีความคิดบ้างไหม?

ตัวอย่างข้อมูล 1:

from win10toast import ToastNotifier

noti = ToastNotifier()
noti.show_toast('Random','Hello World',icon_path='ico.ico',duration=60)

นอกจากนี้แม้ว่าจะduration=60ปิดทางใดทางหนึ่งโดยอัตโนมัติหลังจากประมาณ 5 วินาที

ตัวอย่างข้อมูล 2:

from plyer.utils import platform
from plyer import notification

notification.notify(
    title='Here is the title',
    message='Here is the message',
    app_name='Here is the application name',
    app_icon='path/to/the/icon.' + ('ico' if platform == 'win' else 'png')
)

หลังจากการวิจัยบางอย่างฉันพบว่าwin10toastใช้ได้ผลwin32guiและการกำหนดค่าบางอย่างอาจทำให้ใช้งานได้ แต่ฉันไม่แน่ใจว่าอะไร

ขอบคุณล่วงหน้า

คำตอบ

1 Karthik Sep 13 2020 at 13:10

คุณสามารถใช้callback_on_clickวิธีการโทรกลับเมื่อคลิกที่การแจ้งเตือน

ดังที่ได้กล่าวในหัวข้อนี้ติดตั้ง callback_on_clickคุณจะต้องติดตั้งpip install -e git+https://github.com/Charnelx/Windows-10-Toast-Notifications.git#egg=win10toastเป็นยังไม่รวมกับcallback_on_clickwintoast

คุณจะได้รับข้อผิดพลาด แต่เวอร์ชันใหม่ของคุณtoastNotifierจะถูกคัดลอกด้วยเวอร์ชันที่มีอยู่

คุณจะต้องนำเข้าfrom src.win10toast.win10toast import ToastNotifierแทนการเรียงfrom win10toast import ToastNotifierลำดับเพื่อเข้าถึงcallback_on_clickวิธีการ

โปรดตรวจสอบตัวอย่างข้อมูล

from src.win10toast.win10toast import ToastNotifier
import tkinter as tk
from tkinter import *
import webbrowser

top = tk.Tk()
top.geometry('50x50')
top.title('shop')

url = 'http://www.google.com'

def notify():
   noti.show_toast('Random','Hello World',duration=60, threaded=True,callback_on_click=action) 

def action():
    webbrowser.open_new(url)
    print("Done")
    
noti = ToastNotifier() 
btn = Button(top, text= "Enter", command=notify)
btn.pack()

top.mainloop()

NOTE- ให้แน่ใจว่าคุณใช้threaded=Trueในshow_toast()เพื่อให้ส่วนที่เหลือของโปรแกรมของคุณจะได้รับอนุญาตให้ดำเนินการในขณะที่ขนมปังยังคงใช้งาน มิฉะนั้น gui ของคุณจะหยุด