Susie Xue
Susie Xue

Reputation: 29

The tkinter windows freezes after playing a sound effect when user clicks on "continue"

When I was trying to play a "yay" sound effect after the user has passed the test, I will show the credits. However, considering that some users are impatient and they don't want to see it, I made a tkinter window to show the option of whether to see or not. But when the user clicks on "continue", the tk windows freeze (Freezed window) and after a while, it is normal again.
This may not be a very big problem. However, if it is compiled to exe file, it may cause sudden exit without warning and this is not a good user experience. So is there any way to stop the freeze?
This is part of the codes in which the window freezes.

class Success(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Yay! You have passed my chemistry challenge! Would you like to continue?",
                         font=LARGE_FONT)
        label.pack(pady=10, padx=10)
        button1 = tk.Button(self, text="Continue", command=lambda: [controller.show_frame(Credits), success1()])
        button1.pack()
        button2 = tk.Button(self, text="Quit", command=lambda: controller.destroy())
        button2.pack()

    def correspondingBehavior(self, choice):
        print(choice)


class Credits(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self,
                         text="Credits: bababa..." )
        label.pack(pady=10, padx=10)
        button1 = tk.Button(self, text="Quit", command=lambda: controller.destroy())
        button1.pack()

    def correspondingBehavior(self, choice):
        print(choice)

Before the codes are written, I have imported playsound module and defined the success1() function like this:

def success1():
    playsound("D:/Personal/Game/Yah.mp3")

Upvotes: 2

Views: 329

Answers (1)

Delrius Euphoria
Delrius Euphoria

Reputation: 15098

Don't let it block the main thread:

def success1():
    playsound("D:/Personal/Game/Yah.mp3", block=False)

Or you could create a new thread but that could potentially crash tkinter later.

Upvotes: 3

Related Questions