Use Python to make your exclusive music player (romance only belongs to you at this moment *´▽`*)


foreword

Yesterday was the birthday of a friend of the blogger. In addition to sending a big red envelope, knowing that he likes to listen to music, I specially wrote him an exclusive .

insert image description here


1. Project introduction

The page styles of our commonly used music players, such as Kugou, QQ Music, etc., are all officially set, and users cannot change them according to their own needs, but this exclusive . As an ordinary user, you can change the page layout, background color, and the name of the player according to your imagination.

Of course, the most important thing is that the memory occupied by this player is very very small, only 1.9KB , and any computer with any configuration can run it . Compared with those players with a size of tens or hundreds of megabytes, is it hi? It's time to take off ヾ(✿゚▽゚)ノ.

insert image description here

Let's take a quick look and see the effect:


2. Environment configuration

Before formally writing code, we need to install and configure the following tools in advance:

  • Download and install the Python interpreter
  • Download and install Pycharm
  • Correctly configure the Python interpreter into Pycharm
  • The tripartite library pygame needs to be installed additionally

First , we need to install Python because this project is written in Python language. If there are friends who can’t download and install it, you can read the blogger’s previous blog [The first step of a million programmers, learn to install Python ( Super detailed oh) ].

Second , you need to install Pycharm because it is a very useful tool for writing code. All our Python codes will be written on this editor. Blog [ Detailed tutorial on Pycharm installation and configuration ].

Third , you need to configure the installed Python into Pycharm because Pycharm can only write code, but it cannot translate the code into binary machine code that the computer can understand and execute, so you need to configure the Python interpreter so that all the code is written. After that, it can be explained and executed. As for the detailed steps of configuration, those who don’t understand can refer to the blog mentioned in the second article, so I won’t repeat them here.

Fourth , we need to additionally install the tripartite library pygame because this library will be used later in the code. The reason why it is called a tripartite library, as the name implies, is a library from a third-party source, that is, after Python is installed, it does not have this library. , we need to install it additionally, the specific installation method is as follows:

  1. Press the Win + R keys on the keyboard at the same time to open the run box, the Win key is the Windows icon key

  2. Enter the three letters cmd in the run box, and then click OK to enter the black console

  3. Enter the following command in the console, if you are worried about making mistakes, you can directly copy the following command
    pip install pygame -i https://mirrors.aliyun.com/pypi/simple/

  4. After the input is complete, press the Enter key (that is, the Enter key) and wait for the installation to complete. The wording of successful installation is shown in the red box in the figure below.
    insert image description here


3. Code combat

There are several parts in the actual code combat that I will introduce to you separately.

The first is all the libraries we need to import , as follows:

import os
import pygame
import tkinter as t
from tkinter.filedialog import askdirectory

where the os library isPython's own operating system library, without additional installation, the function is to process files and folders in the computer , such as creating, deleting, querying, etc., because if our player wants to play local songs, it must open the local folder to read all song directories, here is Need to use the os library;

The pygame library isThird Party Game Library, the function of this library is mainly used for all scenes in game development , such as actions, sounds, images and so on. The function in this project is to load and play the sound. Because we need to play, pause and other operations after reading the song, we need to use the pygame library. Of course, this library needs to be installed additionally, as we have mentioned above.

The tkinter library isPython's own window design library, without additional installation, the function is to design the graphical interface window . For example, if we open any software that has a window appearance, then these appearance elements can be designed. In the project, because we need to design the name of the player window, the size of the partition inside, the color, etc., we need to use the tkinter library.

The second is the player window size and position design , the code is as follows:

screenWidth = music_player.winfo_screenwidth()
screenHeight = music_player.winfo_screenheight()
width = 700
height = 700
left = (screenWidth - width) / 2
top = (screenHeight - height) / 2
music_player.geometry("%dx%d+%d+%d" % (width, height, left, top))

If a friend wants to change the size and position of the player window by himself, he can change the numerical parameters in the above code.

Then there is the style layout of the controls in the player , the code is as follows:

Button1 = t.Button(music_player, width=5, height=3, font="Helvetica 12 bold", text="播放", command=play, bg="SeaGreen1", fg="white")
Button2 = t.Button(music_player, width=5, height=3, font="Helvetica 12 bold", text="停止", command=stop, bg="red", fg="white")
Button3 = t.Button(music_player, width=5, height=3, font="Helvetica 12 bold", text="暂停", command=pause, bg="Orchid1", fg="white")
Button4 = t.Button(music_player, width=5, height=3, font="Helvetica 12 bold", text="取消暂停", command=unpause, bg="Yellow4", fg="white")

If you want to change the style of the control according to your preferences, you need to change the parameters in the brackets of the above code.

Finally, attach all the code as follows :

import os
import pygame
import tkinter as t
from tkinter.filedialog import askdirectory

music_player = t.Tk()
music_player.title("小十一的专属音乐播放器")
screenWidth = music_player.winfo_screenwidth()
screenHeight = music_player.winfo_screenheight()
width = 700
height = 700
left = (screenWidth - width) / 2
top = (screenHeight - height) / 2
music_player.geometry("%dx%d+%d+%d" % (width, height, left, top))
directory = askdirectory()
os.chdir(directory)
song_list = os.listdir()
play_list = t.Listbox(music_player, font="Helvetica 12 bold", bg='SkyBlue1', selectmode=t.SINGLE)
for item in song_list:
    pos = 0
    play_list.insert(pos, item)
    pos += 1
pygame.init()
pygame.mixer.init()


# 播放
def play():
    pygame.mixer.music.load(play_list.get(t.ACTIVE))
    var.set(play_list.get(t.ACTIVE))
    pygame.mixer.music.play()


# 停止
def stop():
    pygame.mixer.music.stop()


# 暂停
def pause():
    pygame.mixer.music.pause()


# 取消暂停
def unpause():
    pygame.mixer.music.unpause()


Button1 = t.Button(music_player, width=5, height=3, font="Helvetica 12 bold", text="播放", command=play, bg="SeaGreen1", fg="white")
Button2 = t.Button(music_player, width=5, height=3, font="Helvetica 12 bold", text="停止", command=stop, bg="red", fg="white")
Button3 = t.Button(music_player, width=5, height=3, font="Helvetica 12 bold", text="暂停", command=pause, bg="Orchid1", fg="white")
Button4 = t.Button(music_player, width=5, height=3, font="Helvetica 12 bold", text="取消暂停", command=unpause, bg="Yellow4", fg="white")
var = t.StringVar()
song_title = t.Label(music_player, font="Helvetica 12 bold", textvariable=var)
song_title.pack()
Button1.pack(fill="x")
Button2.pack(fill="x")
Button3.pack(fill="x")
Button4.pack(fill="x")
play_list.pack(fill="both", expand="yes")
music_player.mainloop()

Guess you like

Origin blog.csdn.net/2201_75641637/article/details/128540937