当我浏览文件以导入音频时,如何将音频保存到使用tkinter和pygame创建的特定按钮

我想创建一个音板,我的第一个问题是我不知道如何将音频保存到应用程序中的按钮。例如,当我按下1时,如果没有音频绑定到按钮,它将打开一个浏览文件以选择音频,一旦选择了音频,如何将其保存在代码中,以便下次运行该程序时我按1只会播放音频。

我没有做太多尝试,因为我迷失了在哪里寻找这种东西的机会,我还是对python和整个编码还比较陌生。

from tkinter import *
import tkinter.messagebox
from tkinter import filedialog
from pygame import mixer

# Window
root = Tk()

# Create the menubar
menubar = Menu(root)
root.config(menu=menubar)



# Dropdown Menu file
def browse_file():
    global filename
    filename = filedialog.askopenfilename()


subMenu = Menu(menubar,tearoff=0)
menubar.add_cascade(label='File',menu=subMenu)
subMenu.add_command(label="Open",command=browse_file)
subMenu.add_command(label="Exit",command=root.destroy)


# Dropdown Menu help
def about_us():
    tkinter.messagebox.showinfo('About',"If you read this you're gay")


subMenu = Menu(menubar,tearoff=0)
menubar.add_cascade(label='Help',menu=subMenu)
subMenu.add_command(label="About",command=about_us)

mixer.init()  # Initializing

# The Window
root.title("SoundBard")
root.iconbitmap(r'carlos.ico')

# Labels
text = Label(root,text='Audio Player')
text.pack()

def play_audio():

    # This logic first
    try:
        stopped # Checks if stopped variable is initialized
    except:
        try:
            paused # Checks if paused variable is initialized
        except NameError: # If neither above then executes code below
            try:
                mixer.music.load(filename)
                mixer.music.play()
                statusbar['text'] = "Playing Audio" + " -->  (" + os.path.basename(filename) + ')'
            except:
                tkinter.messagebox.showerror('Error',"Please add an audio file")
        else: # if paused is initialized then the code below is run
            mixer.music.unpause()
            statusbar['text'] = "Resumed"
    else: # if stopped is initialized then the code below is run
        mixer.music.play()
        statusbar['text'] = "Playing Audio" + " -->  (" + os.path.basename(filename) + ')'

def stop_audio():
    global stopped
    stopped = TRUE
    mixer.music.stop()
    statusbar['text'] = "Stopped."

def pause_audio():
    global paused
    paused = TRUE
    mixer.music.pause()
    statusbar['text'] = "Paused"

def rewind_audio():
    play_audio()
    statusbar['text'] = "Restarted"

def one_aud():
    browse_file()

bottomframe = Frame(root)
bottomframe.pack(pady=20,padx=20)



# Images for audio bound keys
one_pic = PhotoImage(file='one.png')
one_btn = Button(bottomframe,image=one_pic,command=one_aud)
one_btn.grid(row=1,column=0,)





我正在谈论one_aud函数。

wodeqiji110 回答:当我浏览文件以导入音频时,如何将音频保存到使用tkinter和pygame创建的特定按钮

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3121855.html

大家都在问