I want to retrieve all apps' or process' names (for example: Google Chrome,...) with Python like what is being shown here (taken from Task Manager):

My question is, how to get all foreground apps' names (or executables' names) in Python?
Based on a comment on an another post, there is no way to do that beforehand, so how did Task Manager manage to get the names as shown in the image above?
I have tried using psutil but it gave me all processes' names, including the ones which are running in background (which I don't need).
import psutil
for process in psutil.process_iter():
print(process.name())
I also tried win32gui but it only gave me the windows' titles, not apps' or processes' names like I needed.
import win32gui
def callback(hwnd, _):
item=win32gui.GetWindowText(hwnd)
if item and win32gui.IsWindowVisible(hwnd):
print(item)
win32gui.EnumWindows(callback, None)
I'm expecting something like this:
>>> ["Google Chrome", "PyCharm", "Task Manager", "Windows Explorer"]
or
>>> ["chrome.exe", "pycharm64.exe", "Taskmgr.exe", "explorer.exe"]