Build GUI Application for Guess Indian State using Tkinter Python
In this article, you will learn to make a GUI game in Python, in which we will be shown a map of Indian states and an input prompt were in the prompt, the user has to enter a state name of India. If the user guessed it correctly, the name would hover over its location on the map. You can also see the score as the title of the input prompt window.
- Python3 – Python is a high-level, general-purpose, and very popular programming language. It is being used in web development, Machine Learning applications, e.t.c.
- Turtle – “Turtle” is a Python feature like a drawing board, which lets us command a turtle to draw all over it
- Pandas – Pandas package offers various data structures and operations for manipulating numerical data and time series.
- A blank political map of India.
- A CSV sheet that will hold data of coordinates.
Step 1: The initial setup
The first step is to import our libraries and create the basic setup, in which a window pop up and we have the map of India covering the screen. We also have a title for our window and a turtle through which we draw or write on the screen. Currently, the turtle doesn’t have to do anything, but it needs to be hidden. The height and width were given according to the size of the map. If the user would like to choose some other map, set the length and width accordingly. Also note that in order to work with images in Turtle, the image must be in GIF format.
Step 2: Creating the data for coordinates of our States.
In this step, we will locate our states once the name is entered. Also, we need coordinates of the states for that, we will use the following code, which prints the x and y coordinates of the point where we click. Remember that the center of the window is the origin of our coordinate system. When you get the coordinates of all states of India printed on the console, copy them and paste them into an excel or google sheet against their respective state names and save it as a CSV file. After saving the file you can comment on the following code.
You can see how it looks when you click on the screen. Now click on points where you would want your state name to be displayed after its name is entered.
Step 3: Get the Data from CSV.
The next step is to create a list of state names that we can use in our program. We read the complete data from the CSV file using the Pandas library and its method, and then extract the values from the ‘State’ column.
Step 4: Game Play
Here because there are 29 states in India, we shall repeat the loop 29 times. Because the total of our correct guesses list is zero at first, the loop will continue to execute until the list’s length is less than 29. The first step is to create an input prompt to ask the user for his response. He will enter the state name if he wants to play; else, he will enter “Exit” to exit. Convert the user’s answer to the title case, just as all of our state names are, because the user may have entered the answer in a different case. We don’t want to inform him that his answer was incorrect because his situation differed from ours. If the user chooses not to play, the loop is broken after generating a CSV file from the Data Frame with the names of states that the user failed to predict. Then we check to see if the user’s response is correct. We retrieve the state’s data from our data set using that answer, which is the x and y coordinates.
Step 5: Game Result
When the loop terminates, we want to display the user’s score and performance. Based on his score, we determine whether his performance is good, average, or poor. We clear the entire window to make space for the result in a new blank window. We put the result in the center,