我正在编写一个简单的音乐播放器。
我已经在Stackoverflow中搜索了其他问题,但是,这些解决方案不适用于我的pygame构建。
我的代码如下。我正在使用Tkinter进行gui构建。
import sys
from Tkinter import *
import tkMessageBox
import pygame
myGui = Tk()
def mClose():
mExit = tkMessageBox.askokcancel(title="Quit", message="are you sure?")
if mExit ==True:
myGui.destroy()
return
def mPlay():
pygame.mixer.init()
pygame.mixer.music.load("/home/david/Downloads/test.mp3")
pygame.mixer.music.play()
def unPause():
pygame.mixer.music.unpause()
def mPause():
pygame.mixer.music.pause()
myGui.title("My Audio")
myGui.geometry("200x200+600+300")
mLabel = Label(myGui, text="My Audio").pack()
''' Button for Closing App'''
mButton = Button(myGui, text="Close", command = mClose).pack()
'''Play Button'''
mButton = Button(myGui, text="Play", command = mPlay).pack()
'''Pause Button'''
mButton = Button(myGui, text="Pause", command = mPause).pack()
'''UnPause Button'''
mButton = Button(myGui, text="UnPause", command = unPause).pack()
我已经厌倦了使用pygame.mixer.music.get_busy()来组合暂停和取消暂停。但是,如果它被暂停,则布尔值仍然返回true以进行激活。
我使用以下内容无济于事:
def play_pause():
paused = not paused
if paused: pygame.mixer.music.unpause()
else: pygame.mixer.music.pause()
我得到以下内容:
File "/home/david/Documents/tkinter_testing.py", line 29, in play_pause
paused = not paused
UnboundLocalError: local variable 'paused' referenced before assignment.
任何想法或帮助。在此先感谢您的帮助。
答案 0 :(得分:1)
您正在为paused
分配一个值,但为该值调用自己。我相信你要找的是
paused = False
或
paused = not True
答案 1 :(得分:1)
你的逻辑不正确。
假设我们从混音器暂停开始,所以:
paused is True
我们调用play_pause()来切换它,暂停设置为不暂停,所以现在:
paused is False
所以我们执行else语句,并暂停()调音台,但它已经暂停了。解决方案是将切换移动到设置之后(这可能是最清楚的)或者反转从if-else块调用的逻辑。