O'Reilly logo

Tkinter GUI Application Development Blueprints - Second Edition by Bhaskar Chaudhary

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Making the piano keyboard

Let's now build the piano keyboard. All keys on the keyboard will be made using the Label widget. We will superimpose the label widget with an image of black and white keys using Tkinter's PhotoImage class.

The PhotoImage class is used to display images in label, text, button, and canvas widgets. We used it in Chapter 2, Making a Text Editor to add icons to buttons.  Since this class can only handle .gif or .bpm format images, we add four .gif images to a folder named pictures. These four images are black_key.gif, white_key.gif,  black_key_pressed.gif, and white_key_pressed.gif.

Since we will refer to these images over and over again,  we add their reference to 7.02 constants.py

WHITE_KEY_IMAGE = '../pictures/white_key.gif' ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required