Habit Tracker

 import sqlite3

import tkinter as tk

from tkinter import messagebox


# Database Setup

conn = sqlite3.connect("habits.db")

cursor = conn.cursor()

cursor.execute("""

    CREATE TABLE IF NOT EXISTS habits (

        id INTEGER PRIMARY KEY AUTOINCREMENT,

        name TEXT NOT NULL,

        status TEXT DEFAULT 'Pending'

    )

""")

conn.commit()


# Function to add a new habit

def add_habit():

    habit_name = habit_entry.get().strip()

    if habit_name:

        cursor.execute("INSERT INTO habits (name, status) VALUES (?, 'Pending')", (habit_name,))

        conn.commit()

        habit_entry.delete(0, tk.END)

        load_habits()

    else:

        messagebox.showwarning("Warning", "Please enter a habit name!")


# Function to mark a habit as completed

def complete_habit(habit_id):

    cursor.execute("UPDATE habits SET status = 'Completed' WHERE id = ?", (habit_id,))

    conn.commit()

    load_habits()


# Function to delete a habit

def delete_habit(habit_id):

    cursor.execute("DELETE FROM habits WHERE id = ?", (habit_id,))

    conn.commit()

    load_habits()


# Function to load habits from the database

def load_habits():

    habit_list.delete(0, tk.END)

    cursor.execute("SELECT id, name, status FROM habits")

    for habit in cursor.fetchall():

        habit_id, name, status = habit

        habit_list.insert(tk.END, f"{habit_id}. {name} - {status}")


# GUI Setup

root = tk.Tk()

root.title("Habit Tracker")

root.geometry("400x500")


tk.Label(root, text="Enter a New Habit:", font=("Arial", 12)).pack(pady=5)

habit_entry = tk.Entry(root, width=40)

habit_entry.pack(pady=5)

tk.Button(root, text="Add Habit", command=add_habit).pack(pady=5)


tk.Label(root, text="Your Habits:", font=("Arial", 12)).pack(pady=5)

habit_list = tk.Listbox(root, width=50, height=10)

habit_list.pack()


tk.Button(root, text="Mark as Completed", command=lambda: complete_habit(habit_list.get(tk.ACTIVE).split('.')[0])).pack(pady=5)

tk.Button(root, text="Delete Habit", command=lambda: delete_habit(habit_list.get(tk.ACTIVE).split('.')[0])).pack(pady=5)


load_habits()


root.mainloop()


Secure Notes App

 import os

import tkinter as tk

from tkinter import messagebox, simpledialog

from cryptography.fernet import Fernet


# Generate a key file if it doesn’t exist

KEY_FILE = "secret.key"

NOTES_FILE = "secure_notes.txt"


def generate_key():

    key = Fernet.generate_key()

    with open(KEY_FILE, "wb") as key_file:

        key_file.write(key)


def load_key():

    if not os.path.exists(KEY_FILE):

        generate_key()

    with open(KEY_FILE, "rb") as key_file:

        return key_file.read()


# Load encryption key

key = load_key()

cipher_suite = Fernet(key)


def encrypt_message(message):

    return cipher_suite.encrypt(message.encode()).decode()


def decrypt_message(encrypted_message):

    return cipher_suite.decrypt(encrypted_message.encode()).decode()


def save_note():

    note = note_text.get("1.0", tk.END).strip()

    if not note:

        messagebox.showwarning("Warning", "Note cannot be empty!")

        return

    

    encrypted_note = encrypt_message(note)

    with open(NOTES_FILE, "a") as file:

        file.write(encrypted_note + "\n")

    

    messagebox.showinfo("Success", "Note saved securely!")

    note_text.delete("1.0", tk.END)


def load_notes():

    if not os.path.exists(NOTES_FILE):

        messagebox.showinfo("No Notes", "No saved notes found.")

        return

    

    with open(NOTES_FILE, "r") as file:

        encrypted_notes = file.readlines()

    

    if not encrypted_notes:

        messagebox.showinfo("No Notes", "No saved notes found.")

        return


    password = simpledialog.askstring("Password", "Enter decryption password:", show="*")

    

    if password:  # Dummy check

        try:

            decrypted_notes = [decrypt_message(note.strip()) for note in encrypted_notes]

            messagebox.showinfo("Your Notes", "\n\n".join(decrypted_notes))

        except Exception as e:

            messagebox.showerror("Error", "Failed to decrypt notes.")

    else:

        messagebox.showwarning("Warning", "Password cannot be empty!")


# GUI Setup

root = tk.Tk()

root.title("Secure Notes App")

root.geometry("400x400")


tk.Label(root, text="Enter your note:", font=("Arial", 12)).pack(pady=5)

note_text = tk.Text(root, height=8, width=40)

note_text.pack()


save_btn = tk.Button(root, text="Save Note", command=save_note)

save_btn.pack(pady=5)


load_btn = tk.Button(root, text="Load Notes", command=load_notes)

load_btn.pack(pady=5)


root.mainloop()


Password Manager

 import sqlite3

import tkinter as tk

from tkinter import messagebox

from cryptography.fernet import Fernet

import random

import string

import os


# Generate and save encryption key

KEY_FILE = "key.key"


def generate_key():

    if not os.path.exists(KEY_FILE):

        key = Fernet.generate_key()

        with open(KEY_FILE, "wb") as key_file:

            key_file.write(key)


def load_key():

    with open(KEY_FILE, "rb") as key_file:

        return key_file.read()


generate_key()

key = load_key()

cipher = Fernet(key)


# Database Setup

conn = sqlite3.connect("passwords.db")

cursor = conn.cursor()

cursor.execute("""

    CREATE TABLE IF NOT EXISTS passwords (

        id INTEGER PRIMARY KEY,

        website TEXT,

        username TEXT,

        password TEXT

    )

""")

conn.commit()


# Function to encrypt password

def encrypt_password(password):

    return cipher.encrypt(password.encode()).decode()


# Function to decrypt password

def decrypt_password(encrypted_password):

    return cipher.decrypt(encrypted_password.encode()).decode()


# Function to generate a random password

def generate_password():

    characters = string.ascii_letters + string.digits + string.punctuation

    return ''.join(random.choice(characters) for _ in range(12))


# Function to save credentials

def save_password():

    website = website_entry.get()

    username = username_entry.get()

    password = password_entry.get()


    if not website or not username or not password:

        messagebox.showerror("Error", "All fields are required!")

        return


    encrypted_password = encrypt_password(password)

    cursor.execute("INSERT INTO passwords (website, username, password) VALUES (?, ?, ?)",

                   (website, username, encrypted_password))

    conn.commit()

    messagebox.showinfo("Success", "Password saved successfully!")


    website_entry.delete(0, tk.END)

    username_entry.delete(0, tk.END)

    password_entry.delete(0, tk.END)


# Function to retrieve passwords

def retrieve_passwords():

    cursor.execute("SELECT website, username, password FROM passwords")

    records = cursor.fetchall()


    result_text.delete(1.0, tk.END)

    for record in records:

        website, username, encrypted_password = record

        decrypted_password = decrypt_password(encrypted_password)

        result_text.insert(tk.END, f"Website: {website}\nUsername: {username}\nPassword: {decrypted_password}\n\n")


# Function to generate a random password

def fill_generated_password():

    password_entry.delete(0, tk.END)

    password_entry.insert(0, generate_password())


# GUI Setup

root = tk.Tk()

root.title("Password Manager")

root.geometry("400x500")


tk.Label(root, text="Website:").pack()

website_entry = tk.Entry(root, width=40)

website_entry.pack()


tk.Label(root, text="Username:").pack()

username_entry = tk.Entry(root, width=40)

username_entry.pack()


tk.Label(root, text="Password:").pack()

password_entry = tk.Entry(root, width=40, show="*")

password_entry.pack()


tk.Button(root, text="Generate Password", command=fill_generated_password).pack(pady=5)

tk.Button(root, text="Save Password", command=save_password).pack(pady=5)

tk.Button(root, text="Retrieve Passwords", command=retrieve_passwords).pack(pady=5)


result_text = tk.Text(root, height=10, width=45)

result_text.pack()


root.mainloop()


Book Tracker

 import sqlite3

from tkinter import Tk, Label, Entry, Button, ttk, messagebox


# Database setup

def setup_database():

    conn = sqlite3.connect('books.db')

    cursor = conn.cursor()

    cursor.execute("""

        CREATE TABLE IF NOT EXISTS books (

            id INTEGER PRIMARY KEY AUTOINCREMENT,

            title TEXT NOT NULL,

            author TEXT NOT NULL,

            status TEXT NOT NULL

        )

    """)

    conn.commit()

    conn.close()


# Add book

def add_book(title, author, status):

    if not title or not author or not status:

        messagebox.showerror("Error", "All fields are required.")

        return

    conn = sqlite3.connect('books.db')

    cursor = conn.cursor()

    cursor.execute("INSERT INTO books (title, author, status) VALUES (?, ?, ?)", (title, author, status))

    conn.commit()

    conn.close()

    messagebox.showinfo("Success", "Book added successfully!")

    refresh_list()


# Get books

def get_books():

    conn = sqlite3.connect('books.db')

    cursor = conn.cursor()

    cursor.execute("SELECT id, title, author, status FROM books")

    books = cursor.fetchall()

    conn.close()

    return books


# Update book status

def update_status(book_id, new_status):

    conn = sqlite3.connect('books.db')

    cursor = conn.cursor()

    cursor.execute("UPDATE books SET status = ? WHERE id = ?", (new_status, book_id))

    conn.commit()

    conn.close()

    messagebox.showinfo("Success", "Book status updated successfully!")

    refresh_list()


# Delete book

def delete_book(book_id):

    conn = sqlite3.connect('books.db')

    cursor = conn.cursor()

    cursor.execute("DELETE FROM books WHERE id = ?", (book_id,))

    conn.commit()

    conn.close()

    messagebox.showinfo("Success", "Book deleted successfully!")

    refresh_list()


# Refresh the book list in the GUI

def refresh_list():

    for row in tree.get_children():

        tree.delete(row)

    books = get_books()

    for book in books:

        tree.insert("", "end", values=book)


# GUI setup

def setup_gui():

    global tree

    root = Tk()

    root.title("Book Tracker")


    # Labels and input fields

    Label(root, text="Title:").grid(row=0, column=0, padx=5, pady=5)

    title_entry = Entry(root)

    title_entry.grid(row=0, column=1, padx=5, pady=5)


    Label(root, text="Author:").grid(row=1, column=0, padx=5, pady=5)

    author_entry = Entry(root)

    author_entry.grid(row=1, column=1, padx=5, pady=5)


    Label(root, text="Status:").grid(row=2, column=0, padx=5, pady=5)

    status_combobox = ttk.Combobox(root, values=["To Read", "Reading", "Finished"])

    status_combobox.grid(row=2, column=1, padx=5, pady=5)


    # Buttons

    Button(root, text="Add Book", command=lambda: add_book(title_entry.get(), author_entry.get(), status_combobox.get())).grid(row=3, column=0, columnspan=2, pady=10)


    # Book list

    columns = ("ID", "Title", "Author", "Status")

    tree = ttk.Treeview(root, columns=columns, show="headings")

    for col in columns:

        tree.heading(col, text=col)

        tree.column(col, width=150)

    tree.grid(row=4, column=0, columnspan=2, padx=5, pady=5)


    # Actions

    Button(root, text="Update Status", command=lambda: update_status(tree.item(tree.focus())['values'][0], status_combobox.get())).grid(row=5, column=0, pady=10)

    Button(root, text="Delete Book", command=lambda: delete_book(tree.item(tree.focus())['values'][0])).grid(row=5, column=1, pady=10)


    refresh_list()

    root.mainloop()


if __name__ == "__main__":

    setup_database()

    setup_gui()