2016-11-28 72 views
0

我在使用pyinstaller(或py2exe或cxfreeze)將以下python腳本綁定到單個可執行文件時遇到了一些麻煩。我只包括我一直試圖節省空間的pyinstaller代碼,但如果任何人有任何想法使其與任何其他程序一起工作,請隨時通知我。當我嘗試打開該EXE文件創建tkinter python腳本的EXE

pyinstaller --hidden-import=matplotlib --hidden-import=numpy --hidden-import=tkinter --windowed --one-file script.py 

我已經試過以上的變化,我不斷收到「無法執行腳本pyi_rth_pkgres」的錯誤。

import matplotlib 
matplotlib.use('TkAgg') 
import numpy as np 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 
import matplotlib.pyplot as plt 
import tkinter as tk 

class Application(tk.Frame): 
    def __init__(self, master=None): 
     tk.Frame.__init__(self,master) 
     self.createWidgets() 

    def createWidgets(self): 
     fig=plt.figure(figsize=(8,8)) 
     ax=fig.add_axes([0.1,0.1,0.8,0.8],polar=True) 
     canvas=FigureCanvasTkAgg(fig,master=root) 
     canvas.get_tk_widget().grid(row=0,column=1) 
     canvas.show() 

     self.plotbutton=tk.Button(master=root, text="plot", command=lambda:self.plot(canvas,ax)) 
     self.plotbutton.grid(row=0,column=0) 

    def plot(self,canvas,ax): 
     c = ['r','b','g'] # plot marker colors 
     ax.clear()   # clear axes from previous plot 
     for i in range(3): 
      theta = np.random.uniform(0,360,10) 
      r = np.random.uniform(0,1,10) 
      ax.plot(theta,r,linestyle="None",marker='o', color=c[i]) 
      canvas.draw() 

root=tk.Tk() 
app=Application(master=root) 
app.mainloop() 

我知道這是很模糊的,但我想我扔在那裏,看看是否任何人有任何想法,我要去哪裏錯了/可能是什麼問題。

謝謝!

編輯:我使用Python 3.5,但如果任何人都可以得到它與一個不同的版本,這將是偉大的。我試過其他版本,但仍然沒有運氣。

+0

什麼Python版本是您使用與運行?由於所使用的dll的變化,3.5+有一些問題。 –

+0

我主要使用3.5+,儘管我通過virtualenv嘗試了其他版本(2.7,3.4),沒有任何運氣 –

回答

0

我會參考本教程使用cx_freeze。我相信CX凍結不會與蟒蛇3.5工作,但我已經在Python用它的可執行文件2.7

tutorial

0

下面的代碼片段應該編譯代碼,至少如果你使用的是Windows 。正如Tomasz Plaskota提到的,Python 3.5中的cx_freeze和tkinter有一些新問題,需要通過自定義調整來解決。在所有文件路徑中,您必須交換

C:\Program Files (x86)\Python 3.5 

到您的python路徑。

from cx_Freeze import setup, Executable, hooks 
# NOTE: you can include any other necessary external imports here aswell 


import os 
os.environ['TCL_LIBRARY'] = r"C:\Program Files (x86)\Python 3.5\tcl\tcl8.6" 
os.environ['TK_LIBRARY'] = r"C:\Program Files (x86)\Python 3.5\tcl\tk8.6" 

includefiles = [r"C:\Program Files (x86)\Python 3.5\DLLs\tcl86t.dll",r"C:\Program Files (x86)\Python 3.5\DLLs\tk86t.dll"] # include any files here that you wish 
includes = ['tkinter.filedialog'] 
excludes = [] 
packages = [] 

exe = Executable(
# what to build 
    script = "cx_freeze_example.py", # the name of your main python script goes here 
    initScript = None, 
    base = 'Win32GUI', # if creating a GUI instead of a console app, type "Win32GUI" 
    targetName = "cx_freeze_example.exe", # this is the name of the executable file 
    icon = None # if you want to use an icon file, specify the file name here 
) 

setup(
# the actual setup & the definition of other misc. info 
    name = "cx_freeze example", # program name 
    version = "1.0", 
    description = '', 
    author = "", 
    options = {"build_exe": {"excludes":excludes,"packages":packages, 
     "include_files":includefiles,"includes": includes}}, 
    executables = [exe] 
) 

此代碼應在同一文件夾作爲主腳本和