方法1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
from gpiozero import LED,Button,LEDBoard,ButtonBoard,TonalBuzzer from time import sleep #引用时间库 from signal import pause cal = ButtonBoard(11, 25, 9, 10) row = LEDBoard(24, 23, 22, 27) b = TonalBuzzer(17,octaves=3)#A1 A4 A7 row.value = [0,0,0,0] def key(): row.value = [0,1,1,1] if sum(cal.value)>0: if cal.value[0]: print("S13") b.play("A2") elif cal.value[1]: print("S9") b.play("B2") elif cal.value[2]: print("S5") b.play("C3") else: print("S1") b.play("D3") #print("row0,col", cal.value.index(1)) #print(cal.value) row.value = [1,0,1,1] if sum(cal.value)>0: if cal.value[0]: print("S14") b.play("E3") elif cal.value[1]: print("S10") b.play("F3") elif cal.value[2]: print("S6") b.play("G3") else: print("S2") b.play("A3") #print(cal.value) row.value = [1,1,0,1] if sum(cal.value)>0: if cal.value[0]: print("S15") b.play("B3") elif cal.value[1]: print("S11") b.play("C4") elif cal.value[2]: print("S7") b.play("D4") else: print("S3") b.play("E4") #print(cal.value) row.value = [1,1,1,0] if sum(cal.value)>0: if cal.value[0]: print("S16") b.play("F4") elif cal.value[1]: print("S12") b.play("G4") elif cal.value[2]: print("S8") b.play("A4") else: print("S4") b.play("B4") #print(cal.value) row.value = [0,0,0,0] sleep(0.1) b.stop() cal.when_pressed = key pause() ''' def key(): row.value = [0,1,1,1] if sum(cal.value)>0: if cal.value[0]: print("S13") elif cal.value[1]: print("S9") elif cal.value[2]: print("S5") else: print("S1") #print("row0,col", cal.value.index(1)) #print(cal.value) row.value = [1,0,1,1] if sum(cal.value)>0: if cal.value[0]: print("S14") elif cal.value[1]: print("S10") elif cal.value[2]: print("S6") else: print("S2") #print(cal.value) row.value = [1,1,0,1] if sum(cal.value)>0: if cal.value[0]: print("S15") elif cal.value[1]: print("S11") elif cal.value[2]: print("S7") else: print("S3") #print(cal.value) row.value = [1,1,1,0] if sum(cal.value)>0: if cal.value[0]: print("S16") elif cal.value[1]: print("S12") elif cal.value[2]: print("S8") else: print("S4") #print(cal.value) row.value = [0,0,0,0] #sleep(0.1) cal.when_pressed = key pause() ''' ''' while True: #cal.wait_for_press() #print(cal.value) row.value = [0,0,0,0] if cal.is_pressed: print(cal.value) row.value = [0,1,1,1] if cal.is_pressed: print("r1") for key in range(len(cal.value)): if cal.value[key] == True: print("row0,cal",key,"is pressed") row.value = [1,0,1,1] if cal.is_pressed: print("r2") for key in range(len(cal.value)): if cal.value[key] == True: print("row1,cal",key,"is pressed") row.value = [1,1,0,1] if cal.is_pressed: print("r3") for key in range(len(cal.value)): if cal.value[key] == True: print("row2,cal",key,"is pressed") row.value = [1,1,1,0] if cal.is_pressed: print("r4") for key in range(len(cal.value)): if cal.value[key] == True: print("row3,cal",key,"is pressed") #else: #print("no key") # cal.wait_for_release() sleep(1) pause() ''' ''' from gpiozero import Button from time import sleep #引用时间库 button = Button(11) while True: if button.is_pressed: print("Button is pressed") else: print("Button is not pressed") sleep(1) ''' |
方法2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
from gpiozero import LED,Button,LEDBoard,ButtonBoard,TonalBuzzer from time import sleep #引用时间库 from signal import pause cal = ButtonBoard(11, 25, 9, 10) row = LEDBoard(24, 23, 22, 27) b = TonalBuzzer(17,octaves=3)#A1 A4 A7 mtone = ["C3", "D3", "E3", "F3", "G3", "A3", "B3", "C4", "D4", "E4", "F4", "G4", "A4", "B4", "C5", "D5", ] row.value = [0,0,0,0] keyvalue = [] def key(): global keyvalue if sum(cal.value)>0: row.value = [0,1,1,1] keyvalue += (list(cal.value)) row.value = [1,0,1,1] keyvalue += (list(cal.value)) row.value = [1,1,0,1] keyvalue += (list(cal.value)) row.value = [1,1,1,0] keyvalue += (list(cal.value)) # print(keyvalue) print(keyvalue.index(1)) b.play(mtone[keyvalue.index(1)]) sleep(0.3) b.stop() keyvalue = [] row.value = [0,0,0,0] cal.when_pressed = key pause() |
tk电子琴
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import tkinter from gpiozero import LED,TonalBuzzer from functools import partial #import tkMessageBox top = tkinter.Tk() bz = TonalBuzzer(17,octaves=3)#A1 A4 A7 mtone = [ "C4", "D4", "E4", "F4", "G4", "A4", "B4", "C5", "D5", ] def call_release(event): b.stop() #print("LED release") #方法1: #def button_p(id): # def a(e): # print(id) # return a #方法2 #def button_p(i): # BT = tkinter.Button(top, text = mtone[i]) # BT.bind('<Button-1>',lambda x:bz.play(mtone[i]))#lambda x:bz.play(x.)) # BT.pack(side = tkinter.LEFT) # #list(map(button_p, range(8))) #方法3 #for i in range(8): # BT = tkinter.Button(top, text = mtone[i]) ## BT.bind('<Button-1>',button_p(i)) #方法1的代码 # BT.bind('<Button-1>',lambda x:bz.play(x.widget["text"]))#lambda x:bz.play(x.)) # BT.pack(side = tkinter.LEFT) #方法4 for tone in mtone: BT = tkinter.Button(top, text = tone) # BT.bind('<Button-1>',button_p(i)) #方法1的代码 BT.bind('<Button-1>',lambda x:bz.play(x.widget["text"]))#lambda x:bz.play(x.)) BT.pack(side = tkinter.LEFT) top.bind('<ButtonRelease-1>',lambda x:bz.stop()) top.mainloop() |
转载请注明:徐自远的乱七八糟小站 » 2.1 4×4行列按键+TK电子琴