【问题标题】:Convert python file that contains mp3 song into executable将包含mp3歌曲的python文件转换为可执行文件
【发布时间】:2020-08-17 16:23:48
【问题描述】:

所以我正在尝试使用 pyinstaller 将 tkinter 文件转换为可执行文件。我已经用图片完成了,但是当我创建包含某种音频的可执行文件时,它会显示“无法执行脚本 playmusic”(playmusic 是脚本名称)。我尝试使用 .wav 歌曲更改脚本名称,将 dist 文件夹中的 exe 与歌曲一起替换到脚本目录,但这些都不起作用。我也尝试过使用 auto-py-to-exe,但它不起作用。这首歌是用 pygame 播放的,并且在 VS Code 中没有任何问题。我使用的是 pyinstaller 还是其他模块都没关系。我在 Windows 10 上使用 python 3.8.2。

这是python脚本:

import tkinter as tk
from pygame import mixer, time

root = tk.Tk()
root.title("Music Player")
root.geometry("300x100+800+100")

label1=tk.Label(root, text="Start Song: ", fg='green', anchor="w")
label1.grid(row=0, column=0)

file = 'Ignite.mp3'
mixer.init()
mixer.music.load(file)

play = lambda: mixer.music.play()
button1 = tk.Button(root, text = 'Play', command = play)
while mixer.music.get_busy(): time.Clock().tick(10)
button1.grid(row=0, column=2)

pause = lambda: mixer.music.pause()
button2 = tk.Button(root, text="Pause", command = pause)
button2.grid(row=0, column=3)

resume = lambda: mixer.music.unpause()
button3 = tk.Button(root, text="Resume", command = resume)
button3.grid(row=0, column=4)

stop = lambda: mixer.music.stop()
button4 = tk.Button(root, text="Stop", command=stop)
button4.grid(row=0, column=5)

root.mainloop()

这是一种音乐播放器。 我运行来执行脚本的 pyinstaller 代码如下:

pyinstaller --add-data "Ignite.mp3;." --onefile -w playmusic.py

它执行没有问题,但是当我运行执行的脚本时,在弹出窗口中显示“无法执行脚本播放音乐”

【问题讨论】:

  • 也分享你的pyinstaller代码行
  • 我已经编辑了问题 - 添加了脚本文件和 pyinstaller 代码
  • 将函数传递给按钮的非常奇怪的方法,但尝试pyinstaller -F -c playmusic.py,然后将音乐文件复制到exe所在的位置,如果弹出的控制台上仍然弹出任何错误,用那个编辑Q
  • 究竟有什么作用?我可以将其添加为答案
  • 我用了命令'pyinstaller -F -c playmusic.py',把执行的脚本和歌曲放在同一个目录下。

标签: python-3.x tkinter pygame pyinstaller py2exe


【解决方案1】:

文件必须在同一目录中才能正确执行脚本。

如果你在脚本中说:

file = 'Ignite.mp3'
mixer.music.load(file)

如果该文件不是您的同一目录(与您的脚本)。它会在 python 脚本中引发错误,因此也会在您的 exe 中引发错误。

或者你可以说一个绝对路径之类的东西:

file = 'C:\\somefolder\\some_more_folder\\Ignite.mp3'
mixer.music.load()

但是建议第一种方法是在你制作exe的时候发布给一些人,所以你需要一个安装文件,所以绝对路径是行不通的,因为它只是你电脑上的路径。但是 ypu 可以指定安装到程序文件的位置,仍然可以找到方法,但建议使用第一种方法。

所以在确保你可以使用pyinstaller代码制作一个exe之后:

pyinstaller -F -w playmusic.py 

希望对您有所帮助,如果还有错误请告诉我。

干杯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-17
    • 2020-12-04
    • 2014-08-07
    • 1970-01-01
    • 2011-09-04
    • 2015-03-17
    • 2010-11-14
    • 1970-01-01
    相关资源
    最近更新 更多