Color game in Python using Tkinter

Color game in Python using Tkinter

Color game in Python using Tkinter

In this Blog Post, we will study “Color game in Python using Tkinter”.

For developing a GUI Application in python Tkinter library can be used.

Tkinter library is very easy and simple and popular also for GUI development.

We can develop a game in which we can enter a color that will appear on a screen and then it will increase by one.

The time given for this game is 30 seconds. And the colors used in the game Red, Blue, Green, Pink, Black,

Yellow, Orange, White, Purple, and Brown.

In this game, the player wants to enter the color name.


# import the modules
import tkinter
import random

# list of possible colour.
My_colours = ['Red', 'Blue', 'Green', 'Pink', 'Black',
           'Yellow', 'Orange', 'White', 'Purple', 'Brown']
score = 0

# the game time will left, initially it is 30 seconds.
timeleft = 30

# function that will be start the game.
def startGame(event):
    if timeleft == 30:
        # start the countdown timer.

    # it will run the function to
    # choose the next colour.

# Function to choose and
# display the next colour.
def nextColour():
    # use the globally declared 'score'
    # and 'play' variables above.
    global score
    global timeleft

    # if a game is currently in play
    if timeleft > 0:

        # make the text entry box active.

        # if the colour typed is equal
        # to the colour of the text
        if e.get().lower() == colours[1].lower():
            score += 1

        # clear the text entry box.
        e.delete(0, tkinter.END)


        # To change the Type of colour, by changing the
        # The text _and_ the colour to the a random colour value
        label.config(fg=str(colours[1]), text=str(colours[0]))

        # update the score.
        scoreLabel.config(text="Score: " + str(score))

    # Countdown timer function

def countdown():
    global timeleft

    # if a game is in play
    if timeleft > 0:
        # decrement the timer.
        timeleft -= 1

        # update the time left label
        timeLabel.config(text="Time left: "
                              + str(timeleft))

        # run the function again after 1 second.
        timeLabel.after(1000, countdown)

    # Driver Code

# create a GUI window
root = tkinter.Tk()

# set the title

# set the size

# add an instructions label
instructions = tkinter.Label(root, text="Type in the colour"
                                        "of the words, and not the word text!",
                             font=('Helvetica', 12))

# add a score label
scoreLabel = tkinter.Label(root, text="Press enter to start",
                           font=('Helvetica', 12))

# add a time left label
timeLabel = tkinter.Label(root, text="Time left: " +
                                     str(timeleft), font=('Helvetica', 12))


# add a label for displaying the colours
label = tkinter.Label(root, font=('Helvetica', 60))

# add a text entry box for
# typing in colours
e = tkinter.Entry(root)

# run the 'startGame' function
# when the enter key is pressed
root.bind('<Return>', startGame)

# set focus on the entry box

# start the GUI


Color game in Python using Tkinter


Also Read This:

Calculate Internet Speed in Python

Get Corona Virus Cases Using Python

NumPy in Python

Python with MySQL


Hey Friends, Welcome to thetechbuddy. This blog is about technical or programming knowledge.

Leave a Reply