可以使用python脚本最小化活动窗口吗?例如,我打开了一个Firefox浏览器,并在终端窗口中运行一个python脚本,该脚本将使浏览器最小化几秒钟。我需要这个给Debian。
发布于 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模块
import gi
gi.require_version('Wnck','3.0')
from gi.repository import Wnck
如果您没有gi包或Wnck模块
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3-gi gir1.2-wnck-3.0
Wnck模块允许您与任务管理器交互以操作窗口。
下面是一个脚本,它可以找到所有打开的Chromium窗口,最小化它们5秒钟,然后取消最小化。当尝试此代码时,请打开一个Chromium窗口并取消最小化。
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
。
https://stackoverflow.com/questions/45426203
复制相似问题