用python最小化窗口 [英] Minimize window with python
问题描述
是否可以使用python脚本最小化活动窗口?例如,我打开了Firefox浏览器,并在终端窗口中运行了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.
推荐答案
是的。
SPECS:
SPECS:
Raspberry pi 3,操作系统:Raspbian,版本:9(拉伸)
Raspberry pi 3, OS: Raspbian, Version: 9 (Stretch)
Python 3.5.3,gi软件包版本:3.22,Wnck版本:3.20.1
Python 3.5.3, gi package version: 3.22, Wnck version: 3.20.1
您将需要使用gi.repository中的Wnck模块
You will need to use the Wnck module from gi.repository
import gi
gi.require_version('Wnck','3.0')
from gi.repository import Wnck
如果您没有gi软件包或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
Wnck模块允许您与任务管理器进行交互以操作窗口。
The Wnck module allows you to interface with the task manager to manipulate windows.
下面是一个脚本,它将查找所有打开的Chromium窗口,将其最小化5秒钟,然后将其最小化。尝试此代码时,请打开并最小化Chromium窗口。
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.
此链接提供Wnck 3.0模块类的文档> https://lazka.github.io/pgi-docs/#Wnck-3.0/classes.html
This link provides the documentation for the Wnck 3.0 module classes https://lazka.github.io/pgi-docs/#Wnck-3.0/classes.html
要检查gi软件包的版本,请在导入gi软件包后在Python终端中输入 gi.version_info
。
To check your gi package version type gi.version_info
into the Python terminal after you have imported the gi package.
这篇关于用python最小化窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!