3

How can I get the code to call a function when a button is pressed, and get input from a textbox from a pywinrt toast?

I am making a python library that can make windows toast notifications easier to make in python.

The Code:

import winrt.windows.ui.notifications as notifications
import winrt.windows.ui.notifications.management as listener
import winrt.windows.data.xml.dom as dom

#create notifier
nManager = notifications.ToastNotificationManager
notifier = nManager.create_toast_notifier(r"C:\Users\Admin\AppData\Local\Programs\Python\Python38\python.exe")

#define your notification as
tString = """
<toast>
    <visual>
        <binding template='ToastGeneric'>
            <text>Hi!</text>
            <text>I am a toast.</text>
        </binding>
    </visual>
    <actions>
        <input id="textBox" type="text" placeHolderContent="Type a reply"/>
        <action
            content="send message"
            arguments="action=reply&amp;convId=01"
            activationType="background"
            hint-inputId="textBox"/>
        <action
            content="OK"
            arguments="action=viewdetails&amp;contentId=02"
            activationType="foreground"/>
    </actions>
</toast>
"""

#convert notification to an XmlDocument
xDoc = dom.XmlDocument()
xDoc.load_xml(tString)

#display notification
notifier.show(notifications.ToastNotification(xDoc))

Thanks!

1 Answer 1

0

The buttons can be used like this:

#importing required modules
import winrt.windows.ui.notifications as notifications
import winrt.windows.data.xml.dom as dom
from time import sleep
import sys

# get python path
path = sys.executable

# create notification objects
nManager = notifications.ToastNotificationManager
notifier = nManager.create_toast_notifier(path)

# define the xml notification document.
tString = """
<toast>
    <visual>
        <binding template='ToastGeneric'>
            <text>Another Message from Tim!</text>
            <text>Hi there!</text>
        </binding>
    </visual>

    <actions>
        <action
           content="View Message"
           arguments="test1"
           activationType="backround"/>
    </actions>
</toast>
"""

# load the xml document.
xDoc = dom.XmlDocument()
xDoc.load_xml(tString)

notification = notifications.ToastNotification(xDoc)

# display notification
notifier.show(notification)

# this is not called on the main thread.
def handle_activated(sender, _):
    print([sender, _])
    print('Button was pressed!')

# add the activation token.
activated_token = notification.add_activated(handle_activated)

# do something else on the main thread.
while True:
    sleep(1)

Where I learnt about this: this issue. Also answered: here.

Sign up to request clarification or add additional context in comments.

1 Comment

But what about text input?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.