代码之家  ›  专栏  ›  技术社区  ›  Gazdea

如何从Python优化已编译的应用程序

  •  0
  • Gazdea  · 技术社区  · 2 年前

    我写了一个小计算器,并决定尝试创建一个EXE和APK文件。我有一个问题:为什么在将其编译成EXE后,文件大小增加了这么多,并且在启动时似乎正在加载一些东西?一般来说,我该如何优化它?

    我这么做是出于对科学的好奇,所以请用更简单的术语向我解释。我的术语可能有误,请随时更正。我希望这篇文章足够了,这样我就不必写更多了。

    我使用auto-py-to-exe编译了它。

    from tkinter import *
    import tkinter.font as tkFont
    
    
    def button_click(number):
        current = entry.get()
        entry.delete(0, END)
        entry.insert(0, str(current) + str(number))
    
    def button_clear():
        entry.delete(0, END)
    
    def button_clear_number():
         current = entry.get()
         entry.delete(0, END)
         entry.insert(0, str(current[:-1]))
    
    def button_equal(event=0):
        try:
            number = entry.get()
            entry.delete(0, END)
            if number:
                result = eval(f"{number}")
                entry.insert(0, result)
        except:
            entry.insert(0, number)
            print("Error")
    
    
    root = Tk()
    root.title("Калькулятор")
    root.configure(bg='gray')
    root.resizable(False, False)
    
    pixel = PhotoImage(width=50, height=50)
    
    valide_key = ["1","2","3","4","5","6","7","8","9","0",".","+","-","*","/","(",")"]
    
    # функция для проверки ввода
    def validate_entry(text):
        if all(char in valide_key for char in text) or text == "":
            return True
        else:
            return False
    
    entry = Entry(root, width=15, font=tkFont.Font(size=20), validate="key", validatecommand=(root.register(validate_entry), "%S"))
    entry.grid(row=0, column=0, columnspan=4)
    entry.focus_set()
    entry.bind("<Return>", button_equal)
    
    buttons = [
        ("7", 3, 0),
        ("8", 3, 1),
        ("9", 3, 2),
        ("4", 4, 0),
        ("5", 4, 1),
        ("6", 4, 2),
        ("1", 5, 0),
        ("2", 5, 1),
        ("3", 5, 2),
        ("0", 6, 0),
        (".", 6, 1),
        ("+", 6, 3),
        ("-", 5, 3),
        ("*", 4, 3),
        ("/", 3, 3),
        ("(", 2, 0),
        (")", 2, 1)
    ]
    
    for button_text, row, column in buttons:
        button = Button(root,image=pixel,compound="c", text=button_text, command=lambda text=button_text: button_click(text))
        button.grid(row=row, column=column)
    
    clear_button = Button(root,image=pixel,compound="c", text="C",  command=button_clear)
    clear_button.grid(row=2, column=2)
    
    clear_number = Button(root,image=pixel,compound="c", text="<",  command=button_clear_number)
    clear_number.grid(row=2, column=3)
    
    equal_button = Button(root,image=pixel,compound="c", text="=",  command=button_equal)
    equal_button.grid(row=6, column=2)
    
    root.mainloop()
    

    Program

    1 回复  |  直到 2 年前
        1
  •  1
  •   JRiggles    2 年前

    当您使用auto-py-to-exe(在后台使用pyinstaller)创建可执行文件时,该可执行文件必然具有tkinter 捆绑在一起的Python解释器(以及它所需要的任何其他依赖项)。您的应用程序的主 *.py 文件可能很小,但支持/运行它所需的代码并不小。

    您可以通过不使用 --onefile 选项,但这最终使它的可移植性降低,因为它的依赖项现在与 *.exe 它本身

    编辑: 为了澄清,如果你制作了一个tkinter应用程序,tkinter将被捆绑。。。它并不总是包括在内。