How do I pause a song in python?

1

I can make the program play the music, but the whole window is not functional and only works again when the music stops, ie creating a button with def to pause the button only becomes "clickable" when the whole song stop.

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()
    
asked by anonymous 17.05.2018 / 11:43

1 answer

1

I believe that event checking is missing, since event.get() without being called the screen will be 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
    
17.05.2018 / 14:26