【问题标题】:Minimize window with python用python最小化窗口
【发布时间】:2017-07-31 22:24:39
【问题描述】:

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

【问题讨论】:

标签: python python-3.x debian


【解决方案1】:

是的,你可以。这是我的做法。

规格:

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

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 包后,在 Python 终端中检查您的 gi 包版本类型 gi.version_info

【讨论】:

  • 谢谢,这在树莓派上也能完美运行!在 Pi 上,即使铬进程被称为铬浏览器,窗口仍然被称为“铬”,因此无需修改代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 2012-03-21
  • 2011-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
相关资源
最近更新 更多