How do I pause a song in python?

Asked

Viewed 582 times

1

I can make the program play the music, but the whole window is without function and only comes back to work when the music stops, that is to say creating a def button to pause the button only becomes "clickable" when all the music stops.

from tkinter import *
from pygame import *
from pygame import mixer
jan = Tk()
jan.title("JANELAA")
jan.geometry('400x400')

class Sistema:
   def __init__(self, box):
    self.frame = Frame(box)
    self.frame.pack()
    self.menu = Menu(box)
    self.menuFile = Menu(self.menu)
    self.menuFile.add_command(label="Musicas" , command=self.msc)
    self.menuFile.add_command(label="Notepad")
    self.menu.add_cascade(label="File", menu=self.menuFile)
    self.menuOption = Menu(self.menu)
    self.menuOption.add_command(label="Calculadora")
    self.menu.add_cascade(label="Options" , menu=self.menuOption)
    box.config(menu=self.menu)

def msc(self):
    lbl = Label(jan, text="MUSICA COLD BLOODED")
    lbl.place(x=50, y=50)

    def ok():
        mixer.init()
        mixer.music.load('coldb.mp3')
        mixer.music.play(1 , 1)
        while mixer.music.get_busy():
            time.Clock().tick(10)

    bt = Button(jan, text="TOCAR", command=ok)
    bt.place(x=50, y=70)
Sistema(jan)
jan.mainloop()

1 answer

1


I believe that the verification of events is missing, since the event.get() without being called the screen will get frozen.

def ok():
    mixer.init()
    mixer.music.load('coldb.mp3')
    mixer.music.play(1 , 1)
    musica_tocando = True
    while mixer.music.get_busy():
        time.Clock().tick(10)
        for event in event.get()
            if event.type == MOUSEBUTTONDOWN:
                if musica_tocando: 
                    mixer.music.pause()
                    musica_tocando = False
                else:
                    mixer.music.unpause() 
                    musica_tocando = True
  • Face vlw the program agr no longer need to wait for the music to end, however I’m still struggling to create the pause button, I do not understand how the event MOUSEBUTTONDOWN: says it is to pause but there is no pause button, and I had to add a -> : <- after envent.get() but it gives an error in the interpreter but I thank you since the problem I was giving was solved

  • Like the pause button I created it does not just pause it for all the music and does not come back anymore when I click on the start button msm, I must create a button and apply functions to go back to where you left off?

  • Dude, I had to create 3 buttons & #Xa

  • the error was as follows: for Event in Event.get() : Unboundlocalerror: local variable 'Event' referenced before assignment

  • Try to see if the error is not indentation. The reference to Event has to be inside the "for" loop, otherwise the interpreter does not "think" it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.