Build a US Lottery Number Generator with Python and Tkinter

Published: (February 15, 2026 at 12:04 AM EST)
6 min read
Source: Dev.to

Source: Dev.to

🎲 Lottery Number Generator – Desktop App (Python + Tkinter)

📦 Step 1 – Import Required Libraries

import sys
import os
import random
import tkinter as tk
from tkinter import messagebox
ModulePurpose
tkinterGUI creation
randomGenerate lottery numbers
messageboxShow pop‑ups (e.g., About dialog)
sys, osSystem‑level utilities (optional)

🎨 Step 2 – Define the App Theme

# ── Colours ───────────────────────────────────────
APP_BG      = "#121212"   # whole‑app background
PANEL_BG    = "#1F1F1F"   # sidebar panel background
BTN_BG      = "#2C2C2C"   # default button background
ACCENT      = "#FF6F61"   # highlighted elements (e.g., selected lottery)
TEXT_CLR    = "#E0E0E0"   # primary text colour
SUBTEXT_CLR = "#AAAAAA"   # secondary text colour
BALL_BG     = "#333333"   # generic ball background
BALL_FG     = "#FFFFFF"   # generic ball foreground

# ── Font ─────────────────────────────────────────
FONT = ("Segoe UI", 11)

Feel free to tweak any colour codes or the font to match your own style.

🗂️ Step 3 – Lottery Presets

LOTTERY_PRESETS = {
    "Powerball": {
        "main_numbers": (1, 69),   # range for the 5 white balls
        "main_count": 5,
        "special_numbers": (1, 26),# range for the Powerball (red)
        "special_count": 1,
    },
    "Mega Millions": {
        "main_numbers": (1, 70),   # range for the 5 white balls
        "main_count": 5,
        "special_numbers": (1, 25),# range for the Mega Ball (gold)
        "special_count": 1,
    },
}
  • main_numbers → normal (white) numbers
  • special_numbers → Powerball / Mega Ball
  • main_count & special_count → how many numbers to draw

🏗️ Step 4 – Main App Class

class LotteryGeneratorApp:
    def __init__(self, root):
        self.root = root
        root.title("MateTools – Lottery Number Generator US Standard")
        root.geometry("1000x520")
        root.configure(bg=APP_BG)
        root.resizable(False, False)

        # -----------------------------------------------------------------
        # Build UI (left panel, right panel, etc.) – see the following steps
        # -----------------------------------------------------------------
  • root.title – window title
  • root.geometry – fixed size (1000 × 520)
  • root.resizable(False, False) – disables resizing

📐 Step 5 – Build the Left Panel (Sidebar)

# ----- Container -------------------------------------------------
left = tk.Frame(root, bg=PANEL_BG, width=420)
left.pack(side="left", fill="y")

# ----- Header ----------------------------------------------------
header = tk.Frame(left, bg=PANEL_BG)
header.pack(fill="x", padx=16, pady=(18, 10))

tk.Label(
    header,
    text="MateTools",
    bg=PANEL_BG,
    fg=ACCENT,
    font=("Segoe UI", 20, "bold")
).pack(side="left")

Frame – generic container for grouping widgets
Label – displays static text

# ----- Separator -------------------------------------------------
tk.Frame(left, bg=ACCENT, height=2).pack(fill="x", padx=16, pady=(0, 14))

# ----- Title & Description ----------------------------------------
tk.Label(
    left,
    text="Lottery Number Generator",
    bg=PANEL_BG,
    fg=TEXT_CLR,
    font=("Segoe UI", 14, "bold")
).pack(anchor="w", padx=16, pady=(0, 2))

tk.Label(
    left,
    text="Generate random numbers for Powerball / Mega Millions",
    bg=PANEL_BG,
    fg=SUBTEXT_CLR,
    font=("Segoe UI", 10)
).pack(anchor="w", padx=16, pady=(0, 16))

🎛️ Step 6 – Lottery‑Selection Buttons

self.lottery_var = tk.StringVar(value="Powerball")
self.lottery_buttons = {}

btn_frame = tk.Frame(left, bg=PANEL_BG)
btn_frame.pack(fill="x", padx=16, pady=(0, 16))

for lottery in LOTTERY_PRESETS.keys():
    btn = tk.Button(
        btn_frame,
        text=lottery,
        command=lambda l=lottery: self.select_lottery(l),
        bg=BTN_BG if lottery != "Powerball" else ACCENT,
        fg="white" if lottery == "Powerball" else TEXT_CLR,
        font=FONT,
        relief="flat",
        height=2,
        width=18
    )
    btn.pack(side="left", expand=True, padx=4)
    self.lottery_buttons[lottery] = btn
  • StringVar – tracks which lottery is currently selected.
  • lambda l=lottery – captures the current loop value for the callback.

🔘 Step 7 – “Generate” & “About” Buttons

btn_frame2 = tk.Frame(left, bg=PANEL_BG)
btn_frame2.pack(fill="x", padx=16, pady=16)

def make_btn(text, cmd, color=BTN_BG):
    return tk.Button(
        btn_frame2,
        text=text,
        command=cmd,
        bg=color,
        fg="white",
        font=("Segoe UI", 11, "bold"),
        relief="flat",
        height=2,
        width=18
    )

make_btn("Generate Numbers", self.generate_numbers).pack(side="left", expand=True, padx=4)
make_btn("About", self.show_about, ACCENT).pack(side="left", expand=True, padx=4)

make_btn – tiny helper to avoid repeating button‑creation code.

📊 Step 8 – Right Panel (Results Area)

right = tk.Frame(root, bg=APP_BG)
right.pack(side="right", fill="both", expand=True)

self.stats_card = tk.Frame(right, bg=PANEL_BG)
self.stats_card.pack(padx=30, pady=40, fill="both", expand=True)

tk.Label(
    self.stats_card,
    text="Generated Numbers",
    bg=PANEL_BG,
    fg=TEXT_CLR,
    font=("Segoe UI", 14, "bold")
).pack(pady=(20, 10))

self.numbers_frame = tk.Frame(self.stats_card, bg=PANEL_BG)
self.numbers_frame.pack(pady=20)

numbers_frame – will hold the coloured “ball” labels.

🎲 Step 9 – Lottery‑Number Generation Logic

def generate_numbers(self):
    # ---- Clear any previous results ------------------------------
    for widget in self.numbers_frame.winfo_children():
        widget.destroy()

    # ---- Determine which lottery is active -----------------------
    lottery = self.lottery_var.get()
    preset = LOTTERY_PRESETS[lottery]

    # ---- Draw main numbers (unique) -------------------------------
    main = random.sample(
        range(preset["main_numbers"][0], preset["main_numbers"][1] + 1),
        preset["main_count"]
    )
    # ---- Draw special number(s) ----------------------------------
    special = random.sample(
        range(preset["special_numbers"][0], preset["special_numbers"][1] + 1),
        preset["special_count"]
    )

    main.sort()          # optional: show the white balls in order
  • random.sample guarantees unique numbers.
  • Sorting the main numbers mimics the way official tickets are printed.

🔴 Step 10 – Show Numbers as Colourful Balls

# ---- White (main) balls -----------------------------------------
for num in main:
    ball = tk.Label(
        self.numbers_frame,
        text=str(num),
        bg="#1E90FF",          # bright blue for white balls
        fg="white",
        font=("Segoe UI", 16, "bold"),
        width=4,
        height=2,
        relief="raised",
        bd=2
    )
    ball.pack(side="left", padx=5)

# ---- Special (Powerball / Mega Ball) ball -----------------------
for num in special:
    ball = tk.Label(
        self.numbers_frame,
        text=str(num),
        bg="#FF4500",          # orange‑red for the Powerball / gold for Mega Ball
        fg="white",
        font=("Segoe UI", 16, "bold"),
        width=4,
        height=2,
        relief="raised",
        bd=2
    )
    ball.pack(side="left", padx=5)

Note: The snippet above ends abruptly in the original text. The full implementation would continue with any additional UI updates (e.g., a “copy to clipboard” button) and the select_lottery, show_about, and the main‑loop starter:

def select_lottery(self, name):
    self.lottery_var.set(name)
    # Update button colours to reflect the selection
    for l, btn in self.lottery_buttons.items():
        if l == name:
            btn.configure(bg=ACCENT, fg="white")
        else:
            btn.configure(bg=BTN_BG, fg=TEXT_CLR)

def show_about(self):
    messagebox.showinfo(
        "About MateTools",
        "Lottery Number Generator\n"
        "Author: Your Name\n"
        "Generate random Powerball / Mega Millions numbers."
    )

if __name__ == "__main__":
    root = tk.Tk()
    app = LotteryGeneratorApp(root)
    root.mainloop()

✅ Finished!

Run the script, pick Powerball or Mega Millions, hit Generate Numbers, and watch the colourful balls appear. The About button displays a simple info dialog.

Ideas for Extension

  • Add a “Copy to clipboard” feature.
  • Store the last‑generated set in a file.
  • Expand the UI with more lotteries (e.g., EuroJackpot, Lotto USA).

Happy coding! 🎉

Code Snippets

# Example widget configuration
eight = 2
relief = "raised"
bd = 2

ball.pack(side="left", padx=5)

Color Legend

  • Blue balls → main numbers
  • Orange balls → special numbers (Powerball / Mega Ball)

Step 11: Add About Popup

def show_about(self):
    messagebox.showinfo(
        "About",
        "MateTools – Lottery Number Generator\n\n"
        "• Random number generation\n"
        "• Presets for Powerball and Mega Millions\n\n"
        "Built by MateTools"
    )

Step 12: Run the App

if __name__ == "__main__":
    root = tk.Tk()
    LotteryGeneratorApp(root)
    root.mainloop()
  • root.mainloop() → starts the Tkinter GUI loop

And that’s it! You now have a fully functional US lottery number generator with a modern, beginner‑friendly interface.

0 views
Back to Blog

Related posts

Read more »

Python OOP for Java Developers

Converting a Simple Java Square Class to Python Below is a step‑by‑step conversion of a basic Square class written in Java into an equivalent Python implementa...