How to play sounds in Python with Tkinter?
Python GUI tkinter is very useful when we want to take data from users. User attracts from GUI. GUI is very helpful in day to day life. A graphical user interface helps us to make our daily tasks easier and very productive. If you want to play music with the help of python GUI tkinter, then you come in the right way. For playing sound/music with help of python you need to install the required modules. This, module will help to play sound.
There are two modules to play sound with the help of tkinter python:
- pygame : It is a cross-platform module to create games and GUI’s.
- playsound: It is a cross-platform module and its function name is playsound()
Let’s see how we can play sound/music with help of tkinter python GUI. You have to save your mp3 file in the same folder where you save your python file or you have to give the full path of the mp3 file. The mp3 file used the below methods is given here.
Method 1: (Using playsound)
To install playsound use this command
pip install playsound
- First importing required modules.
- Initializing the Tk() and putting it in the variable for further use.
- Define a function for trigger it with help of a button.
- Create a button to trigger a function with the help of a command.
Syntax : playsound(sound, block=True)
Method 2: (Using pygame)
To install pygame use this command
pip install pygame
- When code will run then there one window open.
- In the window, there is one button. When we click on it there one function will start which plays the song.
- The function needs to be defined above to play the sound.
- Then make a mp3 file that is present on the same folder or when the mp3 file is not present on the same folder then give its full path to play the sound. (Be careful about this)