Python实现实时跟随微信窗口移动的GUI界面
作者:TANKING 时间:2022-06-09 11:12:25
Python写一些简单的GUI界面也是非常简单的,并且Python有着丰富的库,这些库可以很方便我们去操作Windows系统,搭配界面,可以做出很多精美的小工具。本文的案例写一个简单的实例,使用Python创建一个窗口,这个窗口实时监听微信PC版客户端窗口的状态,并且实时跟随在微信电脑版的右侧。
代码
import tkinter as tk
import win32gui
import win32con
class FollowWeChatWindow(tk.Tk):
def __init__(self):
super().__init__()
# 隐藏窗口边框和标题栏
self.overrideredirect(True)
self.title("跟随微信的窗口")
# 窗口置顶
self.wm_attributes('-topmost', True)
# 创建一个标签,用于显示窗口位置信息
self.label = tk.Label(self, text='')
self.label.pack()
# 启动定时器
self.after(50, self.update_window)
def update_window(self):
# 获取微信窗口句柄和位置
wechat_hwnd = win32gui.FindWindow('WeChatMainWndForPC', None)
if wechat_hwnd:
wechat_rect = win32gui.GetWindowRect(wechat_hwnd)
# print(win32gui.GetWindowText(win32gui.GetForegroundWindow()))
# 获取当前鼠标点击的窗口的句柄的标题
getClickHownTitle = win32gui.GetWindowText(win32gui.GetForegroundWindow())
# 判断微信窗口状态,显示或隐藏本窗口
if win32gui.GetForegroundWindow() == wechat_hwnd:
# 当前点击的句柄=微信的句柄
self.wm_attributes('-alpha', 1.0)
elif getClickHownTitle == '跟随微信的窗口':
# 当前点击的窗口的标题=跟随微信的窗口
self.wm_attributes('-alpha', 1.0)
else:
# 不满足以上两个条件的其中一条,都得隐藏窗口
self.wm_attributes('-alpha', 0.0)
print(getClickHownTitle)
# 获取微信窗口高度
WeChat_Height = wechat_rect[3] - wechat_rect[1]
# 更新本窗口位置
self.geometry('200x%d+%d+%d' % (WeChat_Height, wechat_rect[2], wechat_rect[1]))
# 更新标签文本
self.label.configure(text=win32gui.GetForegroundWindow())
else:
# 微信窗口未找到,隐藏本窗口
self.wm_attributes('-alpha', 0.0)
# 继续定时器
self.after(50, self.update_window)
if __name__ == '__main__':
app = FollowWeChatWindow()
app.mainloop()
代码解释
在这个示例中,使用了Python自带的tkinter界面库来实现一个简单的无边框窗口,窗口宽度200,高度与微信窗口一致。通过获取微信客户端的句柄来实时获取微信窗口的位置及大小,启动定时器实时获取到微信的显示状态,50ms更新一次状态以达到跟随的目的。
动图演示
应用
开发一个这样的窗口有什么用途?
客服助手,快捷回复语句
便携笔记,快速记录
备注助手,实时获取微信用户信息
来源:https://segmentfault.com/a/1190000043640284
标签:Python,实时,移动,窗口
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈信息可视化
2010-03-10 10:55:00
![](https://img.aspxhome.com/file/UploadPic/20103/10/01-20s.jpg)
自动清空站点目录下所有文件
2009-06-24 11:11:00
django项目简单调取百度翻译接口的方法
2023-10-20 18:25:41
![](https://img.aspxhome.com/file/2023/0/64280_0s.png)
Python面向对象之静态属性、类方法与静态方法分析
2021-03-28 15:14:22
在Python中通过threshold创建mask方式
2022-10-22 16:52:49
python 爬取百度文库并下载(免费文章限定)
2022-05-08 16:18:28
![](https://img.aspxhome.com/file/2023/3/65413_0s.png)
Python IDE Pycharm中的快捷键列表用法
2022-12-12 19:21:12
![](https://img.aspxhome.com/file/2023/5/86615_0s.png)
python3利用Dlib19.7实现人脸68个特征点标定
2021-05-05 19:13:06
![](https://img.aspxhome.com/file/2023/4/76034_0s.jpg)
Python实现动态二维码生成的示例代码
2022-08-03 15:45:39
![](https://img.aspxhome.com/file/2023/6/79616_0s.png)
TensorFlow绘制loss/accuracy曲线的实例
2022-01-25 08:01:16
![](https://img.aspxhome.com/file/2023/0/95170_0s.jpg)
sqlserver 2000中创建用户的图文方法
2012-03-26 18:26:39
ubuntu环境下python虚拟环境的安装过程
2022-07-25 06:09:07
Python实现删除列表中满足一定条件的元素示例
2023-11-07 14:38:39
PHP学习记录之面向对象(Object-oriented programming,OOP)基础【类、对象、继承等】
2023-11-21 07:25:59
![](https://img.aspxhome.com/file/2023/0/106310_0s.png)
Jquery中Ajax 缓存带来的影响的解决方法
2011-05-21 16:14:00
Python Map 函数的使用
2023-09-02 06:27:17
试了下Golang实现try catch的方法
2023-07-21 20:49:59
深入浅析python定时杀进程
2021-10-23 17:14:15
深入理解Python中变量赋值的问题
2023-03-20 00:28:07
Python3 导入上级目录中的模块实例
2023-09-01 02:25:20