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
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
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)
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
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()
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)
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)
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)
โ 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)
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)
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)
๐ 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.")
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)
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}")
Uses random.choice() to pick a winner.
๐ Step 14: Pick Multiple Winners
winners = random.sample(self.names, n)
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()
Clear everything:
def clear_all(self):
self.names = []
self.update_listbox()
๐พ Step 16: Load Names from a File
path = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt")])
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")
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()
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

Top comments (0)