【问题标题】:Get the name of the current opened application in pythonpython获取当前打开的应用程序名称
【发布时间】:2022-12-30 05:19:11
【问题描述】:

我想获取当前使用python打开的应用程序名称。这是我的代码

import win32.win32gui as win32gui
w = win32gui
window_name = w.GetWindowText(w.GetForegroundWindow())
window_name = str(window_name)
start_time = datetime.now().replace(microsecond=0)
while True:
    new_window_name = w.GetWindowText(w.GetForegroundWindow())
    if window_name!=new_window_name:
        stop_time = datetime.now().replace(microsecond=0)
        print(f"{window_name} Time Spent: {stop_time-start_time}")
        window_name =new_window_name
        start_time = datetime.now().replace(microsecond=0)

有了这个,我只得到应用程序标题而不是应用程序名称,所以我怎么得到它。

【问题讨论】:

  • 进程没有名称。你到底在找什么?

标签: python windows time-management


【解决方案1】:

使用 psutil 可以获得进程名称。

import psutil
import win32process, win32gui

hwnd = win32gui.GetForegroundWindow()
_,pid = win32process.GetWindowThreadProcessId(hwnd)
process = psutil.Process(pid)

process_name = process.name()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-25
    • 2016-03-27
    • 2010-11-24
    • 2013-01-01
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    相关资源
    最近更新 更多