iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >如何用python 实现老板键功能
  • 597
分享到

如何用python 实现老板键功能

2024-04-02 19:04:59 597人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录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.最终效果

以上就是python 老板键功能的实现步骤的详细内容,更多关于python 老板键功能的资料请关注编程网其它相关文章!

--结束END--

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

本文链接: https://www.lsjlt.com/news/121695.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作