Putting the keyboard together

Now it's finally time to combine all the preceding methods to build our complete keyboard of two octaves. 

We begin by defining the exact x_coordinates for all the keys from C1 to B2 in the file constants.py as follows:

WHITE_KEY_X_COORDINATES = [0,40, 80,120, 160, 200, 240,280, 320, 360, 400, 440, 480,520]
BLACK_KEY_X_COORDINATES = [30,70,150,190, 230, 310, 350, 430,470, 510]

The preceding x coordinate numbers have been obtained simply by trial and error as to emulate their location on a keyboard.

Then we modify the previously defined build_keyboard_frame method as follows:

 def build_keyboard_frame(self):
self.keyboard_frame = Frame(self.root, width=WINDOW_WIDTH,
height=KEYBOARD_HEIGHT, background='LavenderBlush2')
self.keyboard_frame.grid_propagate(False)
self.keyboard_frame.grid(row=4, column=0, sticky="nsew")
for index, key in enumerate(WHITE_KEY_NAMES):
x = WHITE_KEY_X_COORDINATES[index]
self.create_key(WHITE_KEY_IMAGE, key, x )
for index, key in enumerate(BLACK_KEY_NAMES):
x = BLACK_KEY_X_COORDINATES[index]
self.create_key(BLACK_KEY_IMAGE, key, x)

The first three lines of the previous method remain as defined in the previous iteration. We then go through all white and black keys creating labels for them at given x coordinates.

That concludes the iteration. If you now run  7.02 view.py, you should see a two-octave keyboard. When you press any key, the key's image should change to blue and it should print the name of the key pressed or released in the Terminal:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.215.96