-2

I am trying to return the output of all windows locations based on names, which I have done.

However for some reason I can't get the results returned as 1 line, a list of tuples etc. I am getting the output returned in separate lines (which I then can't use as a whole).

Code:

import win32gui as win

win_name = "SomeApplication"
def get_all_windows_locs(hwnd, lparam):
    if win_name in win.GetWindowText(hwnd):
        windows_locs = win.GetWindowRect(hwnd)
        print(windows_locs)


win.EnumWindows(get_all_windows_locs, None)

Output:

(2, 2, 1162, 803)
(768, 1112, 1928, 1913)
(1421, 635, 2581, 1436)

I really want to have this returned as an actual list of tuples. Something along the lines of this:

(2, 2, 1162, 803), (768, 1112, 1928, 1913), (1421, 635, 2581, 1436)

The reason I want it returned in 1 line is that if I call it outside the function it only returns the last line from the output.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    well it seems `win.GetWindowRect` doesn't return stuff like that. Why not collect the outputs and put them in a list yourself? – SuperStew Jul 07 '21 at 13:52
  • 1
    `print(windows_locs)` adds a newline. As mentioned in the comment above, put the data in a list and return that. – 001 Jul 07 '21 at 13:54

1 Answers1

0

You can collect all entries by including a global (external to your function) variable. Here, a list all_windows_locs:

import win32gui as win

all_windows_locs = []

win_name = "SomeApplication"
def get_all_windows_locs(hwnd, lparam):
    if win_name in win.GetWindowText(hwnd):
        windows_locs = win.GetWindowRect(hwnd)
        all_windows_locs.append(windows_locs)

win.EnumWindows(get_all_windows_locs, None)
print(all_windows_locs)

An even better solution, to avoid polluting the global namespace with variables, could capture the list in a wrapper function:


def all_windows_locs(win_name):
    windows_locs_list = []

    def test_window(hwnd, lparam):
        if win_name in win.GetWindowText(hwnd):
            windows_locs = win.GetWindowRect(hwnd)
            windows_locs_list.append(windows_locs)
    
    win.EnumWindows(test_window, None)
    return windows_locs_list

print(all_windows_locs("SomeApplication"))
loa_in_
  • 1,030
  • 9
  • 20
  • Ahhh! I was trying to append it before but I must of missed something, this works perfectly! Thank you! – christianjthomas Jul 07 '21 at 14:01
  • Why use a global variable here? A local variable that gets returned would be a much better choice. Using a global variable this way is even bad practice. – Thierry Lathuille Jul 07 '21 at 14:13
  • each time `get_all_windows_locs(hwnd, lparam)` is called, the local vars are created anew, so a local variable can't hold state between calls. I can imagine `EnumWindows` has an argument to pass arbitrary data to the callback function. I didn't look into that though. – loa_in_ Jul 07 '21 at 14:37
  • @ThierryLathuille one can also wrap `get_all_windows_locs(hwnd, lparam)` in another function to capture the list and avoid polluting the global namespace, but that would be overcomplicating the answer – loa_in_ Jul 07 '21 at 14:39