Qixi event, throw a code and throw it away
When the mouse moves to the left side of the screen, the music `confession balloon.mp3` will be played automatically, which can be adjusted by yourself
Walk through
1. Select your area A,
2. Get mouse coordinates in real time
3. Add a judgment, if the mouse coordinates are in area A, play the music file
pip3 install pynput
pip3 install pygame
python3 main.py
Mouse the mouse to the area A on the left half of the screen to automatically play the music,
Mouse the mouse to the area B on the right half of the screen to automatically stop playing,
main.py
from pynput import mousefrom pygame import mixersize = [1920, 1680] # Modify to your own screen sizemusic_file = "Confession Balloon.mp3" # The path where the mp3 file is located, use an absolute path, or a relative path in the same folder as the filemixer.init()mixer.music.load(music_file)play_music = Falsedef mouse_move(x, y):"""mouse move event:param x: abscissa:param y: ordinate:return:"""global play_musicglobal pif x <= size[0] / 2 and not play_music:play_music = Trueprint("The mouse enters the left area A and starts playing music", music_file)mixer.music.play()if x > size[0] / 2 and play_music:play_music = Falseprint("The mouse entered the right area B, stop playing")mixer.music.stop()# Listen for mouse eventswith mouse.Listener(on_move=mouse_move, # mouse move event) as listener:listener.join()
confession balloon in the same folder, mp3
main.py
python3 main.py
If you have any questions, please ask them
If you have any questions, please leave a message
If it works, please like it
If it fails, please leave a message
Available August 2022