【问题标题】:How to get a list of the name of every open window and place that into dataframe?如何获取每个打开窗口的名称列表并将其放入数据框中?
【发布时间】:2020-11-02 18:29:41
【问题描述】:

所以我尝试同时使用 win32gui 和 Pandas 来获取打开的窗口的数据框 (df)。下面是我写的。我最终得到一个错误。我怎样才能返回一个数据帧?

# info http://timgolden.me.uk/pywin32-docs/win32gui__EnumWindows_meth.html

import win32gui
import pandas as pd

def winEnumHandler( hwnd, dfx ):
    if win32gui.IsWindowVisible( hwnd ) and len(win32gui.GetWindowText( hwnd ))>0 :
    idv = hex(hwnd)
    winv = win32gui.GetWindowText(hwnd)
    df = pd.DataFrame({'ID' : idv , 'Window': winv}, index = ['0'])

    frames  = [dfx, df]
    dfx = pd.concat(frames)
    # print(dfx) 
    return dfx # Comment out this and it runs but not the result I want.

dfx= pd.DataFrame() # empty dataframe
win32gui.EnumWindows( winEnumHandler, dfx )
print(dfx)

追溯

Traceback (most recent call last):
  File "c:\Users\s...\Python\List of windows.py", line 19, in <module>
    win32gui.EnumWindows( winEnumHandler, dfx )
TypeError: an integer is required (got type DataFrame)

【问题讨论】:

    标签: python pandas win32gui


    【解决方案1】:

    因此,从函数中获取数据帧的关键是使用全局变量。这个变量在函数内部必须是全局的,这样就不会混淆,python 也不认为它是局部变量。这是代码。

    import win32gui
    import pandas as pd
    
    dfx = pd.DataFrame() 
    i = 0
    
    def winEnumHandler( hwnd, x ):
        global dfx, i
    
        if win32gui.IsWindowVisible( hwnd ) and len(win32gui.GetWindowText( hwnd ))>0 :
            idv = hex(hwnd)
            winv = win32gui.GetWindowText(hwnd)
            df = pd.DataFrame({'ID' : idv , 'Window': winv}, index = [i])
            frames  = [dfx, df]
            dfx = pd.concat(frames)
            i += 1
    
    win32gui.EnumWindows( winEnumHandler, i )
    print(dfx)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-19
      • 2021-06-22
      • 2022-12-18
      • 2018-07-29
      • 2020-05-09
      • 1970-01-01
      • 2019-10-31
      • 1970-01-01
      相关资源
      最近更新 更多