Full Code:
Output of the App:
Hi Guys,
In this tutorial I will teach you to create a Random Name Choosing App with python by the help of tkinter module and random module.
So it's basically a beginner project. Get ready to Smash your Keyboards for some code.
1. First we will import modules required for this project, in this project we will use tkinter(for GUI(Graphical User Interface)) and random module for choosing random name.
Code:
2. Next we will create an app object with the help of tkinter. Tkinter uses some function like Tk() to create an app.
Code:
My app object name is root.
3. Next we will create a list to store the usernames.
Code:
4. Next we will create an entry with the help of Entry function in Tkinter. We will receive the user inputs through entry fields.
It just looks like this:
Code to create entry:
- pack function is used to display the objects on GUI.
5. Next step is to create a function which will store the input of user in the names lists.
Code:
- We are storing the user input in the "user_input" variable. We are using the entry.get() method to obtain the user input.
- Next, we are checking that user input is blank or the user is giving the same name more than 1 time.
- If all of the conditions are false, we will append the user_input in the names list using append function.
7. Next Step is an interesting step where you will be creating a function to pick a random name from the list and display it in the GUI.
Code:
- In the first line of the function, we are specifying the script to pick a random name from names list
- Next, we are creating a label variable where we will use tkinter.Label function to display the randomly picked name.
- Then pack the label to display it in GUI.
8. Now we are going to create a function which will help the user to check what are the names are in the list.
Code:
- We use "for loop" to display the names in order.
- label_namelist is a tkinter label which will display the names.
- Last, we will use pack method for label_namelist to show in GUI.
9. Next, we will be creating the Respective buttons for the respective functions to work. It will be easier to create them with tkinter.Button method.
Code:
Command parameter should be changed for every button to the respective function names which will be called when the button is pressed.
10. Last step is to pack all the buttons and run the root app object in a mainloop.
Code:
Output of the App:
And that concludes the creation of an app that selects a random name from a list. Tkinter is versatile, and this is just one of its many uses. Look forward to more tkinter tutorials in the future. Stay tuned. Farewell, everyone.