英文:
How can I update a tkinter progress bar to show the progress of a song in a playlist using pygame and mutagen?
问题
# 我需要根据歌曲更改进度条。
class AudioPlayer:
# ...(代码部分不翻译)
def update_progress_bar(self):
if pygame.mixer.music.get_busy():
current_time = pygame.mixer.music.get_pos() / 1000.0
song_length = MP3(self.playlist[self.current_track_index]).info.length
percent_elapsed = (current_time / song_length) * 100
self.progress_bar['value'] = percent_elapsed
self.master.after(1000, self.update_progress_bar)
root = ctk.CTk()
# ...(代码部分不翻译)
英文:
I need the progress bar to change depending on the song.
import os
from tkinter import filedialog
import customtkinter as ctk
import pygame
from mutagen.mp3 import MP3
import pygame.event
class AudioPlayer:
def __init__(self, master):
self.master = master
master.title("Audio Player")
master.geometry("400x250")
self.play_button = ctk.CTkButton(master, text=' ▶️', command=self.play, width=60, height=50)
self.pause_button = ctk.CTkButton(master, text='| |', command=self.pause, width=60, height=50)
self.next_button = ctk.CTkButton(master, text='⏩', command=self.next, width=60, height=50)
self.prev_button = ctk.CTkButton(master, text='⏪', command=self.prev, width=60, height=50)
self.track_info_label = ctk.CTkLabel(master, text='')
self.pause_button.place(x=190, y=50)
self.play_button.place(x=120, y=50)
self.next_button.place(x=260, y=50)
self.prev_button.place(x=50, y=50)
self.track_info_label.pack(pady=10, padx=20)
self.volume_scale = ctk.CTkSlider(master, from_=0, to=100, command=self.set_volume)
self.volume_scale.set(50.0)
self.volume_scale.place(x=170, y=110)
self.load_button = ctk.CTkButton(master, text='Load', command=self.load_file, width=100, height=25)
self.load_button.place(x=50, y=110)
self.progress_bar = ctk.CTkProgressBar(master, orientation=ctk.HORIZONTAL, corner_radius=200, mode='determinate', width=270, height=25)
self.progress_bar.place(x=50, y=145)
self.playlist = []
self.current_track_index = -1
pygame.mixer.init()
self.update_progress_bar()
def load_file(self):
directory_path = filedialog.askdirectory()
self.playlist = []
if directory_path:
for dirpath, _, filenames in os.walk(directory_path):
for filename in filenames:
if filename.endswith('.mp3'):
self.playlist.append(os.path.join(dirpath, filename))
def play(self):
if len(self.playlist) > 0:
if self.current_track_index == -1:
self.current_track_index = 0
pygame.mixer.music.load(self.playlist[self.current_track_index])
self.update_track_info()
else:
pygame.mixer.music.unpause()
self.update_progress_bar()
def next(self):
if len(self.playlist) > 0:
self.current_track_index = (self.current_track_index + 1) % len(self.playlist)
self.update_track_info()
pygame.mixer.music.load(self.playlist[self.current_track_index])
pygame.mixer.music.play()
self.progress_bar['value'] = 0
def prev(self):
if len(self.playlist) > 0:
self.current_track_index = (self.current_track_index - 1) % len(self.playlist)
self.update_track_info()
pygame.mixer.music.load(self.playlist[self.current_track_index])
pygame.mixer.music.play()
self.progress_bar['value'] = 0
def update_track_info(self):
if len(self.playlist) > 0:
track_path = self.playlist[self.current_track_index]
track_name = os.path.basename(track_path)
self.track_info_label.configure(text=track_name[:-4])
def pause(self):
pygame.mixer.music.pause()
def set_volume(self, value):
pygame.mixer.init()
pygame.mixer.music.set_volume(float(value) / 100)
def update_progress_bar(self):
if pygame.mixer.music.get_busy():
current_time = pygame.mixer.music.get_pos() / 1000.0
song_length = MP3(self.playlist[self.current_track_index]).info.length
percent_elapsed = (current_time / song_length) * 100
self.progress_bar['value'] = percent_elapsed
self.master.after(1000, self.update_progress_bar)
root = ctk.CTk()
root.resizable(width=False, height=False)
ctk.set_default_color_theme("dark-blue") # Themes: "blue" (standard), "green", "dark-blue"
player = AudioPlayer(root)
root.mainloop()
I don't understand how to do this, because I recently got acquainted with the library and never found an answer to the question.
答案1
得分: 1
你需要创建一个每1秒调用自身的函数,并使用来自自定义tkinter的intvariable更新进度条。
```python
import customtkinter as ctk
from mutagen.mp3 import MP3
root = ctk.CTk()
def update_slider():
current_time = pygame.mixer.music.get_pos() / 1000
song_length = MP3(song_address).info.length
slider_var.set(current_time)
slider_var.configure(to=int(song_length))
root.after(1000, update_slider)
slider_var = ctk.IntVar(value=0)
slider = ctk.ctkslider(root, from_=0, to=1)
root.mainloop()
<details>
<summary>英文:</summary>
you need to make a function that calls itself every 1 second and update the progress bar using an intvariable from custom tkinter
import customtkinter as ctk
from mutagen.mp3 import MP3
root = ctk.CTk()
def update_slider():
current_time = pygame.mixer.music.get_pos() / 1000
song_length = MP3(song_address).info.length
slider_var.set(current_time)
slider_var.configure(to = int(song_length))
root.after(1000, update_slider)
slider_var = ctk.IntVar(value=0)
slider = ctk.ctkslider(root, from_ = 0, to = 1)
root.mainloop()
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论