将tkinter转换为exe

时间:2018-01-17 10:56:50

标签: python-3.x tkinter cx-freeze

目前我正在尝试将我的tkinter python脚本转换为exe文件。我正在使用cx_freeze来做到这一点。当我尝试添加一个aditional文件时,它不知何故不再起作用了。在下面我使用的最小示例中,您可以看到我使用过的方法。

import tkinter as tk

import numpy.core._methods, numpy.lib.format 

class Main(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.geometry("700x400")
        self.wm_iconbitmap('test.ico')

        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand = True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, PageOne):

            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()        
        frame.update_page() # <-- update data on page when you click button

    def get_page(self, page_class):
        return self.frames[page_class]


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller 

        label1 = tk.Label(self, text="What are the sizes?")
        label1.pack()

        L1 = tk.Label(self, text="Length :")
        L1.pack()

        self.E1 = tk.Entry(self)
        self.E1.pack()

        button = tk.Button(self, text="Next", command=lambda: controller.show_frame(PageOne))
        button.pack()

    def update_page(self): # empty method but I need it
        pass   

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        label1 = tk.Label(self, text="You have insert")
        label1.pack()

        # create empty label at start
        self.label2 = tk.Label(self, text="")
        self.label2.pack()

        button = tk.Button(self, text="Back", command=lambda: controller.show_frame(StartPage))
        button.pack()

    def update_page(self):
        # update label when page is changed
        page1 = self.controller.get_page(StartPage) 
        var = page1.E1.get()
        self.label2['text'] = var


app = Main()
app.mainloop() 

第二个脚本:

import cx_Freeze
import sys
import matplotlib 
import os 
import numpy.core._methods
import numpy.lib.format

base = None 

if sys.platform=='win32':
    base = "Win32GUI"


executables = [cx_Freeze.Executable("Show_file.py")]    

cx_Freeze.setup(
        name = "Name",
        options = {"build_exe":{"packages":["tkinter","matplotlib"],"include_files":["test.ico"]}},
        version="0.01",
        executables=executables) 

当我尝试构建exe文件时,它在我不添加图标时起作用。但是,如果我尝试添加图标,exe将不再打开。此外,当我尝试添加数据库excel文件时,我收到这样一个文件不存在的消息。 Allo文件在正确的文件夹中。那不是问题。

有人可以帮我解决这个问题吗?

非常感谢提前。

2 个答案:

答案 0 :(得分:3)

正如标题所说Converting tkinter to exe我相信pyinstaller在这种情况下值得一提。

互联网上有一些关于哪个更好pyinstaller or cx_Freeze的辩论,但我发现pyinstaller更简单,它对tkinter开箱即用。 cmd中的单行:

pyinstaller.exe --onefile --icon=myicon.ico main.py

--onefile选项生成一个输出文件而不是多个输出文件。

--icon会连接您选择的图标。

main.py是您使用main功能的主文件。

答案 1 :(得分:0)

缺少tkinter运行时和库。要包括我建议使用os.environ()的那些,并使用include_files参数包含运行时,因为它们(简要地)描述了here

使用os.environ()很简单。例如,可以这样做:

os.environ["TCL_LIBRARY"] = "<PathToPython>\\Python\\Python36-32\\tcl\\tcl8.6"
os.environ["TK_LIBRARY"] = "<PathToPython>\\Python\\Python36-32\\tcl\\tk8.6"

接下来在include文件arguement中包含运行时(DLL):

    options = {"build_exe":{"packages":["tkinter","matplotlib"],"include_files":["test.ico", "<PathToPython>\\Python\\Python36-32\\DLLs\\tcl86t.dll", "<PathToPython>\\Python\\Python36-32\\DLLs\\tk86t.dll"]}},

现在您的整个设置脚本应如下所示:

import sys # Imports are automatically detected (normally) in the script to freeze
import os 

base = None 

os.environ["TCL_LIBRARY"] = "<PathToPython>\\Python\\Python36-32\\tcl\\tcl8.6"
os.environ["TK_LIBRARY"] = "<PathToPython>\\Python\\Python36-32\\tcl\\tk8.6"

if sys.platform=='win32':
    base = "Win32GUI"


executables = [cx_Freeze.Executable("Show_file.py")]    

cx_Freeze.setup(
        name = "Name",
        options = {"build_exe":{"packages":["tkinter","matplotlib"],"include_files":["test.ico", "<PathToPython>\\\\Python\\Python36-32\\DLLs\\tcl86t.dll", "<PathToPython>\\\\Python\\Python36-32\\DLLs\\tk86t.dll"]}},
        version="0.01",
        executables=executables) 

您不需要在设置脚本中使用的所有导入,cx_Freeze会自动检测它们。