DEV Community

Mate Technologies
Mate Technologies

Posted on

πŸ§‘β€πŸ’» Build a Random Name Picker App with Python & Tkinter (Step-by-Step)

In this tutorial, we’ll build a desktop Random Name Picker app using Python and Tkinter.

The app will let users:

Add names manually

Load names from a file

Pick one or multiple random winners

Remove winners automatically

Save names back to a file

No advanced Python knowledge required β€” we’ll build it piece by piece.

πŸ› οΈ Prerequisites

You’ll need:

Python 3 installed

Basic Python knowledge (functions, lists)

Tkinter (comes bundled with Python)

To check Python:

python --version

πŸ“¦ Step 1: Import Required Modules

Let’s start by importing everything we need.

import tkinter as tk
from tkinter import filedialog, messagebox
import random
import os
Enter fullscreen mode Exit fullscreen mode

What each module does:

tkinter β†’ builds the GUI

filedialog β†’ opens file picker dialogs

messagebox β†’ shows alerts and popups

random β†’ selects random names

os β†’ helps with file paths (optional but useful)

🧱 Step 2: Create the Main Application Class

We’ll organize the app using a class, which keeps everything clean and manageable.

class RandomNamePickerApp:
    def __init__(self, root):
        self.root = root
Enter fullscreen mode Exit fullscreen mode

Here, root is the main Tkinter window passed into our app.

🎨 Step 3: Configure the Main Window

Now let’s style the window.

self.root.title("Random Name Picker")
self.root.geometry("700x750")
self.root.configure(bg="#1f2f3a")
self.root.resizable(False, False)
Enter fullscreen mode Exit fullscreen mode

What this does:

Sets the window title

Fixes the window size

Applies a dark background

Prevents resizing

🧠 Step 4: App State (Data Storage)

We need a place to store names and file paths.

self.names = []
self.file_path = None
Enter fullscreen mode Exit fullscreen mode

self.names β†’ list of all entered names

self.file_path β†’ remembers where the file is saved

🧩 Step 5: Create the UI Layout

We’ll separate UI creation into its own method.

self.create_ui()
Enter fullscreen mode Exit fullscreen mode

This keeps the init method clean and readable.

πŸ–ŒοΈ Step 6: App Title Label

Inside create_ui():

tk.Label(
    self.root,
    text="Random Name Picker",
    font=("Helvetica", 28, "bold"),
    bg="#1f2f3a",
    fg="#ffffff"
).pack(pady=20)
Enter fullscreen mode Exit fullscreen mode

This creates the big title at the top of the app.

✍️ Step 7: Name Input Field

We’ll place the input inside a frame.

input_frame = tk.Frame(self.root, bg="#1f2f3a")
input_frame.pack(pady=10, fill="x", padx=20)
Enter fullscreen mode Exit fullscreen mode

Label + Entry:

tk.Label(
    input_frame,
    text="Enter Name:",
    font=("Helvetica", 12),
    bg="#1f2f3a",
    fg="#bdc3c7"
).grid(row=0, column=0, sticky="w")

self.name_entry = tk.Entry(
    input_frame,
    font=("Helvetica", 12),
    width=30
)
self.name_entry.grid(row=0, column=1, padx=5)
Enter fullscreen mode Exit fullscreen mode

βž• Step 8: Add Name Button

tk.Button(
    input_frame,
    text="Add Name",
    command=self.add_name,
    bg="#2980b9",
    fg="white",
    font=("Helvetica", 12, "bold")
).grid(row=0, column=2, padx=5)
Enter fullscreen mode Exit fullscreen mode

Clicking this button triggers the add_name() method.

πŸ“‹ Step 9: Display Names with a Listbox

self.listbox = tk.Listbox(
    self.root,
    font=("Helvetica", 12),
    bg="#2c3e50",
    fg="white"
)
self.listbox.pack(fill="both", expand=True, padx=20)
Enter fullscreen mode Exit fullscreen mode

This shows all entered names in a scrollable list.

🎯 Step 10: Add Core Buttons

Buttons for picking, removing, and clearing names:

tk.Button(
    self.root,
    text="Pick Random Name",
    command=self.pick_random_name
).pack(pady=5)

tk.Button(
    self.root,
    text="Pick Multiple Names",
    command=self.pick_multiple_names
).pack(pady=5)
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Step 11: Add Name Logic

def add_name(self):
    name = self.name_entry.get().strip()
    if name:
        self.names.append(name)
        self.update_listbox()
        self.name_entry.delete(0, tk.END)
    else:
        messagebox.showerror("Error", "Please enter a name.")
Enter fullscreen mode Exit fullscreen mode

What happens:

Reads input

Adds name to list

Refreshes the UI

Clears the input field

πŸ” Step 12: Refresh the Listbox

def update_listbox(self):
    self.listbox.delete(0, tk.END)
    for name in self.names:
        self.listbox.insert(tk.END, name)
Enter fullscreen mode Exit fullscreen mode

This keeps the UI in sync with the data.

🎲 Step 13: Pick a Random Name

def pick_random_name(self):
    if not self.names:
        messagebox.showerror("Error", "No names to pick from!")
        return

    winner = random.choice(self.names)
    messagebox.showinfo("Winner", f"The selected name is:\n\n{winner}")

Enter fullscreen mode Exit fullscreen mode

Uses random.choice() to pick a winner.

πŸ† Step 14: Pick Multiple Winners

winners = random.sample(self.names, n)
Enter fullscreen mode Exit fullscreen mode

random.sample() ensures no duplicates

Perfect for giveaways or raffles

πŸ—‘οΈ Step 15: Remove & Clear Names

Remove selected names:

def remove_selected(self):
    selected = self.listbox.curselection()
    for index in reversed(selected):
        del self.names[index]
    self.update_listbox()
Enter fullscreen mode Exit fullscreen mode

Clear everything:

def clear_all(self):
    self.names = []
    self.update_listbox()
Enter fullscreen mode Exit fullscreen mode

πŸ’Ύ Step 16: Load Names from a File

path = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt")])
Enter fullscreen mode Exit fullscreen mode

Reads names line-by-line from a .txt file and adds them to the list.

πŸ’½ Step 17: Save Names to a File

with open(save_path, "w", encoding="utf-8") as f:
    for name in self.names:
        f.write(name + "\n")
Enter fullscreen mode Exit fullscreen mode

Each name is saved on a new line.

▢️ Step 18: Run the App

Finally, start the application:

if __name__ == "__main__":
    root = tk.Tk()
    app = RandomNamePickerApp(root)
    root.mainloop()
Enter fullscreen mode Exit fullscreen mode

This launches the GUI window.

πŸŽ‰ Final Thoughts

You’ve just built a fully functional Tkinter desktop app with:

Clean UI

Random selection logic

File handling

Beginner-friendly structure

πŸš€ Ideas to Extend It

Export winners to a file

Add themes (light/dark mode)

Add sound effects

Convert it into an .exe

Random Name Picker

Top comments (0)