如何用python 实现老板键功能
作者:可爱的黑精灵 时间:2022-09-30 10:13:25
目录
1.获取所有顶层窗口
2.手动选择需要设置老板键的程序
3.隐藏或显示选中程序
4.设置显示隐藏快捷键
5.最终效果
主要实现目标:为多个指定的程序实现统一的老板键,一键隐藏多个指定的应用程序的窗口及任务栏。
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.最终效果
来源:https://www.cnblogs.com/chenjy1225/p/14362264.html
标签:python,老板键
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现简单的学生信息管理系统
2021-06-19 10:59:44
![](https://img.aspxhome.com/file/2023/6/82776_0s.jpg)
ASP利用Google实现在线翻译功能
2010-03-07 17:28:00
Python seaborn barplot画图案例
2023-11-07 00:50:55
![](https://img.aspxhome.com/file/2023/7/63977_0s.png)
CSS资料中常见英语词语翻译整理
2008-08-29 12:50:00
如何在Access数据库中立即得到所插入记录的自动编号?
2010-06-17 12:45:00
Ubuntu权限不足无法创建文件夹解决方案
2021-04-06 01:31:27
![](https://img.aspxhome.com/file/2023/6/107756_0s.png)
使用python BeautifulSoup库抓取58手机维修信息
2022-08-10 01:55:20
Python OpenCV之常用滤波器使用详解
2023-02-07 22:18:25
![](https://img.aspxhome.com/file/2023/9/81589_0s.jpg)
Django利用AJAX技术实现博文实时搜索
2023-01-02 01:57:50
![](https://img.aspxhome.com/file/2023/3/80583_0s.jpg)
去掉运行JavaScript时IE产生的警告栏
2008-09-11 18:07:00
![](https://img.aspxhome.com/file/UploadPic/20089/11/2008911181137794s.png)
Python实现点云投影到平面显示
2021-05-10 14:51:15
Python之日期与时间处理模块(date和datetime)
2023-09-29 12:53:06
![](https://img.aspxhome.com/file/2023/2/63232_0s.png)
用python实现域名资产监控的详细步骤
2021-12-05 07:52:49
![](https://img.aspxhome.com/file/2023/8/69538_0s.png)
Golang 使用http Client下载文件的实现方法
2023-07-21 07:32:23
使用Python实现二分法查找的示例
2022-02-08 13:52:53
![](https://img.aspxhome.com/file/2023/9/85379_0s.png)
XML编程实例:用ASP+XML打造留言本
2008-05-04 13:37:00
python二分法实现实例
2023-02-16 05:09:46
pandas将DataFrame的几列数据合并成为一列
2022-07-27 13:46:23
python点云地面点滤波(Progressive Morphological Filter)算法介绍(PCL库)
2021-12-23 00:38:29
![](https://img.aspxhome.com/file/2023/9/96919_0s.png)
python 类的继承 实例方法.静态方法.类方法的代码解析
2021-12-19 14:19:34