Amazing-Python-Scripts
118 строк · 3.5 Кб
1from tkinter import *
2from playsound import playsound
3from threading import Thread
4
5
6class padSound:
7def __init__(self, soundLocation):
8self.soundLocation = soundLocation
9
10def given_sound(self):
11playsound(self.soundLocation)
12
13def play_sound(self, event):
14sound = Thread(target=self.given_sound)
15sound.start()
16
17
18# All the locations of the sounds
19kickLocation = './Beat-Board/Sounds/Kick.wav'
20hiHatLocation = './Beat-Board/Sounds/hiHat.wav'
21snareLocation = './Beat-Board/Sounds/snare.wav'
22pad1Location = './Beat-Board/Sounds/Pad1.wav'
23pad2Location = './Beat-Board/Sounds/Pad2.wav'
24pad3Location = './Beat-Board/Sounds/Pad3.wav'
25pad4Location = './Beat-Board/Sounds/Pad4.wav'
26pad5Location = './Beat-Board/Sounds/Pad5.wav'
27pad6Location = './Beat-Board/Sounds/Pad6.wav'
28
29# Create drum objects
30kickDrum = padSound(kickLocation)
31hiHatDrum = padSound(hiHatLocation)
32snareDrum = padSound(snareLocation)
33
34# Create pad objects
35pad1 = padSound(pad1Location)
36pad2 = padSound(pad2Location)
37pad3 = padSound(pad3Location)
38pad4 = padSound(pad4Location)
39pad5 = padSound(pad5Location)
40pad6 = padSound(pad6Location)
41
42
43def create_layout():
44
45# Creates the Frame
46frame_a = Frame(master=main_window, width=500, height=500, bg="black")
47frame_a.grid(rowspan=3, columnspan=3)
48frame_a.focus_set()
49
50# Creates the Buttons
51# ------------------------------------------------
52# Kick Button
53kickButton = Button(text="Kick", height=5, width=10)
54frame_a.bind('q', kickDrum.play_sound)
55kickButton.bind("<Button-1>", kickDrum.play_sound)
56
57# Hi-hat Button
58hihatButton = Button(text="Hi-Hat", height=5, width=10)
59frame_a.bind('w', hiHatDrum.play_sound)
60hihatButton.bind("<Button-1>", hiHatDrum.play_sound)
61
62# Snare Button
63snareButton = Button(text="Snare", height=5, width=10)
64frame_a.bind('e', snareDrum.play_sound)
65snareButton.bind("<Button-1>", snareDrum.play_sound)
66
67# -------------------------------------------------
68# Pad 1
69pad1Button = Button(text="Pad 1", height=5, width=10)
70frame_a.bind('a', pad1.play_sound)
71pad1Button.bind("<Button-1>", pad1.play_sound)
72
73# Pad 2
74pad2Button = Button(text="Pad 2", height=5, width=10)
75frame_a.bind('s', pad2.play_sound)
76pad2Button.bind("<Button-1>", pad2.play_sound)
77
78# Pad 3
79pad3Button = Button(text="Pad 3", height=5, width=10)
80frame_a.bind('d', pad3.play_sound)
81pad3Button.bind("<Button-1>", pad2.play_sound)
82
83# -------------------------------------------------
84# Pad 4
85pad4Button = Button(text="Pad 4", height=5, width=10)
86frame_a.bind('z', pad4.play_sound)
87pad4Button.bind("<Button-1>", pad4.play_sound)
88
89# Pad 5
90pad5Button = Button(text="Pad 5", height=5, width=10)
91frame_a.bind('x', pad5.play_sound)
92pad5Button.bind("<Button-1>", pad5.play_sound)
93
94# Pad 6
95pad6Button = Button(text="Pad 6", height=5, width=10)
96frame_a.bind('c', pad6.play_sound)
97pad6Button.bind("<Button-1>", pad6.play_sound)
98# -------------------------------------------------
99
100# Display Buttons
101kickButton.grid(row=0)
102hihatButton.grid(row=0, column=1)
103snareButton.grid(row=0, column=2)
104
105pad1Button.grid(row=1)
106pad2Button.grid(row=1, column=1)
107pad3Button.grid(row=1, column=2)
108
109pad4Button.grid(row=2)
110pad5Button.grid(row=2, column=1)
111pad6Button.grid(row=2, column=2)
112
113
114main_window = Tk()
115main_window.resizable(False, False)
116main_window.title('Beat Board')
117create_layout()
118main_window.mainloop()
119