PC版微信自动锁定工具

2025-06-27 文章来源:小K娱乐
人生第一个小软件,孬好的大佬们凑合看
这个软件是为了实现离开办公室的时候自动锁定微信
前提是手机和电脑都在同一个wifi下
手机离开当前wifi后,微信自动锁定
手机需提前设置固定ip
时间间隔建议30秒,因为会连续两次ping不通才会锁定
关闭软件自动缩小到后台运行,右键退出和现实
我也不知道为什么这么点代码这么大的exe,
360会报,不放心可以复制代码自己打包,
技术不精,可能会有误锁定,我也不清楚是咋回事。。。。
成品如下:
https://wwpu.lanzouo.com/iMcyw2zlv2gd
PC版微信自动锁定工具
代码如下:
  1. import tkinter as tk
  2. from tkinter import messagebox, scrolledtext
  3. import threading
  4. import time
  5. import subprocess
  6. import pyautogui
  7. from pystray import MenuItem as item
  8. import pystray
  9. from PIL import Image, ImageDraw
  10. import queue

  11. class WeChatLocker:
  12.     def __init__(self, root):
  13.         self.root = root
  14.         self.root.title("微信自动锁")

  15.         # --- GUI Elements ---
  16.         main_frame = tk.Frame(root)
  17.         main_frame.pack(padx=10, pady=10)

  18.         tk.Label(main_frame, text="手机IP地址:").grid(row=0, column=0, sticky='w', pady=2)
  19.         self.ip_entry = tk.Entry(main_frame, width=25)
  20.         self.ip_entry.grid(row=0, column=1, pady=2)

  21.         tk.Label(main_frame, text="间隔时间(秒):").grid(row=1, column=0, sticky='w', pady=2)
  22.         self.interval_entry = tk.Entry(main_frame, width=25)
  23.         self.interval_entry.grid(row=1, column=1, pady=2)
  24.         self.interval_entry.insert(0, "5")

  25.         button_frame = tk.Frame(main_frame)
  26.         button_frame.grid(row=2, columnspan=2, pady=10)
  27.         self.start_button = tk.Button(button_frame, text="运行", command=self.start_locking)
  28.         self.start_button.pack(side=tk.LEFT, padx=5)
  29.         self.stop_button = tk.Button(button_frame, text="停止", command=self.stop_locking, state=tk.DISABLED)
  30.         self.stop_button.pack(side=tk.LEFT, padx=5)

  31.         tk.Label(main_frame, text="运行信息:").grid(row=3, column=0, sticky='w', pady=2)
  32.         self.info_display = scrolledtext.ScrolledText(main_frame, height=10, width=40, state='normal')
  33.         self.info_display.grid(row=4, columnspan=2, pady=5)
  34.         self.info_display.insert(tk.END, "为避免误ping,连续2次ping不通才会锁定\n建议间隔时间30秒(30秒×2=1分钟)\n52pojie@文火慢燉\n")
  35.         self.info_display.config(state='disabled')

  36.         # --- State and Threading ---
  37.         self.is_running = False
  38.         self.thread = None
  39.         self.message_queue = queue.Queue()

  40.         # --- System Tray and Window Management ---
  41.         self.root.protocol("WM_DELETE_WINDOW", self.hide_to_tray)
  42.         self.icon = None
  43.         self.process_queue()

  44.     def log_message(self, message):
  45.         self.message_queue.put(message)

  46.     def process_queue(self):
  47.         try:
  48.             while True:
  49.                 message = self.message_queue.get_nowait()
  50.                 self.info_display.config(state='normal')
  51.                 self.info_display.insert(tk.END, f"{time.strftime('%H:%M:%S')} - {message}\n")
  52.                 self.info_display.see(tk.END)
  53.                 self.info_display.config(state='disabled')
  54.         except queue.Empty:
  55.             pass
  56.         self.root.after(100, self.process_queue)

  57.     def create_image(self):
  58.         image = Image.new('RGB', (64, 64), 'black')
  59.         dc = ImageDraw.Draw(image)
  60.         dc.rectangle((10, 10, 54, 54), fill='white')
  61.         return image

  62.     def hide_to_tray(self):
  63.         self.root.withdraw()
  64.         image = self.create_image()
  65.         menu = (item('显示', self.show_window), item('退出', self.quit_window))
  66.         self.icon = pystray.Icon("WeChatLocker", image, "微信自动锁定", menu)
  67.         self.icon.run()

  68.     def show_window(self):
  69.         if self.icon:
  70.             self.icon.stop()
  71.         self.root.deiconify()

  72.     def quit_window(self):
  73.         self.stop_locking()
  74.         if self.icon:
  75.             self.icon.stop()
  76.         self.root.destroy()

  77.     def start_locking(self):
  78.         ip = self.ip_entry.get()
  79.         interval_str = self.interval_entry.get()

  80.         if not ip:
  81.             messagebox.showerror("错误", "请输入IP地址")
  82.             return
  83.         try:
  84.             interval = int(interval_str)
  85.             if interval <= 0:
  86.                 raise ValueError
  87.         except ValueError:
  88.             messagebox.showerror("错误", "间隔时间必须是正整数")
  89.             return

  90.         self.is_running = True
  91.         self.start_button.config(state=tk.DISABLED)
  92.         self.stop_button.config(state=tk.NORMAL)
  93.         self.log_message(f"开始监控IP: {ip}")

  94.         self.thread = threading.Thread(target=self.ping_loop, args=(ip, interval), daemon=True)
  95.         self.thread.start()

  96.     def stop_locking(self):
  97.         if self.is_running:
  98.             self.is_running = False
  99.             self.log_message("监控已停止")
  100.         self.start_button.config(state=tk.NORMAL)
  101.         self.stop_button.config(state=tk.DISABLED)

  102.     def ping_loop(self, ip, interval):
  103.         was_pingable = True
  104.         failure_count = 0
  105.         while self.is_running:
  106.             command = ["ping", "-n", "1", "-w", "2000", ip]
  107.             ping_success = False
  108.             try:
  109.                 result = subprocess.run(
  110.                     command,
  111.                     check=False,
  112.                     capture_output=True,
  113.                     text=True,
  114.                     creationflags=subprocess.CREATE_NO_WINDOW
  115.                 )
  116.                 if result.returncode == 0:
  117.                     ping_success = True
  118.             except Exception as e:
  119.                 self.log_message(f"Ping命令执行出错: {e}")

  120.             if ping_success:
  121.                 failure_count = 0
  122.             else:
  123.                 failure_count += 1

  124.             is_pingable = failure_count < 2

  125.             if is_pingable:
  126.                 if not was_pingable:
  127.                     self.log_message(f"Ping {ip} 成功,连接已恢复。")
  128.                 else:
  129.                     self.log_message(f"Ping {ip} 成功,状态正常 (失败次数: {failure_count})")
  130.             else:  # is_pingable is False
  131.                 if was_pingable:
  132.                     self.log_message(f"Ping {ip} 连续失败2次,准备锁定微信。")
  133.                     self.lock_wechat()
  134.                 else:
  135.                     self.log_message(f"Ping {ip} 持续失败 (次数: {failure_count}),微信已锁定,无需重复操作。")

  136.             was_pingable = is_pingable

  137.             for _ in range(interval):
  138.                 if not self.is_running:
  139.                     break
  140.                 time.sleep(1)

  141.     def lock_wechat(self):
  142.         try:
  143.             self.log_message("执行快捷键 Ctrl+Alt+W 打开微信")
  144.             pyautogui.hotkey('ctrl', 'alt', 'w')
  145.             time.sleep(0.5) # 等待微信窗口出现
  146.             self.log_message("执行快捷键 Ctrl+L 锁定微信")
  147.             pyautogui.hotkey('ctrl', 'l')
  148.             time.sleep(0.5) # 等待锁定完成
  149.             pyautogui.hotkey('ctrl', 'alt', 'w') # 再次执行打开微信快捷键来隐藏微信
  150.             self.log_message("微信已锁定并隐藏")
  151.         except Exception as e:
  152.             self.log_message(f"锁定微信时出错: {e}")

  153. if __name__ == "__main__":
  154.     root = tk.Tk()
  155.     app = WeChatLocker(root)
  156.     root.mainloop()
复制代码






正文到此结束

免责声明:本站所有内容仅限用于学习和研究目的,不得将本站任何内容用于商业或者非法用途,否则一切后果由用户自负。本站提供的资源来自网络自动采集,版权争议与本站无关。您必须在下载后24个小时内,从您的设备中彻底删除上述内容。本站十分重视版权问题,侵权删除请及时联系我们。

文章来源:《小K娱乐》 https://www.xkwo.com/thread-25984-1-1.html

网 站 主 题 设 置
网站主题设置