|
|
转的python版本,为防止ctrl+alt+del,必须用管理员权限运行,win7下测试通过
- # -*- coding: utf-8 -*-
- import tkinter as tk
- from tkinter import filedialog, messagebox
- import os
- import sys
- import winreg
- import ctypes
- # ==================== 1. 系统安全加固 (封锁任务管理器) ====================
- def is_admin():
- try: return ctypes.windll.shell32.IsUserAnAdmin()
- except: return False
- def set_lock_registry(disable=True):
- # 封锁路径:同时封锁用户和默认策略
- reg_paths = [
- r"Software\Microsoft\Windows\CurrentVersion\Policies\System",
- r"Software\Policies\Microsoft\Windows\System"
- ]
- value = 1 if disable else 0
- for path in reg_paths:
- try:
- key = winreg.CreateKeyEx(winreg.HKEY_CURRENT_USER, path, 0, winreg.KEY_SET_VALUE)
- winreg.SetValueEx(key, "DisableTaskMgr", 0, winreg.REG_DWORD, value)
- winreg.CloseKey(key)
- except: pass
- # 自动提升管理员权限
- if not is_admin():
- ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
- sys.exit()
- # ==================== 2. 配置管理 ====================
- INI_FILE = "LockConfig.ini"
- def read_config():
- p, c, b = "", 8, ""
- if os.path.exists(INI_FILE):
- with open(INI_FILE, "r", encoding="utf-8") as f:
- for line in f:
- if "Password=" in line: p = line.split("=")[1].strip()
- elif "ClickUnlockCount=" in line: c = int(line.split("=")[1].strip())
- elif "BackgroundImage=" in line: b = line.split("=")[1].strip()
- return p, c, b if (b and b.lower() != "none" and os.path.exists(b)) else None
- def save_config(p, c, b):
- with open(INI_FILE, "w", encoding="utf-8") as f:
- f.write(f"Password={p}\nClickUnlockCount={c}\nBackgroundImage={b or 'none'}\n")
- PASSWORD, CLICK_COUNT, BG_PATH = read_config()
- is_locked = False
- # ==================== 3. 锁屏核心 (含图片轮播) ====================
- def start_lock_screen():
- global is_locked
- set_lock_registry(True) # 锁定任务管理器
- is_locked = True
-
- root = tk.Tk()
- root.overrideredirect(True)
- w, h = root.winfo_screenwidth(), root.winfo_screenheight()
- root.geometry(f"{w}x{h}+0+0")
- root.configure(bg="#000000")
- root.attributes("-topmost", True)
-
- # --- A. 背景层 (轮播标签) ---
- bg_label = tk.Label(root, bg="#000000")
- bg_label.place(x=0, y=0, relwidth=1, relheight=1)
- # --- B. UI 交互层 (始终在最顶层) ---
- ui_frame = tk.Frame(root, bg="#1c1c1c", highlightthickness=2, highlightbackground="#00FF00")
- ui_frame.place(relx=0.5, rely=0.5, anchor="center")
- tk.Label(ui_frame, text="🔒 系统锁定中", font=("微软雅黑", 22, "bold"), fg="#00FF00", bg="#1c1c1c").pack(pady=20, padx=50)
- pass_var = tk.StringVar()
- entry = tk.Entry(ui_frame, textvariable=pass_var, font=("Consolas", 20), show="*", width=15, justify='center', bg="#333", fg="white", insertbackground="white")
- entry.pack(pady=10)
- def do_unlock():
- if pass_var.get() == PASSWORD:
- global is_locked
- is_locked = False
- set_lock_registry(False) # 解除任务管理器封锁
- root.destroy()
- sys.exit(0)
- else:
- entry.config(bg="#721c24")
- root.after(300, lambda: entry.config(bg="#333"))
- pass_var.set("")
- entry.focus_set()
- tk.Button(ui_frame, text="解除锁定", font=("微软雅黑", 14, "bold"), bg="#28a745", fg="white",
- width=15, command=do_unlock, activebackground="#218838", bd=0).pack(pady=20)
- # --- C. 图片轮播逻辑模块 ---
- img_files = []
- if BG_PATH:
- if os.path.isdir(BG_PATH):
- # 扫描文件夹
- exts = ('.png', '.jpg', '.jpeg', '.gif', '.bmp')
- img_files = [os.path.join(BG_PATH, f) for f in os.listdir(BG_PATH) if f.lower().endswith(exts)]
- elif os.path.isfile(BG_PATH):
- img_files = [BG_PATH]
- def play_gallery(index=0):
- if not is_locked or not img_files: return
- try:
- # 使用标准的 PhotoImage
- # 注意:tkinter默认PhotoImage不支持所有JPG,如果报错请确保安装了 Pillow 库并修改此处
- new_img = tk.PhotoImage(file=img_files[index % len(img_files)])
- bg_label.config(image=new_img)
- bg_label.image = new_img
- if len(img_files) > 1:
- root.after(5000, lambda: play_gallery(index + 1)) # 5秒切一张
- except Exception as e:
- # 遇到格式不支持的文件跳过
- root.after(100, lambda: play_gallery(index + 1))
- if img_files:
- play_gallery(0)
- # --- D. 事件保护 ---
- def keep_focus(e=None):
- if is_locked:
- entry.focus_set()
- root.lift()
- root.bind("<Return>", lambda e: do_unlock())
- root.bind("<FocusOut>", lambda e: root.after(10, keep_focus))
- root.bind("<Alt_L>", lambda e: "break")
- root.bind("<Alt_R>", lambda e: "break")
- root.protocol("WM_DELETE_WINDOW", lambda: None)
-
- entry.focus_set()
- root.grab_set()
- root.mainloop()
- # ==================== 4. 设置界面 ====================
- def create_main_gui():
- global BG_PATH
- m_root = tk.Tk()
- m_root.title("挂机锁设置")
- m_root.geometry("400x300")
- tk.Label(m_root, text="设置解锁密码:").pack(pady=10)
- p_var = tk.StringVar(value=PASSWORD)
- tk.Entry(m_root, textvariable=p_var, width=30).pack()
- cur_path = tk.StringVar(value=f"当前路径: {os.path.basename(BG_PATH) if BG_PATH else '未选择'}")
- tk.Label(m_root, textvariable=cur_path, wraplength=350).pack(pady=10)
- def select_dir():
- global BG_PATH
- path = filedialog.askdirectory() # 明确选择文件夹
- if path:
- BG_PATH = path
- cur_path.set(f"当前路径: {path}")
- tk.Button(m_root, text="选择图片文件夹", command=select_dir).pack()
-
- def launch():
- if not p_var.get().strip():
- messagebox.showwarning("提示", "密码不能为空")
- return
- save_config(p_var.get().strip(), 8, BG_PATH)
- m_root.destroy()
- start_lock_screen()
- tk.Button(m_root, text=" 开启锁定 ", bg="#d9534f", fg="white", font=("bold", 12), command=launch, width=20).pack(pady=25)
- m_root.mainloop()
- if __name__ == "__main__":
- if PASSWORD:
- start_lock_screen()
- else:
- create_main_gui()
复制代码
|
|