I browsed station B a while ago, and saw that some Matlabpianos made by up masters are very popular and fun.

picture

As a technical blogger in the Python area, the first thought is of course whether I can make one in Python, which is both fun and can be used for teaching. After some research, I finally came up with a simplified version👇

Get up early Python

, like 165

This article will share ideas and codes.

Realize ideas

Before writing the code, you should sort out your ideas first. If you want to Pythonimplement it, you only need to create an interface and buttons, and bind each button to a function to play scales. Such a simple function will definitely not need PyQtsuch a big guy. tkinterPerfectly realized.

The following question goes to how to Pythonplay audio files, first searched, and learned about playsoundthe library

from playsound import playsound  
playsound('A4.mp3')

As shown in the above code, two lines of code can be played mp3, but it is limited to this, and functions such as the playback duration cannot be specified. If the duration of a scale is 8splayed, the next one must be played after the previous one is played. In this case, the output sound cannot be coherent.

After continuing research, I found that pygameaudio playback and simple processing can be perfectly realized, and the timeplayback duration can be controlled with the library, for example, each scale is only played for 1s

from pygame import mixer
pygame.mixer.music.play()
time.sleep(10)
pygame.mixer.music.stop()

But in this case, there will be a pop when switching between the two scales, so you can use fadeout(time)it to fade out, the volume will gradually change from the initial value to 0 within a specified time, and finally stop playing.

But in this case, there will be a delay when the two scales are switched. If you want to have no delay, you need to use multi-threading to play, but tkinterit seems to be more complicated to use multi-threading in Chinese, so switch ideas and use mixer.Soundmethods to play , still two lines of code can play, and can press multiple keys at the same time!

from pygame import mixer
mixer.Sound("mp3/Cs1.wav").play()

After the music is played, the physical work is done, let's start writing the code!

Python implementation

The first is to download all the corresponding scale files, and you can find it by simply looking for a piano website F12. As for tkinterthe part, there are not too many parts, just the regular creation of titles and buttons

master.title("Python_Piano_GUI")
master.geometry("1766x460")
self.Cs1_button = Button(master,bg="black", fg='white',text="C1#", command=Cs1, height=180, width=50)
self.Cs1_button.grid(row=1, columnspan=2)
self.D1_button = Button(master, bg="white", text="D1", height=200, width=50)
self.D1_button.grid(row=5, column=1)
self.Ds1_button = Button(master, bg="black", fg="white", text="D1#", command=Ds1, height=180, width=50)
self.Ds1_button.grid(row=1, columnspan=4)

·······

It should be noted that under the system, the background color of the adjustment button maccannot be set , and it needs to be installed to solve it.backgroundtkmacosx

In addition, in order to change the color of the button corresponding to the pressed button, it is necessary to monitor the press ( press) and release ( release) of the system keyboard

def on_key_release(event):
if event.keysym in keys:
keys[event.keysym].config(bg=btn_bg)
master.bind('<KeyPress>', play_music)
master.bind("<KeyRelease>", on_key_release)

The rest of the code is just repetitive and boring copy-paste to play music, which will not be explained here. The final effect is as follows
picture