【问题标题】:Pyinstaller - Failed to execute script pyi_rth__tkinterPyinstaller - 无法执行脚本 pyi_rth__tkinter
【发布时间】:2021-05-17 15:41:39
【问题描述】:

我正在尝试使用 pyinstaller 将我的 Python tkinter 记事本转换为 exe。 但是当我打开创建的 exe 文件时出现错误:

Failed to execute script pyi_rth__tkinter

我尝试将 pyinstaller 版本从 github 更改为 5.0 开发人员并导入 pkg_resources.py2_warn 但这不起作用,我得到了同样的错误。 我正在使用 python 3.9.1 这是我的代码:

  from tkinter import *
from tkinter import filedialog
from PIL import ImageTk
from PIL import Image


root = Tk()
root.geometry("600x375")
root.resizable(False, False)
root.title("NetNote")
root.iconbitmap("ikona.ico")
#liczba globalna zapisz
global open_status_name
open_status_name = False





def usuwanie():
    text.delete(1.0, END)
    global open_status_name
    open_status_name = False
    

def open_txt():
 
    text_file = filedialog.askopenfilename(title="Open Text File", filetypes=(("Text Files", "*.txt"),))
    if text_file:
        global open_status_name
        open_status_name = text_file
    text_file = open(text_file, "r")
    stuff = text_file.read()
    text.delete("1.0", END)
    text.insert(END, stuff)
    text_file.close()
#fukcja - save as
def save_as():
    text_file=filedialog.asksaveasfilename(defaultextension=".*", title="Zapisz jako", filetypes=(("Text Files", "*.txt"),))
    if text_file:
        name = text_file
        name = name.replace("","") 
        text_file = open(text_file, "w")
        text_file.write(text.get(1.0, END))
        text_file.close()


def save_file():
    global open_status_name
    if open_status_name:
        text_file = open(open_status_name, "w")
        text_file.write(text.get(1.0, END))
        text_file.close()
    else:
        save_as()

    
    
   

frame = LabelFrame(root, text="Menu",padx=2, pady=2)
frame.grid(row=0, column=0, sticky=S, padx=10, pady=10)



my_logo = Image.open("logo.png")

resized = my_logo.resize((110, 138), Image.ANTIALIAS)
my_logo = ImageTk.PhotoImage(file="logo.png")
new_pic = ImageTk.PhotoImage(resized)


my_label = Label(root, image=new_pic)
my_label.grid(row=0, column=0, sticky=N, pady=20)



przycisk1 = Button(frame, text="Nowy",width=8, height=1, command=usuwanie,)
przycisk1.grid(row=0,column=0, sticky=N, padx=10, pady=10,)




przycisk2 = Button(frame, text="Otwórz", width=8,height=1, command=open_txt)
przycisk2.grid(row=1, column=0,)
root.mainloop



przycisk3 = Button(frame, text="Zapisz jako", width=8,height=1,command=save_as)
przycisk3.grid(row=2, column=0, padx=10, pady=10)



text = Text(root, height=20, width=57, font=("Calibri, 11"))
text.grid(row=0, column=1, padx=10, pady=10)



przycisk4 = Button(frame, text="Zapisz", width=8, height=1, command=save_file)
przycisk4.grid(row=3, column=0, pady=(0, 10))


root.mainloop

【问题讨论】:

  • 确保ikona.ico等图片和.exe文件在同一个文件夹中
  • 是的,我试过了,但我仍然得到同样的错误
  • 我记得 pyinstaller 的文档提供了如何将额外文件 resources 添加到 .exe 的信息。它也有页面"when it goes wrong"。所以首先阅读文档。 (PL: najpierw sprawdź dokumentacje pyinstallera)
  • 首先在控制台中运行以查看完整的错误消息。并始终将完整的错误消息(从“Traceback”一词开始)作为文本(不是截图,不是链接到外部门户)有问题(不是评论)。还有其他有用的信息。
  • 在函数外部创建的每个变量都是全局的 - 所以使用 global open_status_name 外部函数是没用的。我们在函数内部使用global 来通知函数它必须将值(=)分配给外部/全局变量,而不是创建局部变量。

标签: python user-interface tkinter pyinstaller


【解决方案1】:

你可以试试这个方法:

pyinstaller --onefile -w

即使我已经使用 Tkinter 创建了一个记事本,这种方法也为我完成了工作

【讨论】:

  • 这行得通吗?如果它不起作用,请尝试 cx_Freeze
  • 我仍然收到同样的错误,但现在处于窗口模式。我会尝试使用 cx_freeze。
猜你喜欢
  • 1970-01-01
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-30
  • 2021-02-14
  • 2017-02-25
  • 1970-01-01
相关资源
最近更新 更多