python GUI查看本地保存的WiFi密码

技术漫步 2023-06-23 PM 194℃ 0条

代码如下:

import locale
import re
import subprocess
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk


def get_wifi_profiles():
    loc_lang = locale.getdefaultlocale()

    if loc_lang[0] == "zh_CN":
        re_pattern = ["所有用户配置文件 : (.*)\r", "安全密钥:不存在", "关键内容            :(.*)\r"]
    else:
        re_pattern = ["All User Profile : (.*)\r", "Security key:Absent", "Key Content            :(.*)\r"]

    cmd_output = subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output=True).stdout.decode('gbk')
    wifi_names = (re.findall(re_pattern[0], cmd_output))
    wifi_list = []
    if len(wifi_names) != 0:
        for name in wifi_names:
            wifi_profile = {}
            profile_info = subprocess.run(["netsh", "wlan", "show", "profiles", name],
                                          capture_output=True).stdout.decode('gbk')
            if re.search(re_pattern[1], profile_info):
                continue
            else:
                wifi_profile["ssid"] = name
                profile_info_pass = subprocess.run(["netsh", "wlan", "show", "profiles", name, "key=clear"],
                                                   capture_output=True).stdout.decode('gbk')
                password = re.search(re_pattern[2], profile_info_pass)
                if not password:
                    wifi_profile["password"] = "None"
                else:
                    wifi_profile["password"] = password[1]
            wifi_list.append(wifi_profile)
    return wifi_list


def copy_password(password):
    root.clipboard_clear()
    root.clipboard_append(password)
    messagebox.showinfo("密码复制成功", "WiFi密码已复制到剪贴板。")


def show_wifi_profiles():
    wifi_list = get_wifi_profiles()
    for i, wifi in enumerate(wifi_list):
        ssid_label = ttk.Label(root, text=wifi["ssid"])
        ssid_label.grid(row=i + 1, column=0, padx=10, pady=5)

        password_label = ttk.Label(root, text=wifi["password"])
        password_label.grid(row=i + 1, column=1, padx=10, pady=5)

        if wifi["password"] != "None":
            copy_button = ttk.Button(root, text="复制密码",
                                     command=lambda password=wifi["password"]: copy_password(password))
            copy_button.grid(row=i + 1, column=2, padx=10, pady=5)

        if (i + 1) % 2 == 0:
            ssid_label.configure(background="#e0e0e0")
            password_label.configure(background="#e0e0e0")


def on_closing():
    if messagebox.askokcancel("退出程序", "确定要退出WiFi密码查看器吗?"):
        root.destroy()


root = tk.Tk()
root.title("WiFi密码查看器")
root.protocol("WM_DELETE_WINDOW", on_closing)

# 创建表头
ssid_header = ttk.Label(root, text="WiFi名称", font=("Arial", 12, "bold"))
ssid_header.grid(row=0, column=0, padx=10, pady=5)

password_header = ttk.Label(root, text="WiFi密码", font=("Arial", 12, "bold"))
password_header.grid(row=0, column=1, padx=10, pady=5)

show_wifi_profiles()

root.update_idletasks()

# 设置窗口的大小
window_width = root.winfo_width()
window_height = root.winfo_height()

# 设置窗口的位置使其在屏幕中居中
position_x = int((root.winfo_screenwidth() - window_width) / 2)
position_y = int((root.winfo_screenheight() - window_height) / 2)

# 设置窗口的位置和大小
root.geometry(f"{window_width}x{window_height}+{position_x}+{position_y}")
root.minsize(window_width, window_height)

# 设置窗口的背景颜色
root.configure(background="#f0f0f0")

# 设置表头的背景颜色
ssid_header.configure(background="#f0f0f0")
password_header.configure(background="#f0f0f0")

root.mainloop()

运行结果:

2023-06-23T04:14:52.png

转换为exe可执行程序:

pyinstaller -F quickStart.py

生成有图标的exe程序,添加参数-icon:

pyinstaller --icon=logo.ico -F quickStart.py

注:py文件和用于做图标的照片需要存放于同一个文件夹中

【其它】在线ico图标转换工具:https://www.bitbug.net/

标签: WiFi密码查看, python

非特殊说明,本博所有文章均为博主原创。

评论啦~