欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

如何用python 实现老板键功能

时间:2021-10-10 09:13:44|栏目:Python代码|点击:

主要实现目标:为多个指定的程序实现统一的老板键,一键隐藏多个指定的应用程序的窗口及任务栏。

1.获取所有顶层窗口

import win32gui

hwnd_title = dict()

def get_all_hwnd(hwnd, mouse):
 # 判断句柄是否为窗口、窗口是否允许输入、窗口是否可视
 if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
 hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})

# EnumWindows枚举所有顶层窗口
win32gui.EnumWindows(get_all_hwnd, 0)

# 打印出所有窗口名不为空的窗口
for key in list(hwnd_title):
 if hwnd_title[key] is "":
 del hwnd_title[key]

2.手动选择需要设置老板键的程序

import tkinter as tk

root = tk.Tk()
root.geometry("800x400")

# 列表显示所有顶层窗口
listBox = tk.Listbox(root, selectmode="multiple")
listBox.pack(side="left", expand="yes", fill="both")

for i, j in hwnd_title.items():
 if j is not "":
 listBox.insert("end", str(i) + ":" + j)

bt = tk.Button(root, text='选择')
bt.pack()

root.mainloop()

3.隐藏或显示选中程序

# 通过GetWindowRect方法获取隐藏前的窗口位置及尺寸信息
left, top, right, bottom = win32gui.GetWindowRect()

def close_windows(aimLists):
 for k in aimLists:
 # 隐藏程序窗口
 win32gui.SetWindowPos(lists[k][0], 0, 0, 0, 0, 0, SWP_HIDEWINDOW)


def open_windows(aimLists):
 for k in aimLists:
 # 显示程序窗口
 t = lists[k]
 win32gui.SetWindowPos(t['hwnd'], 0, t['left'], t['top'], t['right'] - t['left'], t['bottom'] - t['top'],
  SWP_SHOWWINDOW)

4.设置显示隐藏快捷键

这里设置F7显示,F8隐藏,F12中止

import PyHook3
import pythoncom

def onKeyboardEvent(event):
 key = event.Key
 if key == "F7":
 close_windows(aimLists)
 if key == "F8":
 open_windows(aimLists)
 if key == "F12":
 win32gui.PostQuitMessage(0)
 return True

hm = PyHook3.HookManager()
hm.KeyDown = onKeyboardEvent
hm.HookKeyboard()

# 开启监听
pythoncom.PumpMessages()

5.最终效果

上一篇:python 快速排序代码

栏    目:Python代码

下一篇:Python 给屏幕打印信息加上颜色的实现方法

本文标题:如何用python 实现老板键功能

本文地址:http://www.codeinn.net/misctech/179475.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有