Не дается сделать нормальный таймлайн ткинтер питон
я 2 недели пытаюсь сделать плеер в ткинтере(python) но у меня 1)таймлайн не двигаеться 2)после завершение песни не включается следующая
import os
import pygame
import pickle
from tkinter import Tk, Button, Scale, filedialog, HORIZONTAL, Label, PhotoImage, Listbox
from time import gmtime, strftime
from mutagen.mp3 import MP3
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.mixer.init()
class Player(Tk):
def __init__(self):
super().__init__()
self.title('Python Music Player')
self.geometry("500x400")
self.resizable(0, 0)
# self.image = PhotoImage(file='bg.png')
# self.label = Label(self, image=self.image)
# self.label.place(x=0, y=0, relwidth=1, relheight=1)
self.button1 = Button(self, text="Выберите песню", command=self.load_song)
self.button1.pack()
self.button2 = Button(self, text="Воспроизвести/Продолжить", command=self.play_song)
self.button2.pack()
self.button3 = Button(self, text="Пауза", command=self.pause_song)
self.button3.pack()
self.button4 = Button(self, text="Воспроизвести случайную песню", command=self.play_random_song)
self.button4.pack()
self.button5 = Button(self, text="Удалить песню", command=self.delete_song)
self.button5.pack()
self.song_label = Label(self, text="")
self.song_label.pack()
self.time_label = Label(self, text="Время: 00:00 | 00:00")
self.time_label.pack()
self.volume = Scale(self, from_=0, to_=100, resolution=1, orient=HORIZONTAL,
length=300, sliderlength=10, command=self.set_volume)
self.volume.pack()
self.volume.set(50)
self.timeline = Scale(self, from_=0, to_=1000, resolution=1, orient=HORIZONTAL,
length=300, sliderlength=10, command=self.update_song_position, showvalue=0)
self.timeline.pack()
self.song_listbox = Listbox(self)
self.song_listbox.pack()
self.song_listbox.bind('<<ListboxSelect>>', self.select_song_from_list)
self.song_loaded = False
self.is_paused = False
self.song_length_ms = 0
self.song_start_time = pygame.time.get_ticks()
self.is_user_scrubbing = False
self.timeline.bind("<B1-Motion>", self.start_scrub)
self.timeline.bind("<ButtonRelease-1>", self.end_scrub)
self.update_timeline_music()
self.song_history_file = "song_history.pkl"
if os.path.isfile(self.song_history_file):
with open(self.song_history_file, 'rb') as file:
self.song_history = pickle.load(file)
for song in self.song_history.values():
self.song_listbox.insert("end", song)
else:
self.song_history = {}
self.bind(pygame.USEREVENT, self.handle_song_end)
def select_song_from_list(self, event):
selection = event.widget.curselection()
if selection:
index = selection[0]
song_name = event.widget.get(index)
for filename, name in self.song_history.items():
if name == song_name:
self.song_file = filename
pygame.mixer.music.load(self.song_file)
audio = MP3(self.song_file)
self.song_length_ms = audio.info.length * 1000
self.timeline.config(to_=int(self.song_length_ms / 1000))
self.song_label.config(text=name)
self.play_song()
def update_timeline_music(self):
if not self.is_user_scrubbing:
if pygame.mixer.music.get_busy() or self.is_paused:
current_pos = pygame.time.get_ticks() - self.song_start_time
pos_str = strftime("%M:%S", gmtime(int(current_pos / 1000)))
length_str = strftime("%M:%S", gmtime(int(self.song_length_ms / 1000)))
self.time_label.config(text="Время: {} | {}".format(pos_str, length_str))
self.timeline.set(int(current_pos / 1000))
self.timeline.update()
self.after(1000, self.update_timeline_music)
def update_song_position(self, _=None):
if 'song_file' in dir(self) and pygame.mixer.music.get_busy():
pygame.mixer.music.play(0, self.timeline.get())
def set_volume(self, volume):
pygame.mixer.music.set_volume(int(volume) / 100)
def load_song(self):
self.song_file = filedialog.askopenfilename(initialdir=os.getcwd(),
title="Выберите музыкальный файл",
filetypes=[("Audio files", "*.mp3 *.midi")])
if self.song_file:
pygame.mixer.music.load(self.song_file)
self.song_loaded = True
if self.song_file.endswith('.mp3'):
audio = MP3(self.song_file)
self.song_length_ms = audio.info.length * 1000
self.timeline.config(to_=int(self.song_length_ms / 1000))
song_file_name = os.path.basename(self.song_file)
self.song_label.config(text=song_file_name)
self.song_history[self.song_file] = song_file_name
self.song_listbox.insert("end", song_file_name)
with open(self.song_history_file, 'wb') as file:
pickle.dump(self.song_history, file)
self.play_song()
def handle_song_end(self, event):
if event.type == pygame.USEREVENT:
self.play_next_song()
def play_next_song(self):
if len(self.song_listbox.curselection()) == 0:
return
current_song_index = self.song_listbox.curselection()[0]
next_song_index = (current_song_index + 1) % self.song_listbox.size()
self.song_listbox.selection_clear(0, self.song_listbox.size() - 1)
self.song_listbox.selection_set(next_song_index)
self.song_listbox.activate(next_song_index)
self.select_song_from_list(None)
def pause_song(self):
if pygame.mixer.music.get_busy():
if self.is_paused:
pygame.mixer.music.unpause()
self.is_paused = False
else:
pygame.mixer.music.pause()
self.is_paused = True
def play_random_song(self):
random_song = random.choice(list(self.song_history.keys()))
pygame.mixer.music.load(random_song)
pygame.mixer.music.play()
def delete_song(self):
if hasattr(self, 'song_file'):
if self.song_file in self.song_history.keys():
del self.song_history[self.song_file]
self.song_listbox.delete(0, "end")
for song in self.song_history.values():
self.song_listbox.insert("end", song)
os.remove(self.song_file)
self.song_file = None
self.song_label.config(text="")
self.timeline.config(to_=1000)
self.time_label.config(text="Время: 00:00 | 00:00")
pygame.mixer.music.stop()
def start_scrub(self, event):
self.is_user_scrubbing = True
def end_scrub(self, event):
self.is_user_scrubbing = False
pygame.mixer.music.play(0, self.timeline.get())
def record_music(self):
with open("music_records.txt", "a") as file:
record_str = "Название музыки: {}\\nВремя музыки: {}\\nКогда играла: {}\\n".format(
self.song_label["text"], strftime("%M:%S", gmtime(int(self.song_length_ms / 1000))),
strftime("%Y-%m-%d %H:%M:%S")
)
file.write(record_str)
def play_song(self):
if self.song_loaded:
pygame.mixer.music.play()
self.song_start_time = pygame.time.get_ticks()
self.record_music()
self.update_timeline_music()
player = Player()
player.mainloop()```