Search code examples
pythonaudiotkinterwindowmemory-efficient

Python - notify user every time a loop yields an output Tkinter?


I receive API data every 5 minutes and run a Python code to check if the incoming data fulfills certain conditions. The data is in JSON format, but I convert it to a data frame and perform my checks there. In case one of the checks generates a hit, I'd like to get notified. I'd like to find a fast and resource-preserving way for Python to notify me.

  • I think I can't use "print()" as I won't be watching Python all the time.
  • I could use "data.to_excel", but I'd still have to have a window of my folder open and check manually if a new excel file appears.
  • I read that Tkinter is widely used to generate a new window so I guess that's the way to go.

What would be the most efficient (minimalistic) code to generate a window (preferably one with an audio sound when it appears) with one line of text, e.g. "Condition1/2/3/4 fulfilled, timestamp, string"

Thank you for your kind help


Solution

  • Use messagebox could be very easy.A minimal example:

    from tkinter import messagebox
    import time
    import tkinter as tk
    
    root = tk.Tk()
    info = "what"
    root.withdraw()
    if condition:
        messagebox.showinfo("title",f"Condition-1 fulfilled time:{time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime())} \n info:{info}")
    

    if condition is True,it will show a small window(it will play the default system sound,and no need to use winsound.Beep()).