转换为 .exe 时是否可以保留常规 python 输入文本?

Can regular python input text be carried over when converted to .exe?

我正在开发一个播放音乐的程序,如果您按下一个按钮,它还会播放音效。我计划制作它以便它可以在任何计算机上运行,​​具有任何音效和音乐文件。所以我试着把它变成 .execx_freeze,但它只是一瞬间打开命令提示符,然后再次关闭它。有什么建议吗?

import pygame, time
import tkinter as tk
from pygame.locals import *

print ("WARNING: The program only accepts .wav files.")
musicDir = input("What is the FULL address of the music? (e.g C:\Me\Music\music.wav) ")
soundDir = input("What is the FULL address of the music? (e.g C:\Me\Music\sound.wav) ")

pygame.init() # initialize the pygame

soundObj = pygame.mixer.Sound(musicDir)
soundObj.play(loops=-1)


def playSound():
    soundObj = pygame.mixer.Sound(soundDir)
    soundObj.play()


root = tk.Tk()
frame = tk.Frame(root)
frame.pack()

button = tk.Button(frame, 
               text="QUIT", 
               fg="red",
               command=quit)
button.pack(side=tk.LEFT)
sound = tk.Button(frame,
               text="Play Sound Effect",
               fg="green",
               command=playSound)
sound.pack(side=tk.LEFT)

instruction = tk.Button(root,
             text="Program by Will but really I just wanted another button because it looked cool.", 
             fg="blue")
instruction.pack(side=tk.BOTTOM)
root.mainloop()

我无法对 cx_freeze 执行相同的操作,而且我的代码在没有 .exe 的情况下也能完美运行。我在互联网上尝试了一些方法来解决问题,但都是徒劳的。

但是,如果您真的想制作 .exe,最好的选择之一是 pyinstaller。我一直在用它。

要安装:

sudo -H pip3 install pyinstaller

使用:

pyinstaller test_file.py

完成后,转到"dist"目录并进入以您的项目命名的文件夹。 在那里,会有相当多的文件。但是,会有一个 .exe 文件,就是您需要的那个。所以,点击它到运行.

如果你想输入一个文件的地址,不要使用条目,使用下面的(看起来不错,不会有错误):

from tkinter import filedialog as fd

filename = fd.askopenfilename()
if filename:
    print (filename)

希望对您有所帮助。