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)