python 代码 installer 封装 闪退

#统信UOS下,使用pycharm写好的代码运行没有问题,但是用pyinstaller封装后,点击运行直接闪退。请求帮助--代码如下:

import json
import os
from tkinter import *
from tkinter import messagebox, ttk
import pandas as pd
#  import openpyxl

# 读取配置文件中的表格保存路径
with open("config.json", "r", encoding="utf-8") as f:
    FILE_PATH = json.load(f).get("表格保存路径", "example.xlsx")

# 列名列表
COL_LIST = ["序号", "信息标题", "约稿时限", "约稿单位", "报送单位", "采用情况", "备注"]

# 创建主窗口
root = Tk()

root.title("登记工具")
root.geometry("800x450")  # 设置窗口大小

# 设置列的权重,使其可以自动调整大小
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=1)

# 在主窗口中添加标签和输入框
for i, text in zip(range(1, len(COL_LIST)), COL_LIST[1:]):
    Label(root, text=text).grid(row=i, column=0)

e1 = Entry(root, textvariable=StringVar(), width=70)
e2 = Entry(root, textvariable=StringVar(), width=70)
e3 = Entry(root, textvariable=StringVar(), width=70)
e4 = Entry(root, textvariable=StringVar(), width=70)
e5 = Entry(root, textvariable=StringVar(), width=70)
e6 = Entry(root, textvariable=StringVar(), width=70)

e1.grid(row=1, column=1, padx=10, pady=5)
e2.grid(row=2, column=1, padx=10, pady=5)
e3.grid(row=3, column=1, padx=10, pady=5)
e4.grid(row=4, column=1, padx=10, pady=5)
e5.grid(row=5, column=1, padx=10, pady=5)
e6.grid(row=6, column=1, padx=10, pady=5)

# 添加下拉选择框
Label(root, text="搜索列选择").grid(row=9, column=0)
combo = ttk.Combobox(root, width=67)
combo["values"] = COL_LIST[1:] + ["全局"]
combo.set("信息标题")
combo.grid(row=9, column=1, padx=10, pady=5)

# 添加“添加信息”按钮
def show():
    try:
        df = pd.read_excel(FILE_PATH)
    except FileNotFoundError:
        messagebox.askokcancel("警告", "文件不存在是否创建")
        df = pd.DataFrame(columns=COL_LIST)
        df.to_excel(FILE_PATH, index=False)
    data = {
        "序号": len(df) + 1,
        "信息标题": e1.get(),
        "约稿时限": e2.get(),
        "约稿单位": e3.get(),
        "报送单位": e4.get(),
        "采用情况": e5.get(),
        "备注": e6.get(),
    }
    df.loc[len(df)] = data
    df.to_excel(FILE_PATH, index=False)

Button(root, text="添加信息", width=10, command=show).grid(
    row=7, column=1, sticky=E, padx=10, pady=5
)

# 添加搜索框和搜索按钮
Label(root, text="搜索关键词:").grid(row=8, column=0)
e7 = Entry(root, textvariable=StringVar(), width=70)
e7.grid(row=8, column=1, padx=10, pady=5)

def searchData():
    try:
        df = pd.read_excel(FILE_PATH)
    except FileNotFoundError:
        res = messagebox.askokcancel("警告", "文件不存在是否创建")
        if res:
            df = pd.DataFrame(columns=COL_LIST)
            df.to_excel(FILE_PATH, index=False)
        else:
            messagebox.showinfo("请确保文件存在")
            exit()

    search_keyword = e7.get().lower()

    if combo.get() == "全局":
        # 全局搜索
        result = df[df.applymap(lambda x: search_keyword in str(x).lower()).any(axis=1)]
    else:
        # 按指定列搜索
        result = df[df[combo.get()].str.contains(search_keyword, case=False)]

    if len(result) > 0:
        # 显示搜索结果
        popup_window = Toplevel(root)
        popup_window.title("搜索结果")

        # 创建树状视图并设置列标题
        tree = ttk.Treeview(popup_window)
        tree["columns"] = COL_LIST
        tree.heading("#0", text="序号")
        for col in COL_LIST:
            tree.heading(col, text=col)

        # 插入搜索结果数据
        for i, row in result.iterrows():
            tree.insert("", "end", text=i + 1, values=list(row))

        tree.pack()
    else:
        messagebox.showinfo("搜索结果", "未找到匹配的数据")

Button(root, text="搜索", width=10, command=searchData).grid(
    row=10, column=1, sticky=E, padx=10, pady=5
)

# 运行主窗口循环
if __name__ == "__main__":
  root.mainloop()

#mainloop()

问题点: 打包后工具闪退
分析思路: 第九行的代码,文件路径没有用相对路径,打包后会出错.

import json
import os
from tkinter import *
from tkinter import messagebox, ttk
import pandas as pd
#  import openpyxl
 
# 读取配置文件中的表格保存路径  添加./(当前目录下)
with open("./config.json", "r", encoding="utf-8") as f:
    FILE_PATH = json.load(f).get("表格保存路径", "./example.xlsx")

有什么报错吗?

【相关推荐】



  • 这篇博客: python文件生成exe之在pycharm使用pyinstaller指令中的 问题引出 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:

    之前我在电脑上用python搞了一个小程序(很简单的,就不细讲),但是等到我想把这个py文件生成exe发给别人看一看的时候,我却发现我的电脑上用cmd安装老是出现一些问题,真的就是试了一个晚上的方法,pip也更新了、各种插件也下载了、后面pyinstaller的文件也下载了(可惜没找到位置去安装,装不好),于是我发现了可以在pycharm上安装的方法。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

打包好以后,文件夹是它的依赖,exe运行就可以了

img

可能是有没打包进包内的依赖,建议以带命令界面打包代码,然后设置一个try except,保留住闪退前的界面,上面会有报错信息,根据报错信息就能找到问题,也可以直接使用录屏,然后翻出来闪退前的最后输出代码,可以试一下

不要双击运行,先打开 CMD 命令行界面,然后在界面中输入你的 exe 可执行文件名称进行运行就不会闪退了。

建议使用控制台运行,看看打印的报错信息,再针对性的解决。

在使用pyinstaller封装代码时,可能会遇到一些问题。以下是一些可能导致闪退的常见问题和解决方法:

  1. 缺少依赖项:pyinstaller封装代码时,可能会漏掉一些依赖项。你可以尝试使用--hidden-import参数来手动添加缺少的依赖项。例如,如果你使用了openpyxl库,可以尝试添加--hidden-import=openpyxl参数。

  2. 文件路径问题:在你的代码中,有一个文件路径config.json。确保该文件与封装后的可执行文件在同一目录下。如果不在同一目录下,你可以尝试使用绝对路径来访问该文件。

  3. tkinter兼容性问题:在一些操作系统上,使用pyinstaller封装的tkinter应用程序可能会出现问题。你可以尝试使用--windowed参数来创建一个窗口应用程序,而不是一个控制台应用程序。例如,使用pyinstaller --windowed your_script.py来封装代码。

  4. 异常处理:在你的代码中,有一些异常处理逻辑。确保在封装后的可执行文件中,异常处理逻辑能够正常工作。你可以尝试在代码中添加一些日志记录,以便在闪退时查看日志文件以获取更多信息。

希望这些解决方法能够帮助你解决问题