Home  >  Article  >  Backend Development  >  How to implement a GUI interface that follows the movement of the WeChat window in real time in Python

How to implement a GUI interface that follows the movement of the WeChat window in real time in Python

WBOY
WBOYforward
2023-05-14 19:16:041137browse

It is very simple to write some simple GUI interfaces in Python, and Python has a wealth of libraries. These libraries can make it very convenient for us to operate Windows systems. With the interface, we can make many exquisite gadgets. The case of this article is a simple example, using Python to create a window that monitors the status of the WeChat PC version client window in real time and follows the right side of the WeChat PC version in real time.

Code

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()

Code explanation

In this example, the tkinter interface library that comes with Python is used to implement a simple borderless window with a window width of 200 and a height of 200 Consistent with the WeChat window. Get the position and size of the WeChat window in real time by getting the handle of the WeChat client, start the timer to get the display status of WeChat in real time, and update the status every 50ms to achieve the purpose of following.

Animation Demonstration

How to implement a GUI interface that follows the movement of the WeChat window in real time in Python

The above is the detailed content of How to implement a GUI interface that follows the movement of the WeChat window in real time in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete