【问题标题】:Creating EXE of tkinter python script创建 tkinter python 脚本的 EXE
【发布时间】:2016-11-29 18:51:41
【问题描述】:

我在使用 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,但如果有人可以让它与不同的版本一起工作,那也很棒。我尝试了其他版本,但仍然没有运气。

【问题讨论】:

  • 你用的是什么python版本?由于使用的 dll 的变化,3.5+ 存在一些问题。
  • 我主要使用的是 3.5+,虽然我已经通过 virtualenv 尝试了其他版本(2.7、3.4),但没有任何运气

标签: python matplotlib tkinter


【解决方案1】:

这是一个可以编译你的代码的代码 sn-p,至少在你使用 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

【讨论】:

    【解决方案2】:

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

    tutorial

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      相关资源
      最近更新 更多