Я пытаюсь собрать музыканта c. Пока это действительно просто, и я застрял, пытаясь создать какую-то очередь.
Я решил, что хочу сделать каждую песню в списке «кликабельной», которая будет вызывать другую функцию с путем в качестве аргумента. Я пытался создать динамические c кнопки, которые будут создавать новую кнопку каждый раз, когда я нажимаю Поиск , и много раз терпел неудачу, пытаясь разными способами. На данный момент я застрял на:
def searchmusic(self):
self.newmusicpath = filedialog.askopenfilename(initialdir="/", title = "Szukajka", filetypes = (("mp3","*.mp3"),("all","*.*")))
#self.playmusic(self.newmusicpath)
self.music_dict["song" + str(self.music_on_list_number)] = self.newmusicpath
print(self.music_dict)
locals()['song{}partial'.format(self.music_on_list_number)] = print("TEST DONE")
locals()['song{}'.format(self.music_on_list_number)] = tk.Button(canvas, text = self.newmusicpath, command = exec('song{}partial'.format(self.music_on_list_number)))
exec('song{}.pack()'.format(self.music_on_list_number))
self.music_on_list_number += 1
К сожалению, строка locals()['song{}partial'.format(self.music_on_list_number)] = print("TEST DONE")
выполняется сразу после того, как я ищу новую песню, вместо того, чтобы нажимать кнопку новой песни.
Остальная часть кода:
import pygame
import tkinter as tk
import os
from tkinter import filedialog, Text
import functools
pygame.mixer.init()
class music:
def __init__(self):
self.music_playing = False
self.music_pause = False
self.currently_playing_label = "Nyan Cat"
self.currently_playing = r"C:/Users/Happy\Desktop/Raspbian python/nyan.mp3"
self.music_dict = {}
self.CurrentSongLabel = tk.Label(frame, text = "None", bg = 'green')
self.CurrentSongLabel.pack()
self.music_on_list_number = 0
def volumecontrol(self, volumeslider):
pygame.mixer.music.set_volume(int(volumeslider)/100)
#Start/Pause button
def playbutton(self):
if self.music_playing == False:
self.music_playing = True
self.playmusic(self.currently_playing)
elif self.music_pause == False:
pygame.mixer.music.pause()
self.music_pause = True
elif self.music_pause == True:
pygame.mixer.music.unpause()
self.music_pause = False
#Music play
def playmusic(self, pathtonewmusic):
if pygame.mixer.music.get_busy() != -1:
pygame.mixer.music.stop()
self.currently_playing = pathtonewmusic
pygame.mixer.music.load(self.currently_playing)
pygame.mixer.music.play()
self.currentlyplayinglabel()
#Label change
def currentlyplayinglabel(self):
if self.CurrentSongLabel:
self.CurrentSongLabel.destroy()
self.currently_playing_as_list = self.currently_playing.split("/")
self.currently_playing_label = self.currently_playing_as_list[-1]
self.CurrentSongLabel = tk.Label(frame, text = self.currently_playing_label, bg = 'green', fg = 'lightblue')
self.CurrentSongLabel.pack()
#Music search
def searchmusic(self):
self.newmusicpath = filedialog.askopenfilename(initialdir="/", title = "Szukajka", filetypes = (("mp3","*.mp3"),("all","*.*")))
#self.playmusic(self.newmusicpath)
self.music_dict["song" + str(self.music_on_list_number)] = self.newmusicpath
print(self.music_dict)
locals()['song{}partial'.format(self.music_on_list_number)] = print("TEST DONE")
locals()['song{}'.format(self.music_on_list_number)] = tk.Button(canvas, text = self.newmusicpath, command = exec('song{}partial'.format(self.music_on_list_number)))
exec('song{}.pack()'.format(self.music_on_list_number))
self.music_on_list_number += 1
#Creating windows
root = tk.Tk()
canvas = tk.Canvas(root, width = 300, height = 100, bg = 'red')
canvas.grid(row = 5, columnspan = 3)
frame = tk.Frame(root, bg = 'green')
frame.grid(row = 0, column = 0, columnspan = 4)
LabelPlayingCurrently = tk.Label(frame, text = "CURRENTLY PLAYING:", bg = 'green', fg = 'lightblue')
LabelPlayingCurrently.pack()
#Init music function
music_inited = music()
#Music control buttons
play = tk.Button(root, text = 'Play/Pause', fg='white', padx = 15, pady = 30, bg = "red", command = music_inited.playbutton)
play.grid(row = 3, column = 0, rowspan = 2)
nextsong = tk.Button(root, text = 'Nastepna', fg='white', bg = "red", pady = 10, padx = 6)
nextsong.grid(row = 3, column = 1)
previoussong = tk.Button(root, text = 'Poprzednia', fg='white', bg = "red", pady = 10)
previoussong.grid(row = 4, column = 1)
volume = tk.Scale(root, label = "VOLUME", from_ = 100, to = 0, command=music_inited.volumecontrol)
volume.set(5)
volume.grid(row = 3, column = 2, rowspan = 2)
#Music search button
searchbutton = tk.Button(root, text = 'Search', fg='white', bg = "red", command = music_inited.searchmusic)
searchbutton.grid(row = 8, column = 0, columnspan = 3)
root.mainloop()