Skip to content Skip to sidebar Skip to footer

Access Variable Inside An Non-return Function From Other Package Without Global Variable

Is there a method to access a function result, which I have applied another function onto it. For example: win32gui.EnumWindows(a_function, extra) The EnumWindows will iterate all

Solution 1:

EnumWindows doesn't return anything. The way results are usually retrieved from it is often to make the callback function store data in a global. There's an example of doing that in this question.

Another way is to pass a mutable container object (such as a list) as the extra argument, which will then be passed to the callback as its second argument each time it's called (the first argument is a window handle).

Here's an example using the second technique which passes a local list object to EnumWindows() which the callback function modifies, but only if the window is visible.

import win32gui

defmy_callback(hwnd, list_object):
    if win32gui.IsWindowVisible(hwnd):
        title = win32gui.GetWindowText(hwnd)
        if title:
            list_object.append(title)

defprint_windows_titles():
    my_list = []  # local variable

    win32gui.EnumWindows(my_callback, my_list)  # populates my_list# print result of calling EnumWindowsprint('Titles of Visible Windows:')
    for window_title in my_list:
        print('  {!r}'.format(window_title))

print_windows_titles()

Post a Comment for "Access Variable Inside An Non-return Function From Other Package Without Global Variable"