Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

window.close() does not work #48

Open
rosatheagraham opened this issue Dec 31, 2021 · 7 comments
Open

window.close() does not work #48

rosatheagraham opened this issue Dec 31, 2021 · 7 comments

Comments

@rosatheagraham
Copy link

rosatheagraham commented Dec 31, 2021

Hello, once I was able to close the choosen windows. I have changed the platforms, now I am not able to do that.

import pygetwindow as gw
window = gw.getWindowsWithTitle(title)[0]
print(window)
window.close()

the result of print(window) is below. Therefore, I understand that this is the right object to close.

<Win32Window left="10", top="10", width="945", height="1020", title="some title - Google Chrome">

but window.close() does not work. Nothing happens.

Is there any way to test it for further details?

@macdeport
Copy link

Maybe take a look and ask at PyGetWindow fork

@Kalmat
Copy link

Kalmat commented Jan 1, 2022

Hi!

I have tested your code on my own system (Windows 10) and works perfect. Check this:

import pygetwindow as gw
window = gw.getWindowsWithTitle("Chrome")[0]
print(window)
window.close()

And this is the output:

<Win32Window left="1128", top="12", width="1496", height="1084", title="Nueva pestaña - Google Chrome">

Chrome closes immediately, without any issue. Can you please share which is the content of "title" variable in your code?

Happy new year!!!

@rosatheagraham
Copy link
Author

rosatheagraham commented Jan 1, 2022

Hello and happy new year,

Actually my title is an iterable thing. But when I assign it to gw.getWindowsWithTitle() here is how I do, and how I tested step by step:
(Operating system Windows 10)

print(title)
['some title - Google Chrome', 'some title - Google Chrome']

print(iter(title))
<list_iterator object at 0x000001909D9A0B20>

print(next(iter(title)))
some title - Google Chrome

print(gw.getWindowsWithTitle(next(iter(title))))
[Win32Window(hWnd=66522), Win32Window(hWnd=328614)]

print(gw.getWindowsWithTitle(next(iter(title)))[0])
<Win32Window left="10", top="10", width="945", height="1020", title="some title - Google Chrome">

window = gw.getWindowsWithTitle(next(iter(title)))[0]
window.close() #did not work :(

Once this mysterious thing was working. I can find the right window red-bloodedly and I am able to print.
but I am not able to close.

many thanks.

note: window.minimize() does not work either

@rosatheagraham
Copy link
Author

Hello again,

Right before the relevant code above. I have placed another :

window = gw.getWindowsWithTitle(title)[0]
window.close()

and it worked for the first. But if we repeat the operation in the same code, It doesn't work for the second window. I don't know why?

For your information: I got success closing the second window by finding out the pid and killing it..

@Kalmat
Copy link

Kalmat commented Jan 2, 2022

Hi again!

I think the problem is that the code runs too fast, not leaving enough time for the Window Manager to refresh the list of open windows. Therefore, it's retrieving the first window again (though it was just closed). You can either wait a little bit (time.sleep(something), but not a good strategy anyway because "something" value is unknown), store previous windows ID's and discard them (not using [0], I mean, but looping until finding a new one), or change the strategy. Check this:

import pygetwindow as gw

titles = ["Presentación de PowerPoint - Google Chrome", "Nueva pestaña - Google Chrome"]
windows = gw.getAllWindows()
for win in windows:
    if win.title in titles:
        win.close()

Your code was not working for me either. This other alternative closes both windows.

In addition to that, this is not correct:

print(gw.getWindowsWithTitle(next(iter(title)))[0])

If you try this as a very simple example:

titles = ["Presentación de PowerPoint - Google Chrome", "Nueva pestaña - Google Chrome"]
print(next(iter(titles)))
print(next(iter(titles)))

This would be the output:

Presentación de PowerPoint - Google Chrome
Presentación de PowerPoint - Google Chrome

This is because you are creating a totally new iter() object every single time. I am not sure if you actually need an iter() object (you can check my code, in which you can loop over an iterable object just using "for... in..."). If you already need it, then create the iter() object once, and use next() alone, like this:

titles = ["Presentación de PowerPoint - Google Chrome", "Nueva pestaña - Google Chrome"]
title = iter(titles)
print(next(title))
print(next(title))

@SpawnTerror
Copy link

SpawnTerror commented Dec 30, 2022

Hello. I just tried to use the module, but it doesn't work fully.
import pygetwindow as gw

activeWin = gw.getWindowsWithTitle('L-Connect 3')
print(activeWin)
for w in activeWin:
    print(w)
activeWin[0].activate() # works
activeWin[0].minimize() # doesn't do anything

Console output:
[Win32Window(hWnd=66694)] <Win32Window left="544", top="282", width="960", height="540", title="L-Connect 3">

@Kalmat
Copy link

Kalmat commented Dec 31, 2022

Hi! Not sure what can be happening in your case. Is 'L-Connect 3' a, let's say, normal/regular/standard window?

In the meantime, I would suggest you give a try to PyWinCtl module (a fork from PyGetWindow module):

python3 -m pip install pywinctl

then, try this:

import pywinctl as pwc

activeWin = pwc.getWindowsWithTitle('L-Connect 3')
print(activeWin)   # You can also print any window property you are interested in, besides the object itself
for w in activeWin:
    print(w)  # Same here
activeWin[0].activate()  # Be aware activeWin can be empty, so this (activeWin[0]) may fail
activeWin[0].minimize()  # Same here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants