10 Steps to create a Random Name Choosing App in Python (TUTORIAL).

Full Code:

import tkinter as tk
import random

root = tk.Tk()

names = []

entry = tk.Entry(root, bg="orange", width=50)
entry.pack()

def add_name_to_list():
    user_input = entry.get()
    if(user_input == " "):
        label_space_error = tk.Label(root, text="Dont Leave a the above space Empty...")
        label_space_error.pack()

    if(user_input in names):
        label_error = tk.Label(root, text="Already name Exists...")
        label_error.pack()
    else:
        names.append(user_input)

def pick_random_name():
    name = random.choice(names)
    label = tk.Label(root, text="The Randomly Picked Name is: " + name)
    label.pack()

def get_crrent_name_list():
    for i in range(0, len(names) + 1):
        label_namelist = tk.Label(root, text= str(i + 1) + " . " + names[i])
        label_namelist.pack()

RandomNameButton = tk.Button(root, text='Pick a Random Name', width=50, height=5, command=pick_random_name, bg="black", fg="white")
AddNameButton = tk.Button(root, text='Add Name', width=50, height=5, command=add_name_to_list, bg="Silver", fg="black")
CurrentNameListButton = tk.Button(root, text='Current name List', width=50, height=5, command=get_crrent_name_list, bg="Gold", fg="red")

RandomNameButton.pack()
AddNameButton.pack()
CurrentNameListButton.pack()

root.mainloop()

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:

import tkinter as tk
import random

2. Next we will create an app object with the help of tkinter. Tkinter uses some function like Tk() to create an app.

Code: 

root = tk.Tk()

My app object name is root.

3. Next we will create a list to store the usernames.

Code:

names = []

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:

entry = tk.Entry(root, bg="orange", width=50)
entry.pack()

  • 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:

def add_name_to_list():
    user_input = entry.get()
    if(user_input == " "):
        label_space_error = tk.Label(root, text="Dont Leave a the above space Empty...")
        label_space_error.pack()

    if(user_input in names):
        label_error = tk.Label(root, text="Already name Exists...")
        label_error.pack()
    else:
        names.append(user_input)

  • 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:

def pick_random_name():
    name = random.choice(names)
    label = tk.Label(root, text="The Randomly Picked Name is: " + name)
    label.pack()

  • 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:

def get_crrent_name_list():
    for i in range(0, len(names) + 1):
        label_namelist = tk.Label(root, text= str(i + 1) + " . " + names[i])
        label_namelist.pack()

  • 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: 

RandomNameButton = tk.Button(root, text='Pick a Random Name', width=50, height=5, command=pick_random_name, bg="black", fg="white")
AddNameButton = tk.Button(root, text='Add Name', width=50, height=5, command=add_name_to_list, bg="Silver", fg="black")
CurrentNameListButton = tk.Button(root, text='Current name List', width=50, height=5, command=get_crrent_name_list, bg="Gold", fg="red")

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:

RandomNameButton.pack()
AddNameButton.pack()
CurrentNameListButton.pack()

root.mainloop()

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.