Changing the drawing color

Now we’re going to add a function to change the color of the pen as we use the buttons along the side, and link that function up to the buttons on our screen. Let’s dive in.

Somewhere in your code, define a function called setColor, that takes a string representing the color of the pen

def setColor(color):

Inside of this function, declare a global variable named “pencolor” and set it equal to the color we passed

def setColor(color):
    global penColor
    penColor = color

Fortunately, that’s all for making the function itself, but now we need to attach this function to each button with their respective colors. We will use the Button’s config() function, and set the “command” attribute as follows.

Somewhere in your code, somewhere after you define your “red” button, code the following. You may be wondering about the “lambda: ” keyword, what it essentially does is it bundles our “setcolor” function with the parameter “red”, so it can be assigned to the command attribute.

red.config(command = lambda: setColor("red"))

Now that we have the “red” button coded, move on to coding