5

I'm looking for a way to set the active window. The only solutions I found are outdated. They included modules like pywinauto but its focus() function doesn't work.

I need something that quickly switches/makes another window active.

I tried the code below, but it doesn't work as it says object has no attribute 'focus'

import pygetwindow as gw

win = gw.getWindowsWithTitle('Photoshop')[0]
win.focus()

I am using Windows 8

Khloe
  • 157
  • 1
  • 2
  • 6

2 Answers2

4

I think the object method you're after is activate:

>>> help(win.activate)
Help on method activate in module 
pygetwindow._pygetwindow_win:

activate() method of 
pygetwindow._pygetwindow_win.Win32Window instance
    Activate this window and make it the foreground window.

So changing your code as follows should work.

import pygetwindow as gw

win = gw.getWindowsWithTitle('Photoshop')[0]
win.activate()
import random
  • 3,054
  • 1
  • 17
  • 22
0

Because the accepted answer is only for Windows, here is a Linux solution for most major distributions:

  1. Install PyWinCtl and the dependency tkinter

    sudo apt install python3-tk
    pip install PyWinCtl
    
  2. Change the import in the example code from above

    import pywinctl as gw
    win = gw.getWindowsWithTitle('Signal')[0]
    win.activate()
    
zx485
  • 28,498
  • 28
  • 50
  • 59