创建tkinter python脚本的EXE

时间:2016-11-28 14:13:08

标签: python matplotlib tkinter

我在使用pyinstaller(或py2exe或cxfreeze)将以下python脚本捆绑到单个可执行文件时遇到了一些麻烦。我只包括我一直试图节省空间的pyinstaller代码,但如果有任何想法让它与任何其他程序一起工作,请随时告诉我。

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

我尝试过以上的变体,当我尝试打开EXE文件时,我一直收到“执行脚本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,但是如果有人能够使用不同的版本,那就太棒了。我尝试了其他版本,但仍然没有运气。

2 个答案:

答案 0 :(得分:1)

这是一个应该编译代码的代码片段,至少在使用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]
)

此代码应与主脚本位于同一文件夹中,并使用

运行
python compile_example.py build

答案 1 :(得分:0)

我将使用cx_freeze来参考本教程。我相信cx freeze不能用于python 3.5,但我已经将它用于python 2.7中的可执行文件

tutorial