Windows notification with button using python

2024/10/7 4:37:05

I need to make a program that alerts me with a windows notification, and I found out that this can be simply done with the following code.

I don't care what library I use

from win10toast import ToastNotifier
toast = ToastNotifier()
toast.show_toast("alert","text")

This code gives that following alert

enter image description here

However, I want there to be a button on the notification so I can click it and it will lead me to a url.

enter image description here

Like this example.

Is this possible?

I just found this website about toast contents can anyone help me use this with python?

Answer

This type of behavior is not supported in the currently released version of Windows-10-Toast-Notifications. However, a contributor created a pull request that adds functionality for a callback_on_click parameter that will call a function when the notification is clicked.

This has yet to be merged into the master branch, and given how long it's been since the library has been updated, I wouldn't count on it happening anytime soon. However, you can still install this modified version of the library to make use of this feature:

  • First, you'll need to uninstall the current version of win10toast from your environment (e.g., pip uninstall win10toast).
  • Next, you'll need to install the modified version (e.g., pip install git+https://github.com/Charnelx/Windows-10-Toast-Notifications.git#egg=win10toast).

Then, you can create a toast like this:

toast.show_toast(title="Notification", msg="Hello, there!", callback_on_click=your_callback_function)

A complete working example:

from win10toast import Toasttoast = ToastNotifier()
toast.show_toast(title="Notification", msg="Hello, there!", callback_on_click=lambda: print("Clicked!"))

When you click on the notification, you should see "Clicked!" appear in the Python console.

Important: This will only work if you're using the modified version of the library I mentioned above. Otherwise you will get the error: TypeError: show_toast() got an unexpected keyword argument 'callback_on_click'.

https://en.xdnf.cn/q/70283.html

Related Q&A

numpy IndexError: too many indices for array when indexing matrix with another

I have a matrix a which I create like this:>>> a = np.matrix("1 2 3; 4 5 6; 7 8 9; 10 11 12")I have a matrix labels which I create like this:>>> labels = np.matrix("1;0…

how to use enum in swig with python?

I have a enum declaration as follows:typedef enum mail_ {Out = 0,Int = 1,Spam = 2 } mail;Function:mail status; int fill_mail_data(int i, &status);In the function above, status gets filled up and wi…

What does except Exception as e mean in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…

Numpy efficient big matrix multiplication

To store big matrix on disk I use numpy.memmap.Here is a sample code to test big matrix multiplication:import numpy as np import timerows= 10000 # it can be large for example 1kk cols= 1000#create some…

Basic parallel python program freezes on Windows

This is the basic Python example from https://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.pool on parallel processingfrom multiprocessing import Pooldef f(x):return x*xif __na…

Formatting multiple worksheets using xlsxwriter

How to copy the same formatting to different sheets of the same Excel file using the xlsxwriter library in Python?The code I tried is: import xlsxwriterimport pandas as pd import numpy as npfrom xlsxw…

Good python library for generating audio files? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

Keras ConvLSTM2D: ValueError on output layer

I am trying to train a 2D convolutional LSTM to make categorical predictions based on video data. However, my output layer seems to be running into a problem:"ValueError: Error when checking targe…

debugging argpars in python

May I know what is the best practice to debug an argpars function.Say I have a py file test_file.py with the following lines# Script start import argparse import os parser = argparse.ArgumentParser() p…

Non-blocking server in Twisted

I am building an application that needs to run a TCP server on a thread other than the main. When trying to run the following code:reactor.listenTCP(ServerConfiguration.tcpport, TcpCommandFactory()) re…