python程序开发窗口_用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.

解决方案

Yes you can. Here is how I did it.

SPECS:

Raspberry pi 3, OS: Raspbian, Version: 9 (Stretch)

Python 3.5.3, gi package version: 3.22, Wnck version: 3.20.1

You will need to use the Wnck module from gi.repository

import gi

gi.require_version('Wnck','3.0')

from gi.repository import 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

The Wnck module allows you to interface with the task manager to manipulate windows.

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.

This link provides the documentation for the Wnck 3.0 module classes https://lazka.github.io/pgi-docs/#Wnck-3.0/classes.html

To check your gi package version type gi.version_info into the Python terminal after you have imported the gi package.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值