Sec Cam
Sec Cam

Reputation: 67

Minimize window with python

Is is possible to minimize an active window using a python script? For example, I have open a Firefox browser and in a terminal window I run a python script that will minimize the browser for a few seconds. I need this for Debian.

Upvotes: 0

Views: 4122

Answers (1)

happyZucchini_
happyZucchini_

Reputation: 63

Yes you can. Here is how I did it.

SPECS:

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.

Upvotes: 2

Related Questions