Victor Domingos
Victor Domingos

Reputation: 1093

How to display a system notification in Python?

What is the best way to display system notifications with Python, preferably using tkinter for cross platform implementation (I am on OS X, so I'm also open to implementations that would allow integration with Notification Center)?

I want to show a self-destroying message, I mean something that would remain on screen for a few seconds and then would vanish way, but without interfering with user interaction. I don't want to use a messagebox because in requires the user to click in a button to dismiss the message window.

What do you recommend?

Upvotes: 4

Views: 9278

Answers (2)

SomeGenericDev
SomeGenericDev

Reputation: 651

This is what I came up with

import tkinter as tk

def notifyTkInter(message, timeoutSeconds = 10):
    root = tk.Tk()
    root.after(timeoutSeconds * 1000, lambda: root.destroy())
    root.geometry('400x100+1500+900')
    lbl = tk.Message(root, fg='black', border=0, text=message, width=400, font=("Arial", 13))
    lbl.pack()
    root.mainloop()
    
notifyTkInter("Hello World")

Upvotes: 0

Marichyasana
Marichyasana

Reputation: 3154

This works for me. It shows the message as a popup and exits after 2 seconds.

from tkinter import *
from sys import exit
def popupError(s):
    popupRoot = Tk()
    popupRoot.after(2000, exit)
    popupButton = Button(popupRoot, text = s, font = ("Verdana", 12), bg = "yellow", command = exit)
    popupButton.pack()
    popupRoot.geometry('400x50+700+500')
    popupRoot.mainloop()

Upvotes: 7

Related Questions