首页 > 解决方案 > 使用 pyinstaller 导出 tkinter python 应用程序时出错?

问题描述

我正在尝试使用 pyinstaller 将我的 tkinter 应用程序导出为独立的可执行文件。

我使用 autpy-to-exe GUI 来运行它。命令如下

pyinstaller --noconfirm --onedir --windowed --debug "all"  "D:/Internship/keras-video-classification/exe/predictGUI.py"
Running auto-py-to-exe v2.7.8
Building directory: C:\Users\Slaye\AppData\Local\Temp\tmpf0x1a0dk
Provided command: pyinstaller --noconfirm --onedir --windowed --debug "all"  "D:/Internship/keras-video-classification/exe/predictGUI.py"
Recursion Limit is set to 5000
Executing: pyinstaller --noconfirm --onedir --windowed --debug all D:/Internship/keras-video-classification/exe/predictGUI.py --distpath C:\Users\Slaye\AppData\Local\Temp\tmpf0x1a0dk\application --workpath C:\Users\Slaye\AppData\Local\Temp\tmpf0x1a0dk\build --specpath C:\Users\Slaye\AppData\Local\Temp\tmpf0x1a0dk

86827 INFO: PyInstaller: 4.0
86837 INFO: Python: 3.8.2
86845 INFO: Platform: Windows-10-10.0.18362-SP0
86856 INFO: wrote C:\Users\Slaye\AppData\Local\Temp\tmpf0x1a0dk\predictGUI.spec
86874 INFO: UPX is not available.
86909 INFO: Extending PYTHONPATH with paths
['D:\\Internship\\keras-video-classification\\exe',
 'C:\\Users\\Slaye\\AppData\\Local\\Temp\\tmpf0x1a0dk']
86963 INFO: checking Analysis
86970 INFO: Building Analysis because Analysis-00.toc is non existent
86977 INFO: Initializing module dependency graph...
87001 INFO: Caching module graph hooks...
87065 INFO: Analyzing base_library.zip ...
95434 INFO: Processing pre-find module path hook distutils from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
95446 INFO: distutils: retargeting to non-venv dir 'c:\\python\\python38-32\\lib'
103510 INFO: Caching module dependency graph...
103884 INFO: running Analysis Analysis-00.toc
103938 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\python\python38-32\python.exe
104425 INFO: Analyzing D:\Internship\keras-video-classification\exe\predictGUI.py
112731 INFO: Processing pre-find module path hook site from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-site.py'.
112738 INFO: site: retargeting to fake-dir 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\fake-modules'
118214 INFO: Processing pre-safe import module hook setuptools.extern.six.moves from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module\\hook-setuptools.extern.six.moves.py'.
125640 INFO: Processing pre-safe import module hook urllib3.packages.six.moves from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module\\hook-urllib3.packages.six.moves.py'.
128195 INFO: Processing module hooks...
128202 INFO: Loading module hook 'hook-certifi.py' from 'c:\\python\\python38-32\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
128229 INFO: Loading module hook 'hook-cv2.py' from 'c:\\python\\python38-32\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
128250 INFO: Loading module hook 'hook-eel.py' from 'c:\\python\\python38-32\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
128550 INFO: Loading module hook 'hook-pycparser.py' from 'c:\\python\\python38-32\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
128561 INFO: Loading module hook 'hook-distutils.py' from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks'...
128578 INFO: Loading module hook 'hook-encodings.py' from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks'...
128821 INFO: Loading module hook 'hook-gevent.py' from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks'...
129953 INFO: Determining a mapping of distributions to packages...
140515 WARNING: Unable to find package for requirement zope.interface from package gevent.
140523 WARNING: Unable to find package for requirement greenlet from package gevent.
140537 WARNING: Unable to find package for requirement zope.event from package gevent.
140551 INFO: Packages required by gevent:
['setuptools', 'cffi']
142046 INFO: Loading module hook 'hook-lib2to3.py' from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks'...
142244 INFO: Loading module hook 'hook-numpy.core.py' from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks'...
142511 INFO: Loading module hook 'hook-numpy.py' from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks'...
142524 INFO: Loading module hook 'hook-pkg_resources.py' from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks'...
143186 INFO: Processing pre-safe import module hook win32com from 'c:\\python\\python38-32\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\pre_safe_import_module\\hook-win32com.py'.
143336 INFO: Processing pre-safe import module hook win32com from 'c:\\python\\python38-32\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\pre_safe_import_module\\hook-win32com.py'.
143952 WARNING: Hidden import "pkg_resources.py2_warn" not found!
143961 WARNING: Hidden import "pkg_resources.markers" not found!
143981 INFO: Excluding import '__main__'
143994 INFO:   Removing import of __main__ from module pkg_resources
144006 INFO: Loading module hook 'hook-scipy.py' from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks'...
144037 INFO: Loading module hook 'hook-setuptools.py' from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks'...
145293 INFO: Loading module hook 'hook-sysconfig.py' from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks'...
145301 INFO: Loading module hook 'hook-xml.dom.domreg.py' from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks'...
145315 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks'...
145334 INFO: Loading module hook 'hook-xml.py' from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks'...
145351 INFO: Loading module hook 'hook-_tkinter.py' from 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks'...
145918 INFO: checking Tree
145928 INFO: Building Tree because Tree-00.toc is non existent
145947 INFO: Building Tree Tree-00.toc
146141 INFO: checking Tree
146155 INFO: Building Tree because Tree-01.toc is non existent
146177 INFO: Building Tree Tree-01.toc
146323 INFO: Looking for ctypes DLLs
146554 INFO: Analyzing run-time hooks ...
146582 INFO: Including run-time hook 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py'
146605 INFO: Including run-time hook 'c:\\python\\python38-32\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\rthooks\\pyi_rth_certifi.py'
146626 INFO: Including run-time hook 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgres.py'
146658 INFO: Including run-time hook 'c:\\python\\python38-32\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth__tkinter.py'
146705 INFO: Looking for dynamic libraries
149727 INFO: Looking for eggs
149739 INFO: Using Python library c:\python\python38-32\python38.dll
149761 INFO: Found binding redirects: 
[]
150841 INFO: Warnings written to C:\Users\Slaye\AppData\Local\Temp\tmpf0x1a0dk\build\predictGUI\warn-predictGUI.txt
151113 INFO: Graph cross-reference written to C:\Users\Slaye\AppData\Local\Temp\tmpf0x1a0dk\build\predictGUI\xref-predictGUI.html
151243 INFO: checking PYZ
151255 INFO: Building PYZ because PYZ-00.toc is non existent
151274 INFO: Building PYZ (ZlibArchive) C:\Users\Slaye\AppData\Local\Temp\tmpf0x1a0dk\build\predictGUI\PYZ-00.pyz
151294 INFO: Building PYZ (ZlibArchive) C:\Users\Slaye\AppData\Local\Temp\tmpf0x1a0dk\build\predictGUI\PYZ-00.pyz completed successfully.
151333 INFO: checking PKG
151346 INFO: Building PKG because PKG-00.toc is non existent
151364 INFO: Building PKG (CArchive) PKG-00.pkg
151835 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
151861 INFO: Bootloader c:\python\python38-32\lib\site-packages\PyInstaller\bootloader\Windows-32bit\runw_d.exe
151881 INFO: checking EXE
151902 INFO: Building EXE because EXE-00.toc is non existent
151923 INFO: Building EXE from EXE-00.toc
151945 INFO: Appending archive to EXE C:\Users\Slaye\AppData\Local\Temp\tmpf0x1a0dk\build\predictGUI\predictGUI.exe
152233 INFO: Building EXE from EXE-00.toc completed successfully.
152267 INFO: checking COLLECT
152284 INFO: Building COLLECT because COLLECT-00.toc is non existent
152309 INFO: Building COLLECT COLLECT-00.toc
162717 INFO: Building COLLECT COLLECT-00.toc completed successfully.

Moving project to: D:\Internship\keras-video-classification\exe\output
Complete.

我要使用的脚本如下

from tkinter import *
import tkinter as tk
from keras.models import load_model
import os
from collections import deque
import numpy as np
import pickle
import cv2
import easygui
import ctypes
import requests
import json
boolHasCode = False
def run(outputName,entryCode,modelP,inputP,labelP,outputP,x,y):
    print("in Run")
    if('xyz' in entryCode or 'amr' in entryCode):
        runPredictor(outputName,modelP,inputP,labelP,outputP,x,y)
        print("running")
    else:
        print("well not today")




def runGet(x,modelP,inputP,labelP,outputP,classWanted,userId):
    ownerID = ''
    print(x)
    #fileSize = os.stat(inputP).st_size
    #fileName = os.path.basename(inputP)
    URL = "https://xxx.wixsite.com/corona/_functions/myFunction/"
    PARAMS = {'userId':x}
    r = requests.get(url=URL, params=PARAMS)
    data = r.json()
    #print(test)
    URLx = "https://xxx.wixsite.com/corona/_functions/testG/"
    #print("SECOND GET")
    #print("User ID is: ", userId)
    #print("File Name is:", fileName)
    #print("size",fileSize)
    #print("video class",classWanted)
    #PARAMSx = {
     #   'vidUserId':userId,
      #  'videoName':fileName,
       # 'videoSize':fileSize,
        #'videoClass':classWanted
   # }
    #y = requests.get(url=URLx, params=PARAMSx)
    #print("Status code", y.status_code)
    #datax = y.json()
    if ((data.get('not available') == 'ok')):
        #ctypes.windll.user32.MessageBoxW(0, "Code already used!", "ERROR!", 1)
        print("test")
    else:
        for i in data.get('items'):
            print("Code:",i['code'])
            manga = i['code']
            testGET.insert(0,i['code'])
            #ctypes.windll.user32.MessageBoxW(0, manga, "Code!", 1)
        print("in else")

    #print(r.text)


def runPut():
    r = requests.put('https://xxx.wixsite.com/corona/_functions-dev/functionPut/', data={'used':'true'})
    print(r)
    print(r.content)

modelP = ""
labelP = ""
inputP = ""
outputP = ""
def getModelPath():
    global modelP
    modelP = easygui.fileopenbox()
def getLabelPath():
    global labelP
    labelP = easygui.fileopenbox()
def getInputPath():
    global inputP
    inputP = easygui.fileopenbox()
def getOutputPath():
    global outputP
    outputP = easygui.diropenbox()



HEIGHT = 700
WIDTH = 800
root = Tk()
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

frame=tk.Frame(root, bg='gray')
frame.place(relx=0.11,rely=0.1,relwidth=0.75, relheight=0.7)







entryModelPath = tk.Entry(frame)
entryModelPath.place(relx=0.7,rely=0.1,relwidth=0.3,relheight=0.05)
entryModelPath.insert(0,'Enter Output Video Name!')
#button.place(relx=0.7,rely=0.1,relwidth=0.2,relheight=0.1)


label = tk.Button(frame, text="Model Path", bg="yellow",command=lambda : getModelPath())
label.place(relx=0.7,rely=0.3,relwidth=0.25,relheight=0.05)
#----------------------------
button2 = tk.Button(frame, text="Upload Label Pickle Path", bg="yellow", command=lambda : getLabelPath())
button2.place(relx=0.7,rely=0.4,relwidth=0.25,relheight=0.05)

#entryLabelPath = tk.Entry(frame)
#entryLabelPath.place(relx=0.3,rely=0.1,relwidth=0.3,relheight=0.05)
#----------------------------
label2 = tk.Button(frame, text="Video Path", bg="yellow", command=lambda : getInputPath())
label2.place(relx=0.7,rely=0.5,relwidth=0.25,relheight=0.05)

entryVideoPath = tk.Entry(frame, text="Enter Preferred Class")
entryVideoPath.place(relx=0.7,rely=0.15,relwidth=0.3,relheight=0.05)
entryVideoPath.insert(0,"Enter Preferred Class")

label3 = tk.Button(frame, text="Output Path", bg="yellow",command=lambda : getOutputPath())
label3.place(relx=0.7,rely=0.6,relwidth=0.25,relheight=0.05)




entryOutputPath = tk.Entry(frame)
entryOutputPath.place(relx=0.7,rely=0.2,relwidth=0.3,relheight=0.05)
entryOutputPath.insert(0,'Enter Code!')

button = tk.Button(frame, text="Predict", bg='red', command=lambda: run(entryModelPath.get(),entryOutputPath.get(),modelP,inputP,labelP,outputP,entryVideoPath.get(),testGET.get()))
button.place(relx=0.05,rely=0.8,relwidth=0.3,relheight=0.1)


testGET = tk.Entry(frame, bg="white")
testGET.place(relx=0.05,rely=0.1,relwidth=0.3,relheight=0.05)
testGET.insert(0,"Enter User ID")

button = tk.Button(frame, text="GET CODE", bg='gray', command=lambda:runGet(testGET.get(),modelP,inputP,labelP,outputP,entryVideoPath.get(),testGET.get()))
button.place(relx=0.1,rely=0.25,relwidth=0.2,relheight=0.1)





def runPredictor(outputName,modelP,inputP,labelP,outputP,classWanted,userId):

    print(modelP)
    print(inputP)
    print(labelP)
    print(outputP)
    fileSize = os.stat(inputP).st_size
    fileName = os.path.basename(inputP)
    print("starting with post")
    print("File size is:",fileSize)
    print("File name is:",fileName)
    print("User ID is:", userId)
    print("Class wanted is: ", classWanted)
    fileSizeToString = str(fileSize)
    print(type(userId))
    print(type(fileName))
    print(type(fileSizeToString))
    print(type(classWanted))
    objtwo = {}

    url = 'https://xxx.wixsite.com/corona/_functions/ggFunction/'
    myobj = {
        'vidUserId':userId,
        'videoName':fileName,
        'videoSize':fileSizeToString,
        'videoClass':classWanted
    }
    header = {'Content-Type': 'application/x-www-form-urlencoded'}

    PARAMS = {'vidUserId':userId,'vidName':fileName,'videoSize':fileSizeToString,'videoClass':classWanted}


    z = requests.post(url, data=json.dumps(PARAMS), headers=header)
    data = z.json()
    print(json.dumps(data, indent=2, sort_keys=True))
    print(z.status_code)
    print("done with post")




    model = load_model(modelP)
    lb = pickle.loads(open(labelP, "rb").read())
    mean = np.array([123.68, 116.779, 103.939][::1], dtype="float32")
    Q = deque(maxlen=1)
    vs = cv2.VideoCapture(inputP)
    writer = None
    (W, H) = (None, None)
    while True:
        (grabbed,frame) = vs.read()
        if not grabbed:
            break
        if W is None or H is None:
            (H, W) = frame.shape[:2]
        output = frame.copy()
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame = cv2.resize(frame, (224, 224)).astype("float32")
        frame -= mean
        preds = model.predict(np.expand_dims(frame, axis=0))[0]
        Q.append(preds)
        results = np.array(Q).mean(axis=0)
        i = np.argmax(results)
        label = lb.classes_[i]
        text = "activity: {}".format(label)
        cv2.putText(output, text, (35, 50), cv2.FONT_HERSHEY_SIMPLEX,
                    1.25, (0, 255, 0), 5)
        if writer is None:
            # initialize our video writer
            sd =  outputP+outputName+".avi"
            print("output is to: ", sd)
            fourcc = cv2.VideoWriter_fourcc(*"MJPG")
            writer = cv2.VideoWriter(sd, fourcc, 30,
                                     (W, H), True)
        if (format(label) == classWanted):
            writer.write(output)
            print("writing output")
        cv2.imshow("Output", output)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break
    print("DONE")
    print("La done begad")
    writer.release()
    vs.release()
    #root.destroy()


root.mainloop()

#Tk().withdraw()
#filename = askopenfilename()
#print(filename)

当我尝试打开运行可执行文件的 GUI 时,我看到 3 个不同的错误。 错误1

错误2

错误3

请注意,我在 Anaconda 环境中运行。蟒蛇 3.7。

谢谢!

更新#1

尝试了 --console 而不是 --windowed 和控制台在执行时打印了以下内容

https://0bin.net/paste/W6g9L39K#Y5unpfaMHVGj90BOpf+r7JHA23n7uNYQ105gwS6FV+7

更新#3

捕获控制台中显示的错误。 控制台错误

标签: pythontkinterpyinstaller

解决方案


推荐阅读