Bee*_*tje 4 tkinter cx-freeze python-3.x
目前,我正在尝试将tkinter python脚本转换为exe文件。我正在使用cx_freeze来做到这一点。当我尝试添加其他文件时,它某种程度上不再起作用。在下面我使用的最小示例中,您可以看到我使用的方法。
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()
Run Code Online (Sandbox Code Playgroud)
第二个脚本:
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)
Run Code Online (Sandbox Code Playgroud)
当我尝试生成exe文件时,当我不添加图标时,它将起作用。但是,如果我尝试添加图标,该exe文件将无法再打开。此外,当我尝试添加数据库excel文件时,我收到这样的消息,即该文件不存在。所有文件都位于正确的文件夹中。那不是问题。
有人可以帮我解决这个问题吗?
提前谢谢了。
正如标题所说Converting tkinter to exe我相信pyinstaller值得在这种情况下提。
在Internet 上有一些关于哪种更好的pyinstaller或cx_Freeze的争论,但是我发现更pyinstaller简单了,它对我很有效tkinter。一衬在cmd中:
pyinstaller.exe --onefile --icon=myicon.ico main.py
Run Code Online (Sandbox Code Playgroud)
--onefile 选项产生一个输出文件,而不是多个。
--icon 将连接您选择的图标。
main.py是具有该main功能的主文件。
| 归档时间: |
|
| 查看次数: |
14153 次 |
| 最近记录: |