Search code examples
pywin32

How can I use win32gui in order to have an app run in the background?


I was able to find a great piece of code from here on how to create a Tray app using the win32gui module.

However, that app runs based on a function called notify which only runs when the mouse is moving over the icon.

How can I make the app do something constantly in a loop, regardless of what the user does?

(In other words, I want to use win32gui in a non-event-based way.)


Solution

  • Instead of using win32gui.PumpMessages (which runs its own loop) in the code, replace it with the following loop:

    while True:
        your_function()
        win32gui.PumpWaitingMessages()
    

    This will allow you to run your own functions in the program loop.