In this tutorial, we’ll build a Random Lottery Number Generator with a clean, modern GUI using Python’s Tkinter library. By the end, you’ll have a fully functional desktop app where users can generate unique random numbers.
Step 1: Import the Necessary Libraries
We start by importing the Python modules we need:
import sys
import os
import tkinter as tk
from tkinter import messagebox
import random
Explanation:
sys & os → Help us locate files and work with file paths (useful if we package the app).
tkinter → Provides the GUI framework.
messagebox → To show popup messages (errors, info).
random → To generate random lottery numbers.
Step 2: Set Up App Colors and Fonts
We can define a theme for the app so it looks modern and consistent.
APP_BG = "#121212"
PANEL_BG = "#1F1F1F"
BTN_BG = "#2C2C2C"
ACCENT = "#FF6F61"
TEXT_CLR = "#E0E0E0"
SUBTEXT_CLR = "#AAAAAA"
FONT = ("Segoe UI", 11)
Explanation:
These variables store the colors for backgrounds, text, buttons, and accents.
FONT defines the default font for our labels and buttons.
Using variables makes it easy to change the app’s theme later.
Step 3: Define a Function for Resource Paths
This is useful if you package your app with tools like PyInstaller:
def resource_path(file_name):
base_path = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, file_name)
Explanation:
sys._MEIPASS is a temporary folder PyInstaller creates for bundled files.
This function ensures your app can find resources (like icons or images) whether it’s run as a script or an executable.
Step 4: Create the Lottery Logic
This function generates unique random numbers:
def generate_lottery(min_num, max_num, count):
if count > (max_num - min_num + 1):
raise ValueError("Count exceeds available unique numbers.")
return sorted(random.sample(range(min_num, max_num + 1), count))
Explanation:
random.sample ensures numbers are unique.
sorted arranges numbers in ascending order.
We check if count is larger than the available range to avoid errors.
Step 5: Build the Main App Class
We encapsulate the GUI logic inside a class:
class LotteryGenerator:
def __init__(self, root):
self.root = root
root.title("MateTools – Random Lottery Generator")
root.geometry("1000x520")
root.configure(bg=APP_BG)
root.resizable(False, False)
Explanation:
root.title → Sets the window title.
root.geometry → Defines the window size.
root.configure(bg=APP_BG) → Sets the background color.
root.resizable(False, False) → Prevents the user from resizing the window.
Step 6: Create the Left Panel
The left panel will contain the inputs and buttons.
left = tk.Frame(root, bg=PANEL_BG, width=420)
left.pack(side="left", fill="y")
Explanation:
A Frame is a container for widgets.
side="left" → Places it on the left.
fill="y" → Makes it stretch vertically.
Step 7: Add a Header
We’ll give the app a title and separator line.
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")
tk.Frame(left, bg=ACCENT, height=2).pack(fill="x", padx=16, pady=(0, 14))
Explanation:
Label → Displays text.
Frame with height=2 → Acts as a horizontal line.
padx & pady → Adds spacing around widgets.
Step 8: Add Input Fields
Users can enter the minimum number, maximum number, and how many numbers to draw.
def labeled_entry(label):
tk.Label(
left,
text=label,
bg=PANEL_BG,
fg=TEXT_CLR,
font=("Segoe UI", 11, "bold")
).pack(anchor="w", padx=16, pady=(10, 4))
e = tk.Entry(
left,
bg=BTN_BG,
fg="white",
font=FONT,
relief="flat"
)
e.pack(fill="x", padx=16)
return e
self.min_entry = labeled_entry("Minimum Number")
self.min_entry.insert(0, "1")
self.max_entry = labeled_entry("Maximum Number")
self.max_entry.insert(0, "49")
self.count_entry = labeled_entry("Numbers to Draw")
self.count_entry.insert(0, "6")
Explanation:
labeled_entry is a helper function to avoid repeating code.
.insert(0, "1") sets a default value.
Each input field is an Entry widget for user input.
Step 9: Add Buttons
We need Generate and About buttons.
btn_frame = tk.Frame(left, bg=PANEL_BG)
btn_frame.pack(fill="x", padx=16, pady=20)
def make_btn(text, cmd, color=BTN_BG):
return tk.Button(
btn_frame,
text=text,
command=cmd,
bg=color,
fg="white",
font=("Segoe UI", 11, "bold"),
relief="flat",
height=2,
width=18
)
make_btn("Generate", self.generate).pack(side="left", expand=True, padx=4)
make_btn("About", self.show_about, ACCENT).pack(side="left", expand=True, padx=4)
Explanation:
make_btn is a helper function to create styled buttons.
command → The function called when the button is clicked.
expand=True → Makes buttons grow evenly in the frame.
Step 10: Right Panel & Result Display
The right panel shows generated numbers.
right = tk.Frame(root, bg=APP_BG)
right.pack(side="right", fill="both", expand=True)
stats_card = tk.Frame(right, bg=PANEL_BG)
stats_card.pack(padx=30, pady=40, fill="both", expand=True)
tk.Label(
stats_card,
text="Generated Numbers",
bg=PANEL_BG,
fg=TEXT_CLR,
font=("Segoe UI", 14, "bold")
).pack(pady=(20, 10))
self.result_label = tk.Label(
stats_card,
text="—",
bg=PANEL_BG,
fg="white",
font=("Segoe UI", 22, "bold")
)
self.result_label.pack(pady=30)
Explanation:
result_label will dynamically update with the generated numbers.
Using a separate panel makes the UI cleaner and organized.
Step 11: Add Functions to Generate Numbers & Show About
def generate(self):
try:
min_n = int(self.min_entry.get())
max_n = int(self.max_entry.get())
count = int(self.count_entry.get())
numbers = generate_lottery(min_n, max_n, count)
self.result_label.config(text=" ".join(map(str, numbers)))
except Exception as e:
messagebox.showerror("Error", str(e))
def show_about(self):
messagebox.showinfo(
"About",
"MateTools – Random Lottery Generator\n\n"
"• Unique random numbers\n"
"• Custom ranges & counts\n"
"• Clean, modern UI\n\n"
"Built by MateTools"
)
Explanation:
generate → Reads inputs, generates numbers, updates result_label.
show_about → Displays a popup with information about the app.
try/except → Handles invalid inputs gracefully.
Step 12: Run the App
Finally, we run the Tkinter main loop:
if __name__ == "__main__":
root = tk.Tk()
LotteryGenerator(root)
root.mainloop()
Explanation:
Tk() → Creates the main window.
LotteryGenerator(root) → Initializes our app.
root.mainloop() → Starts the event loop to make the GUI interactive.
✅ Congratulations! You now have a fully functional Random Lottery Generator with a clean, modern Tkinter interface.

Top comments (0)