DEV Community

Mate Technologies
Mate Technologies

Posted on

Build a US Lottery Number Generator with Python and Tkinter

In this tutorial, we’ll create a desktop app that generates random numbers for Powerball and Mega Millions. We’ll use Python and Tkinter for the GUI.

By the end, you’ll have a sleek, interactive app with colored “number balls.”

Step 1: Import Necessary Libraries

We'll need Python’s built-in libraries for GUI (tkinter), random number generation, and system operations.

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

tkinter → for the GUI

random → to generate lottery numbers

messagebox → to show popups like the "About" info

Step 2: Define App Theme

Let’s make the app look modern by defining colors and fonts.

THEME

APP_BG = "#121212"
PANEL_BG = "#1F1F1F"
BTN_BG = "#2C2C2C"
ACCENT = "#FF6F61"
TEXT_CLR = "#E0E0E0"
SUBTEXT_CLR = "#AAAAAA"
BALL_BG = "#333333"
BALL_FG = "#FFFFFF"

FONT = ("Segoe UI", 11)
Enter fullscreen mode Exit fullscreen mode

APP_BG → background for the whole app

PANEL_BG → sidebar panel background

ACCENT → color for highlighted elements

This keeps your UI consistent and easy to tweak later.

Step 3: Define Lottery Presets

We’ll support Powerball and Mega Millions, including their number ranges.

LOTTERY CONFIG

LOTTERY_PRESETS = {
    "Powerball": {"main_numbers": (1, 69), "main_count": 5, "special_numbers": (1, 26), "special_count": 1},
    "Mega Millions": {"main_numbers": (1, 70), "main_count": 5, "special_numbers": (1, 25), "special_count": 1},
}
Enter fullscreen mode Exit fullscreen mode

main_numbers → normal numbers

special_numbers → Powerball or Mega Ball

main_count and special_count → how many numbers to draw

Step 4: Create the Main App Class

We’ll wrap the GUI in a class to keep it organized.

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)
Enter fullscreen mode Exit fullscreen mode

root.title → sets the window title

root.geometry → window size

root.resizable(False, False) → fixes the window size

Step 5: Build the Left Panel

This will contain the app title, description, and buttons.

LEFT PANEL

left = tk.Frame(root, bg=PANEL_BG, width=420)
left.pack(side="left", fill="y")
Enter fullscreen mode Exit fullscreen mode

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")

Enter fullscreen mode Exit fullscreen mode

Frame → container for organizing widgets

Label → shows text

We’ll also add a line separator and description:

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

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))
Enter fullscreen mode Exit fullscreen mode

Step 6: Add Lottery Selection Buttons

Users can choose which lottery to generate numbers for.

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
Enter fullscreen mode Exit fullscreen mode

StringVar → tracks the selected lottery

lambda l=lottery → passes lottery name to the handler

Step 7: Add 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)
Enter fullscreen mode Exit fullscreen mode

make_btn → reusable function to create buttons

Generate Numbers → triggers number generation

About → shows information about the app

Step 8: Build the Right Panel for Results

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)
Enter fullscreen mode Exit fullscreen mode

numbers_frame → container where lottery balls will appear

Step 9: Add Lottery Logic

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

    lottery = self.lottery_var.get()
    preset = LOTTERY_PRESETS[lottery]

    main = random.sample(range(preset["main_numbers"][0], preset["main_numbers"][1] + 1), preset["main_count"])
    special = random.sample(range(preset["special_numbers"][0], preset["special_numbers"][1] + 1), preset["special_count"])

    main.sort()
Enter fullscreen mode Exit fullscreen mode

random.sample → ensures unique numbers

main.sort() → orders main numbers

Step 10: Display Numbers as Balls

Display main numbers

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

Display special numbers

for num in special:
    ball = tk.Label(
        self.numbers_frame,
        text=str(num),
        bg="#FF4500",
        fg="white",
        font=("Segoe UI", 16, "bold"),
        width=4,
        height=2,
        relief="raised",
        bd=2
    )
    ball.pack(side="left", padx=5)
Enter fullscreen mode Exit fullscreen mode

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"
    )
Enter fullscreen mode Exit fullscreen mode

Step 12: Run the App

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

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.

Lottery Number Generator US Standard

Top comments (0)