首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用python最小化窗口

使用python最小化窗口
EN

Stack Overflow用户
提问于 2017-08-01 06:24:39
回答 1查看 2.7K关注 0票数 0

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

EN

回答 1

Stack Overflow用户

发布于 2018-07-29 05:07:41

是的你可以。这就是我是如何做到的。

规格:

Raspberry pi 3,操作系统: Raspbian,版本:9(扩展)

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

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

代码语言:javascript
复制
import gi
gi.require_version('Wnck','3.0')
from gi.repository import Wnck

如果您没有gi包或Wnck模块

代码语言:javascript
复制
sudo apt-get update
sudo apt-get upgrade    
sudo apt-get install python3-gi gir1.2-wnck-3.0

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

下面是一个脚本,它可以找到所有打开的Chromium窗口,最小化它们5秒钟,然后取消最小化。当尝试此代码时,请打开一个Chromium窗口并取消最小化。

代码语言:javascript
复制
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.

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

要检查gi包的版本,在导入gi包之后,在Python终端中输入gi.version_info

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45426203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档