gpt4 book ai didi

python - 我正在用 Python3(也使用 Tkinter)制作一个 mp3 付款器,但我正面临死胡同

转载 作者:行者123 更新时间:2023-12-04 01:08:41 25 4
gpt4 key购买 nike

我正在制作一个向播放器添加歌曲的菜单功能。

def add_song():
song = filedialog.askopenfilename(initialdir='C:\\Users\\Soham\\Music', title="Choose a
song!",filetypes=(("mp3 Files", "*.mp3"), ))
song_name = song.split("/")[-1].split(".")[0]
song_list.insert(END, song_name)

然后我有一个播放按钮,它被编码为播放添加的歌曲 -

play_button = Button(controls_frame, image=play_button_img,borderwidth=0, command = play)
play_button.grid(row=0,column=2,padx=5)

所以,func,play() 的代码是 -

def play():
song = song_list.get(ACTIVE)
pygame.mixer.music.load(song)
pygame.mixer.music.play(loops=0)

但是 play() 中的 dong 变量实际上只是歌曲的名称,因为它已经在 add_song() 中被拆分了。而 pygame 需要整个路径,因为歌曲与 python 文件不在同一个目录中。所以 pygame 无法打开和播放导致错误的歌曲 -

  Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Soham\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line
1885, in __call__
return self.func(*args)
File "c:\Users\Soham\Desktop\HM MP.py", line 26, in play
pygame.mixer.music.load(song)
pygame.error: Couldn't open 'Avicii - The Nights'

那么我能做些什么呢,有没有另一种方法可以分开显示歌曲名称的路径,而不会给 pygame 播放音乐带来任何问题??

此外,我正在使用 Windows 10 Pro 高端机器并使用 Python 3。

最佳答案

由于您将歌曲插入到列表框中并从那里播放,您可以做的是,制作一个索引字典,从 0 开始作为键,值作为歌曲名称和路径的列表,所以它类似于像 song_dict = {idx:[song_name,song_path]}。所以每个 idx 都是您从列表框中选择的。我已经用这个做了一个例子,看看:

from tkinter import *
from tkinter import filedialog
import pygame

root = Tk()
pygame.mixer.init()

song_dict = {} # Empty dict to assign values to it
count = 0 # An idx number to it increase later
def add_song():
global count

song_path = filedialog.askopenfilename(initialdir='C://Users//Soham//Music', title="Choose a song!",filetypes=(("mp3 Files", "*.mp3"), ))
song_name = song_path.split("/")[-1].split(".")[0]
song_dict[count] = [song_name,song_path] # Create the desired dictionary
song_list.insert(END, song_name) # Insert just the song_name to the Listbox

count += 1 # Increase the idx number

def play_song(*args):
idx = song_list.curselection()[0] # Get the index of the selected item
song = song_dict[idx][1] # Get the corresponding song from the dictionary
pygame.mixer.music.load(song) # Load the song
pygame.mixer.music.play(loops=0) # Play the song


song_list = Listbox(root,width=50)
song_list.pack(pady=10)

choose = Button(root,text='Choose song',command=add_song)
choose.pack(pady=10)

song_list.bind('<Double-Button-1>',play_song) # Just double click the desired song to play

root.mainloop()

只需双击您要播放的歌曲。您也可以像在代码中那样使用按钮而不是 bind()

song_dict 的结构示例如下:

{0: ['Shawn Mendes - Perfectly Wrong', 'C:/PyProjects/sONGS/Shawn Mendes - Perfectly Wrong.mp3'],
1: ['Lil Nas X - Old Town Road (feat', 'C:/PyProjects/sONGS/Lil Nas X - Old Town Road (feat. Billy Ray Cyrus) - Remix.mp3'],
2: ['NF - Time - Edit', 'C:/PyProjects/sONGS/NF - Time - Edit.mp3']}

虽然我也建议制作一个按钮来询问目录并获取该目录中的所有 mp3 文件并填充列表框。

如果你想使用filedialog.askopenfilenames那么你可以编辑函数如下:

import os

def add_song():
songs_path = filedialog.askopenfilenames(initialdir='C://Users//Soham//Music', title="Choose a song!")

for count,song in enumerate(songs_path):
song_name = os.path.basename(song)
song_dict[count] = [song_name,song] # Create the desired dictionary
song_list.insert(END, song_name) # Insert just the song_name to the Listbox

在这种情况下,您不需要预定义的 count 变量,因为我们从 for 循环中生成它们。

编辑:为什么不使用 split,而是使用 os.path,这样您就可以从路径中获取基本名称,例如:

import os

song_name = os.path.basename(song_path) # If its tuple then loop through and follow

关于python - 我正在用 Python3(也使用 Tkinter)制作一个 mp3 付款器,但我正面临死胡同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65516619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com