Is it possible in python to minimize an active window by name or by focus state and, if so, how?
For example: I have an open Chromium browser and run a script in python and for five seconds my browser is minimized.
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It only takes a minute to sign up.Sign up to join this community
Yes you can. Here is how I did it.
Raspberry pi 3, OS: Raspbian, Version: 9 (Stretch)
Python 3.5.3, gi package version: 3.22, Wnck version: 3.20.1
You will need to use the Wnck module from gi.repository
import gi gi.require_version('Wnck','3.0') from gi.repository import Wnck
If you do not have the gi package or Wnck module
sudo apt-get update sudo apt-get upgrade sudo apt-get install python3-gi gir1.2-wnck-3.0
The Wnck module allows you to interface with the task manager to manipulate windows.
Below is a script that will find all open Chromium windows, minimize them for 5 seconds and then unminimize them. When trying this code please have a Chromium window open and unminimized.
import gi #Import gi pageage gi.require_version('Wnck','3.0') from gi.repository import Wnck #Import Wnck module from time import sleep #Used for 5 second delay screen=Wnck.Screen.get_default() #Get screen information screen.force_update() #Update screen object windows=screen.get_windows() #Get all windows in task bar. The first 2 elements I believe relate to the OS GUI interface itself and the task bar. All other elements are the open windows in the task bar in that order. for w in windows: #Go through each window one by one. if 'Chromium' in w.get_name(): #Get name of window and check if it includes 'Chromium' w.minimize() #If so, minimize ask the task manager to minimize that window. sleep(5) #Wait your desired 5 seconds. w.unminimize(1) #Ask the task manager to unminimize that window. This function needs an integer, I don't know what it does. print(w.get_name()) #Print the windows name to give you a warm fuzzy it was the right window.
This link provides the documentation for the Wnck 3.0 module classes https://lazka.github.io/pgi-docs/#Wnck-3.0/classes.html
To check your gi package version type
gi.version_info into the Python terminal after you have imported the gi package.