PC版微信自动锁定工具

11次阅读
没有评论

共计 2914 个字符,预计需要花费 8 分钟才能阅读完成。

人生第一个小软件,孬好的大佬们凑合看
这个软件是为了实现离开办公室的时候自动锁定微信
前提是手机和电脑都在同一个 wifi 下
手机离开当前 wifi 后,微信自动锁定
手机需提前设置固定 ip
时间间隔建议 30 秒,因为会连续两次 ping 不通才会锁定
关闭软件自动缩小到后台运行,右键退出和现实
我也不知道为什么这么点代码这么大的 exe,
360 会报,不放心可以复制代码自己打包,
技术不精,可能会有误锁定,我也不清楚是咋回事。。。。
成品如下:
https://wwpu.lanzouo.com/iMcyw2zlv2gd
PC 版微信自动锁定工具
代码如下:

import tkinter as tk
from tkinter import messagebox, scrolledtext
import threading
import time
import subprocess
import pyautogui
from pystray import MenuItem as item
import pystray
from PIL import Image, ImageDraw
import queue
 
class WeChatLocker:
    def __init__(self, root):
        self.root = root
        self.root.title("微信自动锁")
 
        # --- GUI Elements ---
        main_frame = tk.Frame(root)
        main_frame.pack(padx=10, pady=10)
 
        tk.Label(main_frame, text="手机 IP 地址:").grid(row=0, column=0, sticky='w', pady=2)
        self.ip_entry = tk.Entry(main_frame, width=25)
        self.ip_entry.grid(row=0, column=1, pady=2)
 
        tk.Label(main_frame, text="间隔时间 ( 秒):").grid(row=1, column=0, sticky='w', pady=2)
        self.interval_entry = tk.Entry(main_frame, width=25)
        self.interval_entry.grid(row=1, column=1, pady=2)
        self.interval_entry.insert(0, "5")
 
        button_frame = tk.Frame(main_frame)
        button_frame.grid(row=2, columnspan=2, pady=10)
        self.start_button = tk.Button(button_frame, text="运行", command=self.start_locking)
        self.start_button.pack(side=tk.LEFT, padx=5)
        self.stop_button = tk.Button(button_frame, text="停止", command=self.stop_locking, state=tk.DISABLED)
        self.stop_button.pack(side=tk.LEFT, padx=5)
 
        tk.Label(main_frame, text="运行信息:").grid(row=3, column=0, sticky='w', pady=2)
        self.info_display = scrolledtext.ScrolledText(main_frame, height=10, width=40, state='normal')
        self.info_display.grid(row=4, columnspan=2, pady=5)
        self.info_display.insert(tk.END, "为避免误 ping,连续 2 次 ping 不通才会锁定 \n 建议间隔时间 30 秒(30 秒×2= 1 分钟)\n52pojie@文火慢燉 \n")
        self.info_display.config(state='disabled')
 
        # --- State and Threading ---
        self.is_running = False
        self.thread = None
        self.message_queue = queue.Queue()
 
        # --- System Tray and Window Management ---
        self.root.protocol("WM_DELETE_WINDOW", self.hide_to_tray)
        self.icon = None
        self.process_queue()
 
    def log_message(self, message):
        self.message_queue.put(message)
 
    def process_queue(self):
        try:
            while True:
                message = self.message_queue.get_nowait()
                self.info_display.config(state='normal')
                self.info_display.insert(tk.END, f"{time.strftime('%H:%M:%S')} - {message}\n")
                self.info_display.see(tk.END)
                self.info_display.config(state='disabled')
        except queue.Empty:
            pass
        self.root.after(100, self.process_queue)
 
    def create_image(self):
        image = Image.new('RGB', (64, 64), 'black')
        dc = ImageDraw.Draw(image)
        dc.rectangle((10, 10, 54, 54), fill='white')
        return image
 
    def hide_to_tray(self):
        self.root.withdraw()
        image = self.create_image()
        menu = (item('显示', self.show_window), item('退出', self.quit_window))
        self.icon = pystray.Icon("WeChatLocker", image, "微信自动锁定", menu)
        self.icon.run()
 
    def show_window(self):
        if self.icon:
            self.icon.stop()
        self.root.deiconify()
 
    def quit_window(self):
        self.stop_locking()
        if self.icon:
            self.icon.stop()
        self.root.destroy()
 
    def start_locking(self):
        ip = self.ip_entry.get()
        interval_str = self.interval_entry.get()
 
        if not ip:
            messagebox.showerror("错误", "请输入 IP 地址")
            return
        try:
            interval = int(interval_str)
            if interval <= 0:
                raise ValueError
        except ValueError:
            messagebox.showerror("错误", "间隔时间必须是正整数")
            return
 
        self.is_running = True
        self.start_button.config(state=tk.DISABLED)
        self.stop_button.config(state=tk.NORMAL)
        self.log_message(f"开始监控 IP: {ip}")
 
        self.thread = threading.Thread(target=self.ping_loop, args=(ip, interval), daemon=True)
        self.thread.start()
 
    def stop_locking(self):
        if self.is_running:
            self.is_running = False
            self.log_message("监控已停止")
        self.start_button.config(state=tk.NORMAL)
        self.stop_button.config(state=tk.DISABLED)
 
    def ping_loop(self, ip, interval):
        was_pingable = True
        failure_count = 0
        while self.is_running:
            command = ["ping", "-n", "1", "-w", "2000", ip]
            ping_success = False
            try:
                result = subprocess.run(
                    command, 
                    check=False, 
                    capture_output=True, 
                    text=True, 
                    creationflags=subprocess.CREATE_NO_WINDOW
                )
                if result.returncode == 0:
                    ping_success = True
            except Exception as e:
                self.log_message(f"Ping 命令执行出错: {e}")
 
            if ping_success:
                failure_count = 0
            else:
                failure_count += 1
 
            is_pingable = failure_count < 2
 
            if is_pingable:
                if not was_pingable:
                    self.log_message(f"Ping {ip} 成功,连接已恢复。")
                else:
                    self.log_message(f"Ping {ip} 成功,状态正常 (失败次数: {failure_count})")
            else:  # is_pingable is False
                if was_pingable:
                    self.log_message(f"Ping {ip} 连续失败 2 次,准备锁定微信。")
                    self.lock_wechat()
                else:
                    self.log_message(f"Ping {ip} 持续失败 (次数: {failure_count}),微信已锁定,无需重复操作。")
 
            was_pingable = is_pingable
 
            for _ in range(interval):
                if not self.is_running:
                    break
                time.sleep(1)
 
    def lock_wechat(self):
        try:
            self.log_message("执行快捷键 Ctrl+Alt+W 打开微信")
            pyautogui.hotkey('ctrl', 'alt', 'w')
            time.sleep(0.5) # 等待微信窗口出现
            self.log_message("执行快捷键 Ctrl+L 锁定微信")
            pyautogui.hotkey('ctrl', 'l')
            time.sleep(0.5) # 等待锁定完成
            pyautogui.hotkey('ctrl', 'alt', 'w') # 再次执行打开微信快捷键来隐藏微信
            self.log_message("微信已锁定并隐藏")
        except Exception as e:
            self.log_message(f"锁定微信时出错: {e}")
 
if __name__ == "__main__":
    root = tk.Tk()
    app = WeChatLocker(root)
    root.mainloop()

正文完
 0
suyan
版权声明:本站原创文章,由 suyan 于2025-06-27发表,共计2914字。
转载说明:转载本网站任何内容,请按照转载方式正确书写本站原文地址。本站提供的一切软件、教程和内容信息仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。如果您喜欢该程序,请支持正版,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。敬请谅解!
评论(没有评论)
验证码