当声音效果结束时,我想要一个tkinter标签什么也不显示。
我一直在研究如何在没有运气的情况下创建/初始化/捕捉音乐活动的结尾。
def play_btn():
if mixer.music.get_busy():
mixer.music.fadeout(1000)
snd_fyl.set(snd_list.get(ACTIVE))
mixer.music.load(snd_dir+"/"+snd_list.get(ACTIVE)+"mp3")
mixer.music.play()
def stop_btn():
mixer.music.stop()
clear_label()
def clear_label():
snd_fyl.set("")
snd_lbl1 = LabelFrame(MainWindow, text="Sound effect playing", labelanchor=N)
snd_playing_lbl = Label(snd_lbl1, width=40, textvariable=snd_fyl)显然,play_btn函数从列表中发挥了声音效果。
stop_btn函数过早地停止声音效果并清除标签。
clear_label函数是为end_of_song事件做好准备而创建的。
发布于 2019-11-06 04:50:37
您必须使用set_endevent()来设置值,当音乐完成后,它将发送到事件队列。
MUSIC_END = pygame.USEREVENT+1
pygame.mixer.music.set_endevent(MUSIC_END)然后您可以在事件循环中测试它。
if event.type == MUSIC_END:
print('music end event')当音乐结束时,它会打印文本--而不是当你停止或暂停它的时候。
顺便说一句:在Linux上,我在播放音乐之前几毫秒就看到了这篇文章。
完整的示例-但是没有tkinter
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
MUSIC_END = pygame.USEREVENT+1
pygame.mixer.music.set_endevent(MUSIC_END)
pygame.mixer.music.load('sound.wav')
pygame.mixer.music.play()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == MUSIC_END:
print('music end event')
if event.type == pygame.MOUSEBUTTONDOWN:
# play again
pygame.mixer.music.play()
pygame.quit()编辑:使用tkinter的示例
import pygame
import tkinter as tk
def check_event():
for event in pygame.event.get():
if event.type == MUSIC_END:
print('music end event')
label['text'] = ''
root.after(100, check_event)
def play():
label['text'] = 'playing'
pygame.mixer.music.play()
# --- main ---
pygame.init()
MUSIC_END = pygame.USEREVENT+1
pygame.mixer.music.set_endevent(MUSIC_END)
pygame.mixer.music.load('sound.wav')
root = tk.Tk()
label = tk.Label(root)
label.pack()
button = tk.Button(root, text='Play', command=play)
button.pack()
check_event()
root.mainloop()
pygame.quit()https://stackoverflow.com/questions/58630700
复制相似问题