简体   繁体   English

在python中将pygame与tkinter一起使用时,如何使用同一按钮暂停和取消暂停?

[英]Using pygame with tkinter in Python, how do I pause and unpause with the same button?

Pygame is an extension you download that offers built in functions. Pygame是您下载的扩展,具有内置功能。 I'm currently working on a song player, and have a button that pauses the song and unpauses the song. 我目前正在研究歌曲播放器,并且有一个按钮可以暂停歌曲和取消暂停歌曲。 How can I combine these two buttons so that I click once and it pauses and then click again and it unpauses etc? 如何合并这两个按钮,以便单击一次并暂停,然后再次单击并取消暂停等? I have two functions right now that needs to be made into one, as they will be assigned to one button.. 我现在需要将两个功能合而为一,因为它们将分配给一个按钮。

This is the code I have that needs to be changed: 这是我需要更改的代码:

def pausesong():
    pygame.mixer.music.pause()

def unpausesong():
    pygame.mixer.music.unpause()

I'm not familiar with pygame, but a simple approach would be to add a flag variable that you can check. 我对pygame不熟悉,但是一种简单的方法是添加一个可以检查的标志变量。

def pausesong():
    pygame.mixer.music.pause()
    paused = True

def unpausesong():
    pygame.mixer.music.unpause()
    paused = False

And then in your button event... 然后在您的按钮事件中...

if paused:
    unpausesong()
else:
    pausesong()
def play_pause():
    paused = not paused
    if paused: pygame.mixer.music.unpause()
    else: pygame.mixer.music.pause()
#resume song #pause the song
pause=False
a=0 #for testing
def pausesong():
    global index,a
    global pause
    a=a+1
    #print(pause,a)
    if pause == False:
        pygame.mixer.music.pause()
        pause = True
        print(pause,a)

    else:
        pygame.mixer.music.unpause()
        pause = False
        print(pause,a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM