gpt4 book ai didi

python-3.x - 将Tkinter转换为exe

转载 作者:行者123 更新时间:2023-12-04 22:55:41 27 4
gpt4 key购买 nike

目前,我正在尝试将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()

第二个脚本:
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文件时,我收到这样的消息,即该文件不存在。所有文件都位于正确的文件夹中。那不是问题。

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

提前谢谢了。

最佳答案

正如标题中所说的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函数的主文件。

关于python-3.x - 将Tkinter转换为exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48299396/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com