You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter按钮调用外部py脚本,如何合并代码实现单文件编译为exe

解决方案

完全可以将两个脚本合并为单文件,合并后无需依赖额外的wlp1.py,也不需要通过子进程调用外部脚本,适配单exe编译需求。

实现逻辑

  • 将wlp1.py中查询WiFi配置的逻辑封装为返回结果字符串的函数
  • 替换原有clickstart函数的子进程调用逻辑,直接调用封装好的函数获取结果插入文本框

合并后的完整代码

from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import sys
import os
import tkinter.scrolledtext as tkst
import subprocess
import re

root = Tk()
root.title("WLP")
root.geometry("975x475")
root.configure(bg='#0c0c0c')
root.resizable(0,0)

# 封装原wlp1.py的逻辑,直接返回结果字符串
def get_wifi_profiles():
    command_output = subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output=True).stdout.decode()
    profile_names = re.findall("All User Profile     : (.*)\r", command_output)
    wifi_list = []
    result = ""
    if len(profile_names) != 0:
        for name in profile_names:
            wifi_profile = {}
            profile_info = subprocess.run(["netsh", "wlan", "show", "profile", name], capture_output=True).stdout.decode()
            if re.search("Security key           : Absent", profile_info):
                continue
            else:
                wifi_profile["ssid"] = name
                profile_info_pass = subprocess.run(["netsh", "wlan", "show", "profile", name, "key=clear"], capture_output=True).stdout.decode()
                password = re.search("Key Content            : (.*)\r", profile_info_pass)
                if password == None:
                    wifi_profile["password"] = None
                else:
                    wifi_profile["password"] = password[1]
                wifi_list.append(wifi_profile)
    # 拼接结果字符串
    for item in wifi_list:
        result += f"{item}\n"
    return result

def clickstart():
    output = get_wifi_profiles()
    Textbox.insert(1.0, output)

Label1 = Label(root, text="WLP", bg="#0c0c0c", fg="#C0C0C0")
Label2 = Label(root, text="********************", bg="#0c0c0c", fg="#C0C0C0")
Label3 = Label(root, text="*********", bg="#0c0c0c", fg="#C0C0C0")
Button1 = Button(root, text="START", bg="#0c0c0c", fg="#C0C0C0", command=clickstart)
Textbox = tkst.ScrolledText(root, width=85, height=10, bg="#0c0c0c", fg="#C0C0C0")

Label1.grid(row=5, column=5, pady=15)
Label2.grid(row=7, column=5, pady=15)
Label3.grid(row=85, column=0, pady=125)
Button1.grid(row=10, column=5, pady=15)
Textbox.grid(row=17, column=5)

root.mainloop()

内容的提问来源于stack exchange,提问作者Vo1d

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 03:15:03