使用 pygame.mixer.music.get_endevent() [英] Utilising the pygame.mixer.music.get_endevent()

查看:65
本文介绍了使用 pygame.mixer.music.get_endevent()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望 tkinter 标签在音效完成后不显示任何内容.

I want a tkinter label to show nothing when a sound effect has finished.

我一直在研究有关如何创建/初始化/赶上音乐活动结束但没有运气的 www.

I've been researching the www on how to create/initialise/catch the end of music event with no luck.

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 函数从列表中播放音效.


Obviously play_btn function plays a sound effect from a list.

stop_btn 函数提前停止音效并清除标签.

The stop_btn function prematurely halts the sound effect and clears the label.

clear_label 函数已经为 end_of_song 事件做好准备

The clear_label function has been created in readiness for the end_of_song event

推荐答案

您必须使用 set_endevent() 来设置值,当音乐结束时它将发送到事件队列.

You have to use set_endevent() to set value which it will send to event queue when music has finished.

MUSIC_END = pygame.USEREVENT+1
pygame.mixer.music.set_endevent(MUSIC_END)

然后你可以在事件循环中测试它

And then you can test it in event loop

if event.type == MUSIC_END:
    print('music end event')

它会在音乐结束时打印文本 - 但在您停止或暂停时不会打印.

It will print text when music has finished - but not when you stop it or pause it.

顺便说一句:在 Linux 上,我在播放音乐结束前几毫秒看到此文本.

BTW: on Linux I see this text few milliseconds before it ends playing music.

完整的工作示例 - 但没有 tkinter

Full working example - but without 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()

这篇关于使用 pygame.mixer.music.get_endevent()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆