做網(wǎng)站一定要學(xué)java嗎寫軟文平臺
本章教程,主要分享一個本地wifi密碼查看器,用python實(shí)現(xiàn)的,感興趣的可以試一試。
具體代碼
import subprocess # 導(dǎo)入 subprocess 模塊,用于執(zhí)行系統(tǒng)命令
import tkinter as tk # 導(dǎo)入 tkinter 模塊,用于創(chuàng)建圖形用戶界面
from tkinter import messagebox, ttk # 從 tkinter 模塊中導(dǎo)入 messagebox 和 ttk 子模塊def get_wifi_passwords():"""獲取本地計(jì)算機(jī)上所有已連接過的 WiFi 配置文件及其密碼。"""try:# 執(zhí)行命令獲取所有 WiFi 配置文件的列表profiles_data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8', errors='ignore')# 解析輸出,提取配置文件名稱profiles = [line.split(':')[1].strip() for line in profiles_data.split('\n') if "All User Profile" in line]wifi_passwords = [] # 存儲 WiFi 名稱和密碼的列表# 遍歷每個配置文件,獲取密碼for profile in profiles:profile_info = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', profile, 'key=clear']).decode('utf-8', errors='ignore')password_lines = [line.split(':')[1].strip() for line in profile_info.split('\n') if "Key Content" in line]password = password_lines[0] if password_lines else "N/A" # 如果沒有密碼,則顯示 "N/A"wifi_passwords.append((profile, password))return wifi_passwordsexcept Exception as e:# 如果發(fā)生錯誤,顯示錯誤信息messagebox.showerror("錯誤", f"發(fā)生錯誤: {str(e)}")return []def copy_password(event):"""復(fù)制選中的 WiFi 密碼到剪貼板。"""selected_item = tree.selection()[0]password = tree.item(selected_item, 'values')[1]root.clipboard_clear()root.clipboard_append(password)messagebox.showinfo("信息", "密碼已復(fù)制到剪貼板")def center_window(window, width, height):"""將窗口顯示在屏幕中央。"""screen_width = window.winfo_screenwidth()screen_height = window.winfo_screenheight()x = (screen_width - width) // 2y = (screen_height - height) // 2window.geometry(f'{width}x{height}+{x}+{y}')# 創(chuàng)建主窗口
root = tk.Tk()
root.title("WiFi 密碼查看器") # 設(shè)置窗口標(biāo)題
window_width = 400
window_height = 300
root.geometry(f'{window_width}x{window_height}') # 設(shè)置窗口大小
center_window(root, window_width, window_height) # 窗口居中顯示# 創(chuàng)建表格
tree = ttk.Treeview(root, columns=('SSID', '密碼'), show='headings')
tree.heading('SSID', text='WiFi名稱', anchor='center')
tree.heading('密碼', text='WiFi密碼', anchor='center')
tree.column('SSID', anchor='center')
tree.column('密碼', anchor='center')
tree.pack(fill=tk.BOTH, expand=True)# 設(shè)置表格樣式
style = ttk.Style()
style.configure('Treeview', rowheight=25)
style.configure('Treeview.Heading', font=('Arial', 12, 'bold'))# 獲取 WiFi 密碼并顯示在表格中
wifi_passwords = get_wifi_passwords()
for wifi, password in wifi_passwords:tree.insert('', tk.END, values=(wifi, password))# 綁定雙擊事件,雙擊表格中的一行即可復(fù)制密碼
tree.bind('<Double-1>', copy_password)# 啟動主事件循環(huán)
root.mainloop()
點(diǎn)擊wifi名稱行,可以快速復(fù)制wifi密碼到粘貼板上。