Skip to content

Commit a8b9639

Browse files
authored
Create Random-Username-Generator.py
1 parent 5099855 commit a8b9639

1 file changed

Lines changed: 222 additions & 0 deletions

File tree

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
import os
2+
import json
3+
import random
4+
import string
5+
import tkinter as tk
6+
from tkinter import ttk, messagebox
7+
import sv_ttk
8+
from threading import Thread
9+
10+
# =========================
11+
# Helpers
12+
# =========================
13+
CONFIG_FILE = "usernames_data.json"
14+
15+
def load_usernames():
16+
if os.path.exists(CONFIG_FILE):
17+
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
18+
return json.load(f)
19+
return []
20+
21+
def save_usernames():
22+
with open(CONFIG_FILE, "w", encoding="utf-8") as f:
23+
json.dump(usernames_data, f, ensure_ascii=False, indent=4)
24+
25+
def set_status(msg):
26+
status_var.set(msg)
27+
root.update_idletasks()
28+
29+
def generate_username(length=8, style="Normal"):
30+
"""Generate username based on selected style."""
31+
if style == "Normal":
32+
chars = string.ascii_letters + string.digits
33+
return ''.join(random.choice(chars) for _ in range(length))
34+
elif style == "Lowercase":
35+
chars = string.ascii_lowercase + string.digits
36+
return ''.join(random.choice(chars) for _ in range(length))
37+
elif style == "Uppercase":
38+
chars = string.ascii_uppercase + string.digits
39+
return ''.join(random.choice(chars) for _ in range(length))
40+
elif style == "NumberEnd":
41+
chars = string.ascii_letters
42+
name = ''.join(random.choice(chars) for _ in range(length-1))
43+
return name + random.choice(string.digits)
44+
else:
45+
return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length))
46+
47+
# =========================
48+
# App Setup
49+
# =========================
50+
root = tk.Tk()
51+
root.title("🎲 Random Username Generator Pro")
52+
root.geometry("970x600")
53+
sv_ttk.set_theme("light")
54+
55+
# =========================
56+
# Globals
57+
# =========================
58+
usernames_data = load_usernames()
59+
filtered_usernames = usernames_data.copy()
60+
current_filter = tk.StringVar(value="")
61+
current_sort = tk.StringVar(value="Alphabetical")
62+
current_style = tk.StringVar(value="Normal")
63+
64+
# =========================
65+
# Username Functions
66+
# =========================
67+
def add_username_thread(length, count, style):
68+
def task():
69+
global usernames_data
70+
for _ in range(count):
71+
username = generate_username(length, style)
72+
usernames_data.append(username)
73+
save_usernames()
74+
apply_filter_sort()
75+
set_status(f"Generated {count} username(s).")
76+
Thread(target=task, daemon=True).start()
77+
78+
def generate_usernames():
79+
try:
80+
length = int(length_entry.get())
81+
count = int(count_entry.get())
82+
if length < 3 or length > 20:
83+
messagebox.showwarning("Invalid Length", "Length must be between 3 and 20.")
84+
return
85+
if count < 1 or count > 1000:
86+
messagebox.showwarning("Invalid Count", "Count must be between 1 and 1000.")
87+
return
88+
except ValueError:
89+
messagebox.showwarning("Invalid Input", "Please enter valid numbers.")
90+
return
91+
add_username_thread(length, count, current_style.get())
92+
93+
def clear_usernames():
94+
if messagebox.askyesno("Confirm", "Clear all saved usernames?"):
95+
usernames_data.clear()
96+
apply_filter_sort()
97+
save_usernames()
98+
set_status("All usernames cleared.")
99+
100+
def copy_selected(event=None):
101+
try:
102+
index = username_text.index("sel.first")
103+
end_index = username_text.index("sel.last")
104+
value = username_text.get(index, end_index)
105+
root.clipboard_clear()
106+
root.clipboard_append(value)
107+
set_status(f"Copied '{value}' to clipboard.")
108+
except tk.TclError:
109+
set_status("No selection to copy.")
110+
111+
def apply_filter_sort(*args):
112+
global filtered_usernames
113+
filter_text = current_filter.get().lower()
114+
if filter_text:
115+
filtered_usernames = [u for u in usernames_data if filter_text in u.lower()]
116+
else:
117+
filtered_usernames = usernames_data.copy()
118+
# Sort
119+
sort_option = current_sort.get()
120+
if sort_option == "Alphabetical":
121+
filtered_usernames.sort()
122+
elif sort_option == "Length (Short → Long)":
123+
filtered_usernames.sort(key=len)
124+
elif sort_option == "Length (Long → Short)":
125+
filtered_usernames.sort(key=len, reverse=True)
126+
update_username_text(filter_text)
127+
128+
def update_username_text(highlight_text=""):
129+
username_text.config(state=tk.NORMAL)
130+
username_text.delete("1.0", tk.END)
131+
for uname in filtered_usernames:
132+
start_index = username_text.index(tk.INSERT)
133+
username_text.insert(tk.END, uname + "\n")
134+
if highlight_text:
135+
# Highlight matching substrings
136+
idx = uname.lower().find(highlight_text)
137+
if idx != -1:
138+
start = f"{start_index}+{idx}c"
139+
end = f"{start_index}+{idx+len(highlight_text)}c"
140+
username_text.tag_add("highlight", start, end)
141+
username_text.tag_config("highlight", foreground="red", font=("Segoe UI", 12, "bold"))
142+
username_text.config(state=tk.DISABLED)
143+
total_var.set(f"Total Usernames: {len(filtered_usernames)}")
144+
145+
# =========================
146+
# GUI Setup
147+
# =========================
148+
main_frame = ttk.Frame(root, padding=20)
149+
main_frame.pack(expand=True, fill="both")
150+
151+
ttk.Label(main_frame, text="🎲 Random Username Generator Pro", font=("Segoe UI", 22, "bold")).pack(pady=(0,10))
152+
153+
# Inputs
154+
input_frame = ttk.LabelFrame(main_frame, text="Settings", padding=10)
155+
input_frame.pack(fill="x", pady=5)
156+
157+
ttk.Label(input_frame, text="Length:").grid(row=0, column=0, padx=5)
158+
length_entry = ttk.Entry(input_frame, width=10)
159+
length_entry.grid(row=0, column=1, padx=5)
160+
length_entry.insert(0, "8")
161+
162+
ttk.Label(input_frame, text="Count:").grid(row=0, column=2, padx=5)
163+
count_entry = ttk.Entry(input_frame, width=10)
164+
count_entry.grid(row=0, column=3, padx=5)
165+
count_entry.insert(0, "10")
166+
167+
ttk.Label(input_frame, text="Style:").grid(row=0, column=4, padx=5)
168+
style_combo = ttk.Combobox(input_frame, state="readonly", textvariable=current_style, width=20)
169+
style_combo['values'] = ["Normal", "Lowercase", "Uppercase", "NumberEnd"]
170+
style_combo.current(0)
171+
style_combo.grid(row=0, column=5, padx=5)
172+
173+
ttk.Button(input_frame, text="Generate", style="Generate.TButton", command=generate_usernames).grid(row=0, column=6, padx=5)
174+
ttk.Button(input_frame, text="Clear All", style="Clear.TButton", command=clear_usernames).grid(row=0, column=7, padx=5)
175+
ttk.Button(input_frame, text="Copy Selected", style="Copy.TButton", command=copy_selected).grid(row=0, column=8, padx=5)
176+
177+
ttk.Style().configure("Generate.TButton", foreground="black", background="#4CAF50")
178+
ttk.Style().configure("Clear.TButton", foreground="black", background="#FF9800")
179+
ttk.Style().configure("Copy.TButton", foreground="black", background="#2196F3")
180+
181+
# Filter & Sort
182+
filter_sort_frame = ttk.LabelFrame(main_frame, text="Filter & Sort", padding=10)
183+
filter_sort_frame.pack(fill="x", pady=5)
184+
185+
ttk.Label(filter_sort_frame, text="Filter:").grid(row=0, column=0, padx=5)
186+
filter_entry = ttk.Entry(filter_sort_frame, textvariable=current_filter)
187+
filter_entry.grid(row=0, column=1, padx=5)
188+
current_filter.trace_add("write", apply_filter_sort)
189+
190+
ttk.Label(filter_sort_frame, text="Sort by:").grid(row=0, column=2, padx=5)
191+
sort_combo = ttk.Combobox(filter_sort_frame, state="readonly", textvariable=current_sort, width=25)
192+
sort_combo['values'] = ["Alphabetical", "Length (Short → Long)", "Length (Long → Short)"]
193+
sort_combo.current(0)
194+
sort_combo.grid(row=0, column=3, padx=5)
195+
sort_combo.bind("<<ComboboxSelected>>", apply_filter_sort)
196+
197+
# Username display with highlighting
198+
text_frame = ttk.Frame(main_frame)
199+
text_frame.pack(expand=True, fill="both", pady=10)
200+
201+
username_text = tk.Text(text_frame, font=("Segoe UI", 12), state=tk.DISABLED, wrap="none", height=10)
202+
username_text.pack(side="left", expand=True, fill="both")
203+
204+
scrollbar = ttk.Scrollbar(text_frame, orient="vertical", command=username_text.yview)
205+
scrollbar.pack(side="right", fill="y")
206+
username_text.config(yscrollcommand=scrollbar.set)
207+
208+
username_text.bind("<Control-c>", copy_selected)
209+
210+
# Total count
211+
total_var = tk.StringVar(value=f"Total Usernames: {len(filtered_usernames)}")
212+
ttk.Label(main_frame, textvariable=total_var, font=("Segoe UI", 12)).pack()
213+
214+
# Status Bar
215+
status_var = tk.StringVar(value="Ready")
216+
ttk.Label(root, textvariable=status_var, anchor="w").pack(side="bottom", fill="x")
217+
218+
# Initial load
219+
apply_filter_sort()
220+
221+
# Run App
222+
root.mainloop()

0 commit comments

Comments
 (0)