Slacker
Slacker

Reputation: 21

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. 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. 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)

Upvotes: 0

Views: 4370

Answers (3)

omar ali
omar ali

Reputation: 21

Use Check Button Text with an if condition

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()

Upvotes: 1

Mike67
Mike67

Reputation: 11342

I used your code to create this example. It changes the button text based on the @AST answer:

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()

Upvotes: 0

astqx
astqx

Reputation: 2096

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. The task value changes every time the button is clicked.

Upvotes: 0

Related Questions