用python最小化窗口

时间:2017-07-31 22:24:39

标签: python python-3.x debian

是否可以使用python脚本最小化活动窗口?例如,我打开了一个Firefox浏览器,在终端窗口中我运行了一个python脚本,可以将浏览器最小化几秒钟。我需要这个用于Debian。

1 个答案:

答案 0 :(得分:1)

可以。这是我的方法。

  

SPECS:

     

Raspberry pi 3,操作系统:Raspbian,版本:9(拉伸)

     

Python 3.5.3,gi软件包版本:3.22,Wnck版本:3.20.1

您将需要使用gi.repository中的Wnck模块

var token = new TokenResponse { RefreshToken = "single token only" };
var credentials = new UserCredential(new 
    GoogleAuthorizationCodeFlow(
        new GoogleAuthorizationCodeFlow.Initializer {
            ClientSecrets = new ClientSecrets() {
                ClientId = "id", 
                ClientSecret = "secret"
            }
        }
    ),
    "user",
    token
);

var calenderClient = new CalendarService(new BaseClientService.Initializer() {
    HttpClientInitializer = credential,
    ApplicationName = ApplicationName,
});

如果您没有gi软件包或Wnck模块

import gi
gi.require_version('Wnck','3.0')
from gi.repository import Wnck

Wnck模块允许您与任务管理器进行交互以操作窗口。

下面是一个脚本,它将查找所有打开的Chromium窗口,将其最小化5秒钟,然后将其最小化。尝试使用此代码时,请打开并最小化Chromium窗口。

sudo apt-get update
sudo apt-get upgrade    
sudo apt-get install python3-gi gir1.2-wnck-3.0

此链接提供Wnck 3.0模块类https://lazka.github.io/pgi-docs/#Wnck-3.0/classes.html

的文档

要在导入gi软件包后在Python终端中输入gi软件包版本,请输入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.