เราสามารถสร้างตัวแจ้งเตือนสำหรับเหตุการณ์ที่เกิดขึ้นใน Windows โดยใช้ Python นี่เป็นเรื่องง่ายมากด้วยโมดูล win10toast หากคุณคุ้นเคยกับ Toast ใน Android การเข้าใจ Toast notifications ด้วย Python จะเป็นเรื่องง่ายมาก เราสามารถสร้างการแจ้งเตือนทุกครั้งที่มีเหตุการณ์เกิดขึ้นเป็นการเตือน มาดูกัน
รันคำสั่งต่อไปนี้ใน คอมมานด์ไลน์ เพื่อติดตั้งโมดูล win10toast
pip install win10toast
ถ้าโมดูลติดตั้งสำเร็จ คุณจะได้ผลลัพธ์ดังนี้เมื่อรันคำสั่ง
Collecting win10toast Downloading https://files.pythonhosted.org/packages/d4/ba/95c0ea87d9bcad68b90d8cb130a313b939c88d8338a2fed7c11eaee972fe/win10toast-0.9-py2.py3-none-any.whl Collecting pypiwin32 (from win10toast) Downloading https://files.pythonhosted.org/packages/d0/1b/2f292bbd742e369a100c91faa0483172cd91a1a422a6692055ac920946c5/pypiwin32-223-py3-none-any.whl Requirement already satisfied: setuptools in c:\users\hafeezulkareem\anaconda3\lib\site-packages (from win10toast) (40.8.0) Requirement already satisfied: pywin32>=223 in c:\users\hafeezulkareem\anaconda3\lib\site-packages (from pypiwin32->win10toast) (223) Installing collected packages: pypiwin32, win10toast Successfully installed pypiwin32-223 win10toast-0.9
ขั้นตอนการสร้าง Toast Notification
นำเข้าคลาส ToastNotifier จาก win10toast
สร้างอินสแตนซ์ของคลาส
เรียกใช้เมธอด show_toast('title', 'message', duration=time_in_sec, icon_path='path to .ico file') พร้อมอาร์กิวเมนต์ที่ต้องการ
คุณจะได้ True เป็นผลลัพธ์หลังจากระยะเวลาการแจ้งเตือนครบ ถ้าสำเร็จ
มาดูตัวอย่างง่ายๆ
ตัวอย่าง
## โปรแกรมสร้าง Toast Notifier แบบง่าย
from win10toast import ToastNotifier
## สร้างอินสแตนซ์
notifier = ToastNotifier()
## เรียกเมธอด show_toast() พร้อมอาร์กิวเมนต์ที่ต้องการ
notifier.show_toast("Sample Notification", "You are learning at Tutorialspoint", duration=25, icon_path="globe.ico")
ถ้าคุณรันโปรแกรมข้างต้น คุณจะได้ผลลัพธ์ดังนี้

คุณสามารถเพิ่มโปรแกรมแจ้งเตือนนี้เมื่อมีเหตุการณ์เกิดขึ้นบนระบบของคุณ หากคุณมีข้อสงสัยเกี่ยวกับบทช่วยสอนนี้ กรุณาเขียนในส่วนความคิดเห็น