简体   繁体   中英

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.

Now I know Tkinter doesn't support sound but I don't care how the program runs. 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). Thanks!

You could use playsound module and use a thread to prevent block:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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