簡體   English   中英

tkinter 按下按鈕 state

[英]tkinter button struck at pressed state

這是我一個接一個播放 mp3 文件的代碼。 但問題是,盡管歌曲正在一首接一首地播放,但 tkinter window 被敲擊並且沒有響應。 那么,有沒有辦法解決這個問題,或者請建議一種正確的循環播放 mp3 文件的方法。 謝謝你!!

import pygame
import time
from tkinter import *
from mutagen.mp3 import MP3
screen = Tk()


def play():
    my_music = [] # list cotaining name of songs.mp3
    i = len(my_music)
    while i != 0:
        pygame.mixer.music.load(f'D://sounds//{my_music[len(my_music) - i]}')
        pygame.mixer.music.play()
        audio = MP3(f'D://sounds//{my_music[len(my_music) - i]}')
        length = audio.info.length
        time.sleep(length)  # sleep untill the song is played totally and then continue 
        i -= 1
        
        
play_button = Button(screen, text='play', command=play)
play_button.pack()
screen.mainloop()

time.sleep time.sleep(...)將阻止 tkinter 主循環更新 GUI。 您可以使用after()替換 while 循環和time.sleep(...)

def play(i=0):
    music_dir = "D:/sounds"
    my_music = [...] # list containing name of songs.mp3
    n = len(my_music)
    song = f'{music_dir}/{my_music[i]}'
    pygame.mixer.music.load(song)
    pygame.mixer.music.play()
    if i < n-1:
        audio = MP3(song)
        length = audio.info.length
        # schedule the next song
        screen.after(int(length*1000), play, i+1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM