简体   繁体   English

如何添加Tkinter按钮让用户播放自己的音乐?

[英]How to add Tkinter button that allows the user to play their own music?

Just a quick question: In a game, I'm making, I want the player to be able to pick an audio file from his/her computer and play it in the game and I'm not all that sure how to do it.只是一个简单的问题:在我正在制作的游戏中,我希望玩家能够从他/她的计算机中选择一个音频文件并在游戏中播放它,但我不太确定该怎么做。 I want them to be able to open a browse files screen (default file explorer) and then select a music file and play it as bgm, all by the click of a button.我希望他们能够打开浏览文件屏幕(默认文件资源管理器),然后打开 select 音乐文件并将其作为 bgm 播放,只需单击一个按钮。

Now I know Tkinter doesn't support sound but I don't care how the program runs.现在我知道 Tkinter 不支持声音,但我不在乎程序如何运行。 As long as I can fit it into my code.只要我能把它融入我的代码。 If you need my code, it's here: https://github.com/SeaPuppy2006/FruitClicker (I'm using my windows build at the moment).如果你需要我的代码,它在这里: https://github.com/SeaPuppy2006/FruitClicker (我正在使用我的 windows 版本)。 Thanks!谢谢!

You could use playsound module and use a thread to prevent block:您可以使用playsound模块并使用线程来防止阻塞:

from playsound import playsound
import tkinter
from tkinter import filedialog
import threading
def f():
    def play():
        pathname = filedialog.askopenfilename()
        playsound(pathname)
    threading.Thread(target=play).start()

root = tkinter.Tk()
tkinter.Button(root,text="playsound",command=f).grid()

root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM