[Python] custom tkinter,sqlite3 to make a homepage

Discussão em 'Python' iniciado por Stack, Março 25, 2025.

  1. Stack

    Stack Membro Participativo

    import tkinter as tk
    import customtkinter as ctk
    import sqlite3
    import re
    import smtplib
    from email.mime.text import MIMEText
    import secrets
    import hashlib


    ctk.set_appearance_mode("light")
    ctk.set_default_color_theme("blue")

    class DatabaseManager:
    def __init__(self, db_name='user_database.db'):
    self.conn = sqlite3.connect(db_name)
    self.cursor = self.conn.cursor()
    self.create_table()

    def create_table(self):
    self.cursor.execute('''
    CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    email TEXT UNIQUE NOT NULL,
    password TEXT NOT NULL,
    reset_token TEXT
    )
    ''')
    self.conn.commit()

    def register_user(self, name, email, password):

    hashed_password = hashlib.sha256(password.encode()).hexdigest()
    try:
    self.cursor.execute(
    "INSERT INTO users (name, email, password) VALUES (?, ?, ?)",
    (name, email, hashed_password)
    )
    self.conn.commit()
    return True
    except sqlite3.IntegrityError:
    return False

    def login_user(self, email, password):

    hashed_password = hashlib.sha256(password.encode()).hexdigest()
    self.cursor.execute(
    "SELECT * FROM users WHERE email = ? AND password = ?",
    (email, hashed_password)
    )
    return self.cursor.fetchone() is not None

    def generate_reset_token(self, email):
    token = secrets.token_urlsafe(16)
    self.cursor.execute(
    "UPDATE users SET reset_token = ? WHERE email = ?",
    (token, email)
    )
    self.conn.commit()
    return token

    def reset_password(self, token, new_password):

    hashed_password = hashlib.sha256(new_password.encode()).hexdigest()
    self.cursor.execute(
    "UPDATE users SET password = ?, reset_token = NULL WHERE reset_token = ?",
    (hashed_password, token)
    )
    self.conn.commit()
    return self.cursor.rowcount > 0

    class EmailSender:
    @staticmethod
    def send_reset_email(to_email, reset_token):
    try:

    sender_email = "your_email@gmail.com"
    sender_password = "your_app_password"

    subject = "Password Reset"
    body = f"Your password reset token is: {reset_token}\n\nUse this token to reset your password."

    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['From'] = sender_email
    msg['To'] = to_email

    with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
    server.login(sender_email, sender_password)
    server.send_message(msg)
    return True
    except Exception as e:
    print(f"Email sending failed: {e}")
    return False

    class LoginApp(ctk.CTk):
    def __init__(self):
    super().__init__()


    self.title("Tech Innovations Company")
    self.geometry("800x600")
    self.configure(fg_color="#90EE90")


    self.db_manager = DatabaseManager()


    self.main_frame = ctk.CTkFrame(self, fg_color="white")
    self.main_frame.pack(expand=True, fill="both", padx=20, pady=20)


    title_label = ctk.CTkLabel(
    self.main_frame,
    text="Tech Innovations Company",
    font=("Helvetica", 24, "bold")
    )
    title_label.pack(pady=20)


    article_text = """
    Tech Innovations Company: Pioneering the Future of Technology

    At Tech Innovations, we believe in transforming challenges into opportunities.
    Our mission is to develop cutting-edge technological solutions that solve real-world
    problems and improve people's lives. With a team of passionate innovators and
    forward-thinking engineers, we are committed to pushing the boundaries of what's possible.

    Our core areas of focus include:
    - Artificial Intelligence
    - Sustainable Technology
    - Digital Transformation
    - User-Centric Design
    """

    article_label = ctk.CTkLabel(
    self.main_frame,
    text=article_text,
    font=("Helvetica", 12),
    justify="left",
    wraplength=600
    )
    article_label.pack(pady=10)


    self.tabview = ctk.CTkTabview(self.main_frame)
    self.tabview.pack(expand=True, fill="both", padx=20, pady=20)


    self.login_tab = self.tabview.add("Login")
    self.setup_login_tab()


    self.register_tab = self.tabview.add("Register")
    self.setup_register_tab()

    def setup_login_tab(self):

    self.login_email = ctk.CTkEntry(
    self.login_tab,
    placeholder_text="Email",
    width=300
    )
    self.login_email.pack(pady=10)


    self.login_password = ctk.CTkEntry(
    self.login_tab,
    placeholder_text="Password",
    show="*",
    width=300
    )
    self.login_password.pack(pady=10)

    login_button = ctk.CTkButton(
    self.login_tab,
    text="Login",
    command=self.login_user
    )
    login_button.pack(pady=10)


    forget_password = ctk.CTkButton(
    self.login_tab,
    text="Forget Password",
    fg_color="transparent",
    text_color="blue",
    command=self.forget_password
    )
    forget_password.pack(pady=5)


    social_frame = ctk.CTkFrame(self.login_tab, fg_color="transparent")
    social_frame.pack(pady=10)

    facebook_button = ctk.CTkButton(
    social_frame,
    text="Login with Facebook",
    fg_color="#3b5998"
    )
    facebook_button.pack(side="left", padx=5)

    email_button = ctk.CTkButton(
    social_frame,
    text="Login with Email",
    fg_color="green"
    )
    email_button.pack(side="left", padx=5)

    def setup_register_tab(self):

    self.register_name = ctk.CTkEntry(
    self.register_tab,
    placeholder_text="Full Name",
    width=300
    )
    self.register_name.pack(pady=10)


    self.register_email = ctk.CTkEntry(
    self.register_tab,
    placeholder_text="Email",
    width=300
    )
    self.register_email.pack(pady=10)


    self.register_password = ctk.CTkEntry(
    self.register_tab,
    placeholder_text="Password",
    show="*",
    width=300
    )
    self.register_password.pack(pady=10)


    self.confirm_password = ctk.CTkEntry(
    self.register_tab,
    placeholder_text="Confirm Password",
    show="*",
    width=300
    )
    self.confirm_password.pack(pady=10)


    register_button = ctk.CTkButton(
    self.register_tab,
    text="Register",
    command=self.register_user
    )
    register_button.pack(pady=10)

    def validate_email(self, email):
    email_regex = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
    return re.match(email_regex, email) is not None

    def register_user(self):
    name = self.register_name.get()
    email = self.register_email.get()
    password = self.register_password.get()
    confirm_password = self.confirm_password.get()


    if not name or not email or not password:
    tk.messagebox.showerror("Error", "All fields are required")
    return

    if not self.validate_email(email):
    tk.messagebox.showerror("Error", "Invalid email format")
    return

    if password != confirm_password:
    tk.messagebox.showerror("Error", "Passwords do not match")
    return


    if self.db_manager.register_user(name, email, password):
    tk.messagebox.showinfo("Success", "Registration Successful")
    else:
    tk.messagebox.showerror("Error", "Email already exists")

    def login_user(self):
    email = self.login_email.get()
    password = self.login_password.get()

    if not email or not password:
    tk.messagebox.showerror("Error", "Email and Password are required")
    return

    if self.db_manager.login_user(email, password):
    tk.messagebox.showinfo("Success", "Login Successful")
    else:
    tk.messagebox.showerror("Error", "Invalid Credentials")

    def forget_password(self):
    email = self.login_email.get()
    if not email:
    tk.messagebox.showerror("Error", "Please enter your email")
    return

    reset_token = self.db_manager.generate_reset_token(email)
    if EmailSender.send_reset_email(email, reset_token):
    tk.messagebox.showinfo("Reset Password", "Reset token sent to your email")
    else:
    tk.messagebox.showerror("Error", "Failed to send reset email")

    if __name__ == "__main__":
    app = LoginApp()
    app.mainloop()


    My customer tkinter on visual code is not working how can i fix that it comes with a squiggly line in yellow. This is a code for an home page of a company to make a home page that will allow you to click to register to sign up by name,email, password and confirmation password by email and save the details in the sql database and make a login button that will by email and password and have a facebook and email and small forget password click here for email to reset passwordenter code here

    Continue reading...

Compartilhe esta Página