Как сделать звук в python 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
Steps Needed
- 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)
Make your own music player in python
In this tutorial, you’re going to learn how to make your own MP3 Music Player in Python using pygame and Tkinter libraries which is able to load, play, pause, and stop the music.
Requirements
To be able to follow this tutorial you need to have Tkinter and pygame installed on your machine
Installation
- Linux
Exit fullscreen mode
- Window
Exit fullscreen mode
Now once everything is ready Installed, we now ready to begin coding our very own music player
Let’s get started
We gonna use Tkinter to render our application menu and buttons, and also loading the music from files and then pygame library to play, pause and stop the music
Basics of pygame
Pygame has an inbuilt method called mixer () which provides us intuitive syntax on dealing with sounds files in python, we will see ;
- loading and playing music
- pausing and unpausing music
- stoping the music file
loading and playing music with pygame
To play music with pygame you’re firstly supposed to import mixer(), initialize it through init(), *and then using *music.load() to load the music and finally playing it with music.play().
Example of usage
Exit fullscreen mode
Pausing and unpausing music with pygame.
use* music.pause() and music.unpause()* to pause and unpause your music, but make your music file is loaded first before using these methods.
Example of usage
Exit fullscreen mode
Stop a music file
use music.stop() to stop your music completely from playing, once you use this method the loaded music will be cleared in pygame memory.
Exit fullscreen mode
Building your music player exoskeleton
We have already covered the basics of playing music with python, now it’s time to begin designing our application user interface pygame is a cross-platform collection of Python tools used for creating video games. It provides sound and graphics libraries for use with the Python programming language. Pygame makes use of the Simple DirectMedia Layer (SDL) library, which was designed to make real-time computer game production easier by eliminating the low-level mechanics of C programming and its variants. This is based on the notion that the most expensive functions in games can be abstracted from the game logic, allowing the game to be structured using a high-level programming language such as Python. SDL’s other features include vector math, collision detection, 2D sprite scene graph management, MIDI support, camera, pixel-array manipulation, transformations, filtering, advanced freetype font support, and drawing. SDL allows you to use any platform to access your system’s multimedia hardware, such as sound, video, a mouse, keyboard, and a joystick. Because SDL and pygame are cross-platform, you can use them to create games and rich multimedia Python programs for any platform that supports them. You’ll need the following items to follow along with this tutorial: Python must be installed before pygame can be used. When you install Python, you get Tkinter as well. Tkinter has a number of advantages when it comes to creating graphical user interfaces. Python is a cross-platform language, therefore Tkinter programs can run on Windows, Mac OS X, and Linux. We don’t need to install this module because it’s included in the Python standard library. The Operating System provides a variety of capabilities for interacting with it. In this tutorial, we’ll use OS to retrieve a playlist of songs from a certain directory and make it available to the music player. In contrast to text-based user links, typed commands, or text exploration, a graphical user interface (GUI) allows users to interact with electronic symbols. Command-line networks have a steep learning curve and need command input via a computer keyboard, hence GUIs were created in response. Create a new file named musicgui.py where we will write our code The tkinter module gives the user access to the Tkinter widgets. Because it is included in the Python standard library, the os module , which is an Operating System with a variety of features for dealing with it, and it does not need to be installed. A root window is created using the tk class. The play list variable defines the tkr.Listbox widget, which shows a group of lines of text that influences the backdrop color and font of the music player. The pygame.mixer.music.load function was used to start the GUI, the stop function to stop the music, the pause function to pause the music player, and the unpause method to undo the pause key’s action. We also specify the functions of the buttons/keypads that are associated with these functions, such as keypad height, font sizes, and functionalities. The .pack() method checks to see if the widget takes up any extra space that the packer has allotted to it. When "x" is used, the widget can only be filled horizontally. You’ve finished your first Tkinter application, which is fantastic! In this tutorial, We have covered some fundamentals of the Tkinter GUI, and we built a music player. I like to write on health related articles and tech articles kindly follow me on medium and on linkedIn. The media player is in dark theme with all the functionalities of a modern media player we can adjust the volume from the player switch back and front to previous and next songs. We can shuffle our playlist and also can move the song to any position as we want using the song slider. Table of Contents The Above packages are used to make the make the Media Player i. tkinter for using Gui in python Tk() function is used to initialize the root window. The root window is a main application window in our programs. Initial after calling all the packages a class My_music_player() is created and in the class a class constructor is created to initialise: i. Initializing Root window Media player title and Icon Play Frame is created to group the control keys of the player. All the Buttons are Assigned their respective functions using command attribute. The functionalities are stop button for stopping the music, Previous and Next Button for Previous and next songs, Play and Pause Button for playing and pausing the musing, shuffle button is also included to shuffle the play list. Buttons and Lables of Tkinter module is used to create the control key in the play frame. Playerarea Frame is created to show the player area where Image is show for better looks. Volume control functions in MP3 player in python Functions and attributes of Pygame module are use to adjust and set the volume using the slider We can move the song to any position as we want using the song slider. Add Button is also included to add song to the player directly form the computer we can select multiple files also at once. The songs are added at the end of the playlist. The Player Frame with above mentioned fuctionalities how to make your own MP3 music player in python using Tkinter The third Frame used in this media player is song_list Frame. Double click functionality is enables on the listbox to play the selected song. Functions of OS modules are used to fetch the song from the song folder. Many function are created to do task according to the button press from the control area. The functions include function to add song, function to manage double click and playing the song, function to adjust volume, Functions for play and pause music and start and stop music, function to hide playlist, function to shuffle the playlist, functions to mute and unmute. The whole media player looks like this how to make your own MP3 music player in python using Tkinter In this article we saw how to make your own MP3 music player in python using Tkinter so about this article you have any query then free to ask meHow to Use Python Tkinter to Make a Music Player GUI.
Because of Pygame Subset for Android, Pygame applications can run on Android phones and tablets. Android includes support for sound, vibration, a keyboard, and an accelerometer.Table of content
Prerequisites
python installed,
A Python-oriented code editor. I use PyCharm.
You can learn more about graphic user interfaces by reading my previous article on section
Pygame is compatible with all versions of Python, however Python 3.6 is suggested and utilized in this article.
However, you can use the pip command to install it if you don’t have it.What is Tkinter?
Tkinter applications appear to be native to the instance on which they are running. This is the situation because the visual components are built utilizing native system programs.Os module
Graphic user interface
Creating a music player.
Let’s start by importing pygame
Tkinter provides many methods, including the geometry () method. We use it to determine the size of the music player.
The askdirectroy () function allows the music player to access the user’s music file.
The pygame.mixer.init() function is used to load and play sounds, as well as control the rate at which they are played.Conclusion
Как сделать звук в python tkinter
Which Packages Used for create own Mp3 music player using python
ii. tkinter.ttk for using Slider for volume and song track
iii. pygame used for sound libraries
iv. os module for importing songs from file
v. PIL module used for images
vi. Random module used for shuffling playlist
vii. Filedialog used for adding more songs in the player directly
viii. Time module used for tracking song sound
ix. Mutagen used to get the song lengthThe use of the constructor is given below
ii. Title of the player
iii. Icon (bitmap)
iv. Dimension(geometry) of the player
v. Initialised pygame and pygame mixer
vi. Variables declared for track and status
vii. Root.root.resizable(0, 0) function is called to prevent resizing of window
viii. Global variables are declared to use in the player
ix. Initializing variables to use it further as accordingly
All the control keys in the player comes under this frame. Control keys include Stop Button, Previous Button, Play and Pause Button,Shuffle Button and Button for hiding the Playlist.Control Keys for MP3 player in python
The button keys are made using icon and Font Awesome 5 font.
The play pause button chances according to the playing and pausing of musicChange in Play Pause Button in python
Song slider and volume controls are given in this frame.
Song Description and status of the song is also included in this frame.
The duration of the song and time elapsed is also shown in this frame.
The symbol of the volume icon changes on increasing and decreasing of the volume and mute and unmute the volume.
Volume can be adjusted using the slider scrolling up and down
Ttk.scale() is used to create the volume slider.
The Description area shows the Name of the song currently Playing and also the status of the song i.e. Playing, Paused or Stopped.Add Button in MP3 player in python
This frame is used for the playlist of the media player.
Listbox of tkinter is used for the playlist.
Scrolling option on the right side of the playlist is enabled for scrolling along y axis to access songs in the playlist.
Song can also be played using tha play button after selecting the song by clicking once.
Bind() is use to bind the mouse click for playing songs.
Song can also be added using the add button.
Functionalities for previous and next songs.and slider functions.
Source Coding For Create Mp3 music player in python :
We need to First import all necessary Library
Create class & Buttons for our application
Adding Load Method to our MusicPlayer class
Adding Play Method to our class
look at our Final will app (app.py)