[英]How can I make a single Button play music, pause music and unpause music in Python using tkinter and pygame?
I've only been able to get the program to play music when the button is pushed.我只能在按下按钮时让程序播放音乐。 Everything I've tried to pause or stop the music has ended in failure.
我试图暂停或停止音乐的一切都以失败告终。 I feel like I should be using an if statement to pause the music if it's currently playing or to unpause it if it's not but I need to understand the proper way to write that.
我觉得我应该使用 if 语句来暂停音乐,如果它当前正在播放,或者如果它不是,则取消暂停,但我需要了解编写它的正确方法。 Would this be the correct approach?
这会是正确的方法吗? I really want to be able to start, pause and unpause the music from a single button but I'm struggling to figure out how to go about coding that.
我真的希望能够从一个按钮开始、暂停和取消暂停音乐,但我正在努力弄清楚如何 go 进行编码。 Here is my code to play the music.
这是我播放音乐的代码。
def Play_music():
pygame.mixer.music.load('tavernsound.mp3')
pygame.mixer.music.play()
and here is the code for the button:这是按钮的代码:
btn11=Button(labelframe, text='Ambiance', width=14, bg='red', fg='black', command=Play_music)
btn11.pack(side=LEFT)
Use Check Button Text with an if condition使用带有 if 条件的检查按钮文本
Example:例子:
from tkinter import Tk, Button
from pygame import mixer
root = Tk()
root.title("Play Music")
root.geometry('350x200')
mixer.init()
mixer.music.load("sound.mp3")
def play_music():
if button["text"] == "Play":
button["text"] = "Pause"
button["bg"] = "red"
mixer.music.play()
else:
button["text"] = "Play"
button["bg"] = "green"
mixer.music.pause()
button = Button(root, text='Play', width=14, bg='green', fg='black', command=play_music)
button.pack()
root.mainloop()
Here is an example.这是一个例子。
from tkinter import *
root = Tk()
def doSomething(task):
global var, button
if task == 0:
var.set("Started Playing")
button.configure(text = "Pause")
button.configure(command = lambda task = 1: doSomething(task))
#play
elif task == 1:
var.set("Paused")
button.configure(text = "Resume")
button.configure(command = lambda task = 0: doSomething(task))
#pause
button = Button(root, text = "Play", command = lambda task = 0: doSomething(task))
button.pack()
var = StringVar()
label = Label(root, textvariable = var)
label.pack()
root.mainloop()
Here the same button is used to play, pause and resume.在这里,相同的按钮用于播放、暂停和恢复。 I have used the
lambda
function which has the syntax lambda arguments: expression
.我使用了
lambda
function 语法lambda arguments: expression
The task
value changes every time the button is clicked.每次单击按钮时,
task
值都会更改。
I used your code to create this example.我用你的代码来创建这个例子。 It changes the button text based on the @AST answer:
它根据@AST 答案更改按钮文本:
from tkinter import *
import pygame
root = Tk()
root.title("MSC")
pygame.init()
mxstate = 0 # music play state
pygame.mixer.music.load('tavernsound.mp3')
def Play_music():
global mxstate
if mxstate == 0: # music not started
pygame.mixer.music.play()
btn11.configure(text = "Pause")
mxstate = 1
return
if mxstate == 1: # music playing
pygame.mixer.music.pause()
btn11.configure(text = "Resume")
else: # music paused
pygame.mixer.music.unpause()
btn11.configure(text = "Pause")
mxstate = 3-mxstate # swap pause state
btn11=Button(root, text='Ambiance', width=14, bg='red', fg='black', command=Play_music)
btn11.pack(side=LEFT)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.