【问题标题】:Can regular python input text be carried over when converted to .exe?转换为 .exe 时可以结转常规 python 输入文本吗?
【发布时间】:2018-06-28 21:36:00
【问题描述】:

我正在开发一个播放音乐的程序,如果你按下按钮也会播放声音效果。我正计划制作它,以便它可以在任何计算机上运行,​​具有任何音效和音乐文件。所以我尝试用cx_freeze 将其设为.exe,但它只是在瞬间打开命令提示符,然后再次关闭它。有什么建议吗?

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 生成的可执行文件拖到命令提示符中,并将输出粘贴到您的问题中。有很多事情可能会出错。
  • 试过拖进去,一样的,闪开后又关上。
  • 放置一个打印语句和文件的最开始,再次cx_freeze,然后再次将其拖入终端。看看它是否打印任何消息。
  • 嗯,命令提示符的问题现在已经解决了,但是像往常一样,在编码时出现了一个新问题 "File "C:\Python 3.6\lib\tkinter_init_ .py", line 36, in import _tkinter # If this failed your Python may not be configured for Tk ImportError: DLL load failed: The specified module could not be found."
  • 我猜你的构建是不完整的。也许this question 会帮助您正确构建可执行文件。

标签: python tkinter pygame cx-freeze


【解决方案1】:

我无法对 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)

希望这会有所帮助。

【讨论】:

  • 按照你说的安装,似乎没有问题,但是当我尝试使用它时,它说'pytinstaller:'pytinstaller'一词不被识别为cmdlet的名称,函数、脚本文件或可运行的程序。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。'
  • 谢谢!我刚刚更改了路径,解决了它没有显示的问题!但是,现在出现一个错误:“soundObj=pygame.mixer.Sound(musicDir) pygame.error: Unable to open file 'C:\\Users\\Will\\Desktop\\program\\musicfile.wav'”有什么建议吗?
  • 我的猜测是由于某种原因 Python 在字符串中加入了双反斜杠,因为我使用“\\”进行了测试,这给了我“\\\\”和“\\\\” ",这给了我 "\\\\\\"。
  • 我添加了文件对话框的语法,它将帮助解决上述错误。如果有帮助,请接受答案。乐意效劳! :)
猜你喜欢
  • 2021-04-21
  • 2019-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 2019-02-09
  • 2016-11-21
相关资源
最近更新 更多