程序员文章、书籍推荐和程序员创业信息与资源分享平台

网站首页 > 技术文章 正文

「代码指尖跃,音符心间流,Python筑梦专属电子钢琴」

hfteth 2025-05-09 18:56:32 技术文章 11 ℃

嗨,音乐极客与代码诗人!我是浪仔,你的Python造梦师~

今天,我们将用0与1的魔法, 亲手铸造一把会呼吸的电子钢琴 从颤音的涟漪到和弦的轰鸣, 从黑白键的触感模拟到云端声场的实时渲染, 让每一行代码都成为通往音乐宇宙的虫洞

无需五线谱的束缚,不必受限于物理琴键, 在这里,你的创意就是唯一的法则 准备好,和浪仔一起,在代码的旷野上,谱写属于你的星辰变奏曲了吗?

先上效果

1.解决方案

按照钢琴按钮进行布局界面,琴按钮用普通的按钮就行替代,按钮设置背景颜色和anchor位置方向,白色按钮36个,黑色按钮25个。再对61个按钮进行绑定函数,每个函数调用背景音乐即可。在此处一定要注意线程开启和关闭(强制关闭)。

2.导入包

import tkinter as mytk
from tkinter import *
from tkinter.ttk import *
import os,time
import threading
import pygame
import inspect
import ctypes
from PIL import Image,ImageTk

3.定义类并设置一般参数值

class App(mytk.Tk):
    def __init__(self):
        super().__init__()
        width = 1350
        height = 500
        pygame.mixer.init()
        screenwidth = self.winfo_screenwidth()
        screenheight = self.winfo_screenheight()
        alignstr = "%dx%d+%d+%d" % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        self.geometry(alignstr)
        self.resizable(0, 0)
        # self["bg"] = "#0E2B4A"
        self.iconbitmap('images/piano.ico')
        self.thread_lock = threading.BoundedSemaphore(value=10)  # 设置最大线程数
        self.init_widgets()
        self.get_time()

4.布局控件

定义一个 init_widgets()函数,作为布局控件的入口函数

布局背景图片

self.canva = mytk.Canvas(self, width=1350, height=500)
self.canva.place(x=0, y=0)
self.canva.delete(ALL)
filename = f'{os.getcwd()}\\images\\bj.jpg'
image = Image.open(filename)
newImg = image.resize((1350, 500), Image.BILINEAR)
img = ImageTk.PhotoImage(newImg)
self.canva.create_image(0, 0, image=img, anchor=mytk.NW)
self.canva.image = img

布局36个白色按钮

#白色按钮36个
self.Button_bs1 = mytk.Button(self, text="1", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('C2.mp3'))
self.Button_bs1.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs1.place(x=50, y=100)
self.bind("<KeyPress-1>", lambda event:self.play_sound('C2.mp3'))
self.labelshow1 = mytk.Label(self,text='do',font=("微软雅黑", 10, "normal"),background='#DEDCD0',foreground='#ffffff')
self.labelshow1.place(x=55, y=328)

self.Button_bs2 = mytk.Button(self, text="2", cursor='hand2', foreground='#000000',
                              font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('D2.mp3'))
self.Button_bs2.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs2.place(x=85, y=100)
self.bind("<KeyPress-2>", lambda event: self.play_sound('D2.mp3'))
self.labelshow2 = mytk.Label(self, text='re', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                             foreground='#ffffff')
self.labelshow2.place(x=90, y=328)

self.Button_bs3 = mytk.Button(self, text="3", cursor='hand2', foreground='#000000',
                              font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('E2.mp3'))
self.Button_bs3.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs3.place(x=120, y=100)
self.bind("<KeyPress-3>", lambda event: self.play_sound('E2.mp3'))
self.labelshow3 = mytk.Label(self, text='mi', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                             foreground='#ffffff')
self.labelshow3.place(x=125, y=328)

self.Button_bs4 = mytk.Button(self, text="4", cursor='hand2', foreground='#000000',
                              font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('F2.mp3'))
self.Button_bs4.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs4.place(x=155, y=100)
self.bind("<KeyPress-4>", lambda event: self.play_sound('F2.mp3'))
self.labelshow4 = mytk.Label(self, text='fa', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                             foreground='#ffffff')
self.labelshow4.place(x=160, y=328)

self.Button_bs5 = mytk.Button(self, text="5", cursor='hand2', foreground='#000000',
                              font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('G2.mp3'))
self.Button_bs5.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs5.place(x=190, y=100)
self.bind("<KeyPress-5>", lambda event: self.play_sound('G2.mp3'))
self.labelshow5 = mytk.Label(self, text='so', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                             foreground='#ffffff')
self.labelshow5.place(x=195, y=328)

self.Button_bs6 = mytk.Button(self, text="6", cursor='hand2', foreground='#000000',
                              font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('A2.mp3'))
self.Button_bs6.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs6.place(x=225, y=100)
self.bind("<KeyPress-6>", lambda event: self.play_sound('A2.mp3'))
self.labelshow6 = mytk.Label(self, text='la', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                             foreground='#ffffff')
self.labelshow6.place(x=230, y=328)

self.Button_bs7 = mytk.Button(self, text="7", cursor='hand2', foreground='#000000',
                              font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('B2.mp3'))
self.Button_bs7.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs7.place(x=260, y=100)
self.bind("<KeyPress-7>", lambda event: self.play_sound('B2.mp3'))
self.labelshow7 = mytk.Label(self, text='si', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                             foreground='#ffffff')
self.labelshow7.place(x=265, y=328)

self.Button_bs8 = mytk.Button(self, text="8", cursor='hand2', foreground='#000000',
                              font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('C3.mp3'))
self.Button_bs8.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs8.place(x=295, y=100)
self.bind("<KeyPress-8>", lambda event: self.play_sound('C3.mp3'))
self.labelshow8 = mytk.Label(self, text='do', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                             foreground='#FBB957')
self.labelshow8.place(x=300, y=328)

self.Button_bs9 = mytk.Button(self, text="9", cursor='hand2', foreground='#000000',
                              font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('D3.mp3'))
self.Button_bs9.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs9.place(x=330, y=100)
self.bind("<KeyPress-9>", lambda event: self.play_sound('D3.mp3'))
self.labelshow9 = mytk.Label(self, text='re', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                             foreground='#FBB957')
self.labelshow9.place(x=335, y=328)

self.Button_bs10 = mytk.Button(self, text="0", cursor='hand2', foreground='#000000',
                              font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('E3.mp3'))
self.Button_bs10.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs10.place(x=365, y=100)
self.bind("<KeyPress-0>", lambda event: self.play_sound('E3.mp3'))
self.labelshow10 = mytk.Label(self, text='mi', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                             foreground='#FBB957')
self.labelshow10.place(x=370, y=328)

self.Button_bs11 = mytk.Button(self, text="q", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('F3.mp3'))
self.Button_bs11.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs11.place(x=400, y=100)
self.bind("<KeyPress-q>", lambda event: self.play_sound('F3.mp3'))
self.labelshow11 = mytk.Label(self, text='fa', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#FBB957')
self.labelshow11.place(x=405, y=328)

self.Button_bs12 = mytk.Button(self, text="w", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('G3.mp3'))
self.Button_bs12.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs12.place(x=435, y=100)
self.bind("<KeyPress-w>", lambda event: self.play_sound('G3.mp3'))
self.labelshow12 = mytk.Label(self, text='so', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#FBB957')
self.labelshow12.place(x=440, y=328)

self.Button_bs13 = mytk.Button(self, text="e", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('A3.mp3'))
self.Button_bs13.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs13.place(x=470, y=100)
self.bind("<KeyPress-e>", lambda event: self.play_sound('A3.mp3'))
self.labelshow13 = mytk.Label(self, text='la', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#FBB957')
self.labelshow13.place(x=475, y=328)

self.Button_bs14 = mytk.Button(self, text="r", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('B3.mp3'))
self.Button_bs14.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs14.place(x=505, y=100)
self.bind("<KeyPress-r>", lambda event: self.play_sound('B3.mp3'))
self.labelshow14 = mytk.Label(self, text='si', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#FBB957')
self.labelshow14.place(x=510, y=328)

self.Button_bs15 = mytk.Button(self, text="t", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('C4.mp3'))
self.Button_bs15.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs15.place(x=540, y=100)
self.bind("<KeyPress-t>", lambda event: self.play_sound('C4.mp3'))
self.labelshow15 = mytk.Label(self, text='do', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#FBB957')
self.labelshow15.place(x=545, y=328)

self.Button_bs16 = mytk.Button(self, text="y", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('D4.mp3'))
self.Button_bs16.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs16.place(x=575, y=100)
self.bind("<KeyPress-y>", lambda event: self.play_sound('D4.mp3'))
self.labelshow16 = mytk.Label(self, text='re', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#FBB957')
self.labelshow16.place(x=580, y=328)

self.Button_bs17 = mytk.Button(self, text="u", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('E4.mp3'))
self.Button_bs17.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs17.place(x=610, y=100)
self.bind("<KeyPress-u>", lambda event: self.play_sound('E4.mp3'))
self.labelshow17 = mytk.Label(self, text='mi', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#FBB957')
self.labelshow17.place(x=615, y=328)

self.Button_bs18 = mytk.Button(self, text="i", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('F4.mp3'))
self.Button_bs18.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs18.place(x=645, y=100)
self.bind("<KeyPress-i>", lambda event: self.play_sound('F4.mp3'))
self.labelshow18 = mytk.Label(self, text='fa', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#FBB957')
self.labelshow18.place(x=650, y=328)

self.Button_bs19 = mytk.Button(self, text="o", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('G4.mp3'))
self.Button_bs19.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs19.place(x=680, y=100)
self.bind("<KeyPress-o>", lambda event: self.play_sound('G4.mp3'))
self.labelshow19 = mytk.Label(self, text='so', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#FBB957')
self.labelshow19.place(x=685, y=328)

self.Button_bs20 = mytk.Button(self, text="p", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('A4.mp3'))
self.Button_bs20.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs20.place(x=715, y=100)
self.bind("<KeyPress-p>", lambda event: self.play_sound('A4.mp3'))
self.labelshow20 = mytk.Label(self, text='la', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#FBB957')
self.labelshow20.place(x=720, y=328)

self.Button_bs21 = mytk.Button(self, text="a", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('B4.mp3'))
self.Button_bs21.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs21.place(x=750, y=100)
self.bind("<KeyPress-a>", lambda event: self.play_sound('B4.mp3'))
self.labelshow21 = mytk.Label(self, text='si', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#FBB957')
self.labelshow21.place(x=755, y=328)

self.Button_bs22 = mytk.Button(self, text="s", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('C5.mp3'))
self.Button_bs22.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs22.place(x=785, y=100)
self.bind("<KeyPress-s>", lambda event: self.play_sound('C5.mp3'))
self.labelshow22 = mytk.Label(self, text='do', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#EE8055')
self.labelshow22.place(x=790, y=328)

self.Button_bs23 = mytk.Button(self, text="d", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('D5.mp3'))
self.Button_bs23.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs23.place(x=820, y=100)
self.bind("<KeyPress-d>", lambda event: self.play_sound('D5.mp3'))
self.labelshow23 = mytk.Label(self, text='re', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#EE8055')
self.labelshow23.place(x=825, y=328)

self.Button_bs24 = mytk.Button(self, text="f", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('E5.mp3'))
self.Button_bs24.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs24.place(x=855, y=100)
self.bind("<KeyPress-f>", lambda event: self.play_sound('E5.mp3'))
self.labelshow24 = mytk.Label(self, text='mi', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#EE8055')
self.labelshow24.place(x=860, y=328)

self.Button_bs25 = mytk.Button(self, text="g", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('F5.mp3'))
self.Button_bs25.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs25.place(x=890, y=100)
self.bind("<KeyPress-g>", lambda event: self.play_sound('F5.mp3'))
self.labelshow25 = mytk.Label(self, text='fa', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#EE8055')
self.labelshow25.place(x=895, y=328)

self.Button_bs26 = mytk.Button(self, text="h", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('G5.mp3'))
self.Button_bs26.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs26.place(x=925, y=100)
self.bind("<KeyPress-h>", lambda event: self.play_sound('G5.mp3'))
self.labelshow26 = mytk.Label(self, text='so', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#EE8055')
self.labelshow26.place(x=930, y=328)

self.Button_bs27 = mytk.Button(self, text="j", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('A5.mp3'))
self.Button_bs27.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs27.place(x=960, y=100)
self.bind("<KeyPress-j>", lambda event: self.play_sound('A5.mp3'))
self.labelshow27 = mytk.Label(self, text='la', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#EE8055')
self.labelshow27.place(x=965, y=328)

self.Button_bs28 = mytk.Button(self, text="k", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('B5.mp3'))
self.Button_bs28.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs28.place(x=995, y=100)
self.bind("<KeyPress-k>", lambda event: self.play_sound('B5.mp3'))
self.labelshow28 = mytk.Label(self, text='si', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#EE8055')
self.labelshow28.place(x=1000, y=328)

self.Button_bs29 = mytk.Button(self, text="l", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('C6.mp3'))
self.Button_bs29.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs29.place(x=1030, y=100)
self.bind("<KeyPress-l>", lambda event: self.play_sound('C6.mp3'))
self.labelshow29 = mytk.Label(self, text='do', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#F07C82')
self.labelshow29.place(x=1035, y=328)

self.Button_bs30 = mytk.Button(self, text="z", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('D6.mp3'))
self.Button_bs30.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs30.place(x=1065, y=100)
self.bind("<KeyPress-z>", lambda event: self.play_sound('D6.mp3'))
self.labelshow30 = mytk.Label(self, text='re', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#F07C82')
self.labelshow30.place(x=1070, y=328)

self.Button_bs31 = mytk.Button(self, text="x", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('E6.mp3'))
self.Button_bs31.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs31.place(x=1100, y=100)
self.bind("<KeyPress-x>", lambda event: self.play_sound('E6.mp3'))
self.labelshow31 = mytk.Label(self, text='mi', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#F07C82')
self.labelshow31.place(x=1105, y=328)

self.Button_bs32 = mytk.Button(self, text="c", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('F6.mp3'))
self.Button_bs32.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs32.place(x=1135, y=100)
self.bind("<KeyPress-c>", lambda event: self.play_sound('F6.mp3'))
self.labelshow32 = mytk.Label(self, text='fa', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#F07C82')
self.labelshow32.place(x=1140, y=328)

self.Button_bs33 = mytk.Button(self, text="v", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('G6.mp3'))
self.Button_bs33.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs33.place(x=1170, y=100)
self.bind("<KeyPress-v>", lambda event: self.play_sound('G6.mp3'))
self.labelshow33 = mytk.Label(self, text='so', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#F07C82')
self.labelshow33.place(x=1175, y=328)

self.Button_bs34 = mytk.Button(self, text="b", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('A6.mp3'))
self.Button_bs34.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs34.place(x=1205, y=100)
self.bind("<KeyPress-b>", lambda event: self.play_sound('A6.mp3'))
self.labelshow34 = mytk.Label(self, text='la', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#F07C82')
self.labelshow34.place(x=1210, y=328)

self.Button_bs35 = mytk.Button(self, text="n", cursor='hand2', foreground='#000000',
                               font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('B6.mp3'))
self.Button_bs35.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs35.place(x=1240, y=100)
self.bind("<KeyPress-n>", lambda event: self.play_sound('B6.mp3'))
self.labelshow35 = mytk.Label(self, text='si', font=("微软雅黑", 10, "normal"), background='#DEDCD0',
                              foreground='#F07C82')
self.labelshow35.place(x=1245, y=328)

self.Button_bs36 = mytk.Button(self, text="m", cursor='hand2', foreground='#000000',font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('C7.mp3'))
self.Button_bs36.config(background='#FBFBFB', width=2, height=8, activebackground='#FACC94', activeforeground='#343A3F')  # FBFBFB
self.Button_bs36.place(x=1275, y=100)
self.bind("<KeyPress-m>", lambda event: self.play_sound('C7.mp3'))
self.labelshow36 = mytk.Label(self, text='do', font=("微软雅黑", 10, "normal"), background='#DEDCD0',foreground='#F62E1D')
self.labelshow36.place(x=1280, y=328)

布局25个黑色按钮

#黑色按钮25个
self.Button_hs1 = mytk.Button(self, text="!",cursor='hand2',foreground='#FFFFFF',font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Cs2.mp3'))
self.Button_hs1.config(background='#020202',width=1,height=6, activebackground='#76C4EE')  #FBFBFB
self.Button_hs1.place(x=72, y=100)
self.bind("<KeyPress-!>", lambda event: self.play_sound('Cs2.mp3'))
self.Button_hs2 = mytk.Button(self, text="@", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Ds2.mp3'))
self.Button_hs2.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs2.place(x=107, y=100)
self.bind("<KeyPress-@>", lambda event: self.play_sound('Ds2.mp3'))

self.Button_hs3 = mytk.Button(self, text="#34;, cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Fs2.mp3'))
self.Button_hs3.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs3.place(x=177, y=100)
self.bind("<KeyPress-!>", lambda event: self.play_sound('Fs2.mp3'))

self.Button_hs4 = mytk.Button(self, text="%", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Gs2.mp3'))
self.Button_hs4.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs4.place(x=212, y=100)
self.bind("<KeyPress-%>", lambda event: self.play_sound('Gs2.mp3'))

self.Button_hs5 = mytk.Button(self, text="^", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('As2.mp3'))
self.Button_hs5.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs5.place(x=247, y=100)
self.bind("<KeyPress-^>", lambda event: self.play_sound('As2.mp3'))

self.Button_hs6 = mytk.Button(self, text="*", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Cs3.mp3'))
self.Button_hs6.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs6.place(x=317, y=100)
self.bind("<KeyPress-*>", lambda event: self.play_sound('Cs3.mp3'))

self.Button_hs7 = mytk.Button(self, text="(", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Ds3.mp3'))
self.Button_hs7.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs7.place(x=352, y=100)
self.bind("<KeyPress-(>", lambda event: self.play_sound('Ds3.mp3'))

self.Button_hs8 = mytk.Button(self, text="Q", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Fs3.mp3'))
self.Button_hs8.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs8.place(x=422, y=100)
self.bind("<KeyPress-Q>", lambda event: self.play_sound('Fs3.mp3'))

self.Button_hs9 = mytk.Button(self, text="W", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Gs3.mp3'))
self.Button_hs9.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs9.place(x=457, y=100)
self.bind("<KeyPress-W>", lambda event: self.play_sound('Gs3.mp3'))

self.Button_hs10 = mytk.Button(self, text="E", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('As3.mp3'))
self.Button_hs10.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs10.place(x=492, y=100)
self.bind("<KeyPress-E>", lambda event: self.play_sound('As3.mp3'))

self.Button_hs11 = mytk.Button(self, text="T", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Cs4.mp3'))
self.Button_hs11.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs11.place(x=562, y=100)
self.bind("<KeyPress-T>", lambda event: self.play_sound('Cs4.mp3'))

self.Button_hs12 = mytk.Button(self, text="Y", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Ds4.mp3'))
self.Button_hs12.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs12.place(x=597, y=100)
self.bind("<KeyPress-Y>", lambda event: self.play_sound('Ds4.mp3'))

self.Button_hs13 = mytk.Button(self, text="I", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Fs4.mp3'))
self.Button_hs13.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs13.place(x=667, y=100)
self.bind("<KeyPress-I>", lambda event: self.play_sound('Fs4.mp3'))

self.Button_hs14 = mytk.Button(self, text="O", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"), anchor='s',command=lambda:self.play_sound('Gs4.mp3'))
self.Button_hs14.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs14.place(x=702, y=100)
self.bind("<KeyPress-O>", lambda event: self.play_sound('Gs4.mp3'))

self.Button_hs15 = mytk.Button(self, text="P", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('As4.mp3'))
self.Button_hs15.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs15.place(x=737, y=100)
self.bind("<KeyPress-P>", lambda event: self.play_sound('As4.mp3'))

self.Button_hs16 = mytk.Button(self, text="S", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Cs5.mp3'))
self.Button_hs16.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs16.place(x=807, y=100)
self.bind("<KeyPress-S>", lambda event: self.play_sound('Cs5.mp3'))

self.Button_hs17 = mytk.Button(self, text="D", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Ds5.mp3'))
self.Button_hs17.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs17.place(x=842, y=100)
self.bind("<KeyPress-D>", lambda event: self.play_sound('Ds5.mp3'))

self.Button_hs18 = mytk.Button(self, text="G", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Fs5.mp3'))
self.Button_hs18.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs18.place(x=912, y=100)
self.bind("<KeyPress-G>", lambda event: self.play_sound('Fs5.mp3'))

self.Button_hs19 = mytk.Button(self, text="H", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Gs5.mp3'))
self.Button_hs19.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs19.place(x=947, y=100)
self.bind("<KeyPress-H>", lambda event: self.play_sound('Gs5.mp3'))

self.Button_hs20 = mytk.Button(self, text="J", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('As5.mp3'))
self.Button_hs20.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs20.place(x=982, y=100)
self.bind("<KeyPress-J>", lambda event: self.play_sound('As5.mp3'))

self.Button_hs21 = mytk.Button(self, text="L", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Cs6.mp3'))
self.Button_hs21.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs21.place(x=1052, y=100)
self.bind("<KeyPress-L>", lambda event: self.play_sound('Cs6.mp3'))

self.Button_hs22 = mytk.Button(self, text="Z", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Ds6.mp3'))
self.Button_hs22.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs22.place(x=1087, y=100)
self.bind("<KeyPress-Z>", lambda event: self.play_sound('Ds6.mp3'))

self.Button_hs23 = mytk.Button(self, text="C", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Fs6.mp3'))
self.Button_hs23.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs23.place(x=1157, y=100)
self.bind("<KeyPress-C>", lambda event: self.play_sound('Fs6.mp3'))

self.Button_hs24 = mytk.Button(self, text="V", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('Gs6.mp3'))
self.Button_hs24.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs24.place(x=1192, y=100)
self.bind("<KeyPress-V>", lambda event: self.play_sound('Gs6.mp3'))

self.Button_hs25 = mytk.Button(self, text="B", cursor='hand2', foreground='#FFFFFF', font=("微软雅黑", 14, "bold"),anchor='s',command=lambda:self.play_sound('As6.mp3'))
self.Button_hs25.config(background='#020202', width=1, height=6, activebackground='#76C4EE')  # FBFBFB
self.Button_hs25.place(x=1227, y=100)
self.bind("<KeyPress-B>", lambda event: self.play_sound('As6.mp3'))

界面布局效果如下

5.定义方法

播放声音,线程解决

#播放声音
def play_sound(self, name,*args):
    ts = threading.enumerate()
    if len(ts) == 1:
        self.thread_lock.acquire()
        t = threading.Thread(target=self.play_sound_,args=(name,))
        t.start()
    else:
        for i in range(1,len(ts)):
            self.stop_thread(ts[i])
        self.thread_lock.acquire()
        t = threading.Thread(target=self.play_sound_, args=(name,))
        t.start()
def play_sound_(self,name):
    #开始播放声音
    path = f'{os.getcwd()}\\sounds\\{name}'
    pygame.mixer.init()
    pygame.mixer.music.load(path)
    pygame.mixer.music.play()
    #释放
    self.thread_lock.release()

强制停止线程

def _async_raise(self, tid, exctype):
    tid = ctypes.c_long(tid)
    if not inspect.isclass(exctype):
        exctype = type(exctype)
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
    if res == 0:
        raise ValueError("invalid thread id")
    elif res != 1:
        ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
        raise SystemError("PyThreadState_SetAsyncExc failed")
def stop_thread(self, thread):
    self._async_raise(thread.ident, SystemExit)

上述代码,全部整合,把类进行实例化,直接运行就能得到钢琴效果

Tags:

最近发表
标签列表