I need you to code an app for me. A trial tracker; It tracks all of your free trials,…
- User
I need you to code an app for me. A trial tracker; It tracks all of your free trials, and the associated email addresses
- Assistant
I need you to code an app for me. A trial tracker; It tracks all of your free trials, and the associated email addresses
Certainly! I'd be happy to help you create a trial tracker application that keeps track of all your free trials along with the associated email addresses. We'll build a simple desktop application using Python and the Tkinter library for the graphical user interface (GUI). We'll also use SQLite for storing the trial data locally.
Create a new directory for your project, e.g., trial_tracker_app, and navigate to it.
Create a new Python file named trial_tracker.py in your project directory.
We need to import the following libraries:
tkinter for the GUI.sqlite3 for the database.datetime for handling dates.import tkinter as tk
from tkinter import messagebox
import sqlite3
from datetime import datetime
We'll use SQLite to store trial information.
# Connect to SQLite database (creates the database file if it doesn't exist)
conn = sqlite3.connect('trials.db')
c = conn.cursor()
# Create the trials table
c.execute('''
CREATE TABLE IF NOT EXISTS trials (
id INTEGER PRIMARY KEY AUTOINCREMENT,
service TEXT NOT NULL,
email TEXT NOT NULL,
start_date TEXT NOT NULL,
end_date TEXT NOT NULL
)
''')
conn.commit()
Create the main application window and set its title.
root = tk.Tk()
root.title("Trial Tracker")
Function to Add a New Trial:
def add_trial():
service = entry_service.get()
email = entry_email.get()
start_date = entry_start_date.get()
end_date = entry_end_date.get()
if service and email and start_date and end_date:
try:
# Validate date formats
datetime.strptime(start_date, '%Y-%m-%d')
datetime.strptime(end_date, '%Y-%m-%d')
# Insert into the database
c.execute("""
INSERT INTO trials (service, email, start_date, end_date)
VALUES (?, ?, ?, ?)
""", (service, email, start_date, end_date))
conn.commit()
# Clear the input fields
entry_service.delete(0, tk.END)
entry_email.delete(0, tk.END)
entry_start_date.delete(0, tk.END)
entry_end_date.delete(0, tk.END)
# Refresh the trials list
view_trials()
except ValueError:
messagebox.showerror("Invalid Date Format", "Please enter dates in YYYY-MM-DD format.")
else:
messagebox.showwarning("Input Error", "Please fill out all fields.")
Function to View All Trials:
def view_trials():
# Clear the listbox
listbox_trials.delete(0, tk.END)
# Fetch trials from the database
c.execute("SELECT * FROM trials")
rows = c.fetchall()
for row in rows:
trial_info = f"ID: {row[0]} | Service: {row[1]} | Email: {row[2]} | Start: {row[3]} | End: {row[4]}"
listbox_trials.insert(tk.END, trial_info)
Function to Delete a Selected Trial:
def delete_trial():
try:
# Get selected item index
selected_index = listbox_trials.curselection()[0]
selected_item = listbox_trials.get(selected_index)
# Extract the ID from the selected item
trial_id = selected_item.split('|')[0].split(':')[1].strip()
# Delete from the database
c.execute("DELETE FROM trials WHERE id = ?", (trial_id,))
conn.commit()
# Refresh the trials list
view_trials()
except IndexError:
messagebox.showwarning("Selection Error", "Please select a trial to delete.")
Input Fields for Adding a New Trial:
# Labels and Entries for Service Name
label_service = tk.Label(root, text="Service Name:")
label_service.grid(row=0, column=0, padx=5, pady=5, sticky=tk.E)
entry_service = tk.Entry(root)
entry_service.grid(row=0, column=1, padx=5, pady=5)
# Labels and Entries for Email Address
label_email = tk.Label(root, text="Email Address:")
label_email.grid(row=1, column=0, padx=5, pady=5, sticky=tk.E)
entry_email = tk.Entry(root)
entry_email.grid(row=1, column=1, padx=5, pady=5)
# Labels and Entries for Start Date
label_start_date = tk.Label(root, text="Start Date (YYYY-MM-DD):")
label_start_date.grid(row=2, column=0, padx=5, pady=5, sticky=tk.E)
entry_start_date = tk.Entry(root)
entry_start_date.grid(row=2, column=1, padx=5, pady=5)
# Labels and Entries for End Date
label_end_date = tk.Label(root, text="End Date (YYYY-MM-DD):")
label_end_date.grid(row=3, column=0, padx=5, pady=5, sticky=tk.E)
entry_end_date = tk.Entry(root)
entry_end_date.grid(row=3, column=1, padx=5, pady=5)
Buttons for Adding and Deleting Trials:
# Button to Add Trial
button_add = tk.Button(root, text="Add Trial", command=add_trial)
button_add.grid(row=4, column=0, columnspan=2, pady=10)
# Button to Delete Selected Trial
button_delete = tk.Button(root, text="Delete Selected Trial", command=delete_trial)
button_delete.grid(row=6, column=0, columnspan=2, pady=10)
Listbox to Display Trials:
# Label for Trials List
label_trials = tk.Label(root, text="Your Trials:")
label_trials.grid(row=5, column=0, padx=5, pady=5)
# Listbox and Scrollbar for Trials
scrollbar = tk.Scrollbar(root)
scrollbar.grid(row=5, column=2, sticky='ns')
listbox_trials = tk.Listbox(root, height=10, width=80, yscrollcommand=scrollbar.set)
listbox_trials.grid(row=5, column=1, padx=5, pady=5)
scrollbar.config(command=listbox_trials.yview)
Call the view_trials() function to display any existing trials when the application starts.
view_trials()
Start the main event loop of the application.
root.mainloop()
# Close the database connection when the application is closed
conn.close()
Here is the complete code for trial_tracker.py:
import tkinter as tk
from tkinter import messagebox
import sqlite3
from datetime import datetime
# Connect to SQLite database
conn = sqlite3.connect('trials.db')
c = conn.cursor()
# Create the trials table
c.execute('''
CREATE TABLE IF NOT EXISTS trials (
id INTEGER PRIMARY KEY AUTOINCREMENT,
service TEXT NOT NULL,
email TEXT NOT NULL,
start_date TEXT NOT NULL,
end_date TEXT NOT NULL
)
''')
conn.commit()
# Create the main application window
root = tk.Tk()
root.title("Trial Tracker")
# Function to add a new trial
def add_trial():
service = entry_service.get()
email = entry_email.get()
start_date = entry_start_date.get()
end_date = entry_end_date.get()
if service and email and start_date and end_date:
try:
# Validate date formats
datetime.strptime(start_date, '%Y-%m-%d')
datetime.strptime(end_date, '%Y-%m-%d')
# Insert into the database
c.execute("""
INSERT INTO trials (service, email, start_date, end_date)
VALUES (?, ?, ?, ?)
""", (service, email, start_date, end_date))
conn.commit()
# Clear the input fields
entry_service.delete(0, tk.END)
entry_email.delete(0, tk.END)
entry_start_date.delete(0, tk.END)
entry_end_date.delete(0, tk.END)
# Refresh the trials list
view_trials()
except ValueError:
messagebox.showerror("Invalid Date Format", "Please enter dates in YYYY-MM-DD format.")
else:
messagebox.showwarning("Input Error", "Please fill out all fields.")
# Function to view all trials
def view_trials():
# Clear the listbox
listbox_trials.delete(0, tk.END)
# Fetch trials from the database
c.execute("SELECT * FROM trials")
rows = c.fetchall()
for row in rows:
trial_info = f"ID: {row[0]} | Service: {row[1]} | Email: {row[2]} | Start: {row[3]} | End: {row[4]}"
listbox_trials.insert(tk.END, trial_info)
# Function to delete a selected trial
def delete_trial():
try:
# Get selected item index
selected_index = listbox_trials.curselection()[0]
selected_item = listbox_trials.get(selected_index)
# Extract the ID from the selected item
trial_id = selected_item.split('|')[0].split(':')[1].strip()
# Delete from the database
c.execute("DELETE FROM trials WHERE id = ?", (trial_id,))
conn.commit()
# Refresh the trials list
view_trials()
except IndexError:
messagebox.showwarning("Selection Error", "Please select a trial to delete.")
# Labels and Entries for inputs
label_service = tk.Label(root, text="Service Name:")
label_service.grid(row=0, column=0, padx=5, pady=5, sticky=tk.E)
entry_service = tk.Entry(root)
entry_service.grid(row=0, column=1, padx=5, pady=5)
label_email = tk.Label(root, text="Email Address:")
label_email.grid(row=1, column=0, padx=5, pady=5, sticky=tk.E)
entry_email = tk.Entry(root)
entry_email.grid(row=1, column=1, padx=5, pady=5)
label_start_date = tk.Label(root, text="Start Date (YYYY-MM-DD):")
label_start_date.grid(row=2, column=0, padx=5, pady=5, sticky=tk.E)
entry_start_date = tk.Entry(root)
entry_start_date.grid(row=2, column=1, padx=5, pady=5)
label_end_date = tk.Label(root, text="End Date (YYYY-MM-DD):")
label_end_date.grid(row=3, column=0, padx=5, pady=5, sticky=tk.E)
entry_end_date = tk.Entry(root)
entry_end_date.grid(row=3, column=1, padx=5, pady=5)
# Buttons
button_add = tk.Button(root, text="Add Trial", command=add_trial)
button_add.grid(row=4, column=0, columnspan=2, pady=10)
button_delete = tk.Button(root, text="Delete Selected Trial", command=delete_trial)
button_delete.grid(row=6, column=0, columnspan=2, pady=10)
# Listbox to display trials
label_trials = tk.Label(root, text="Your Trials:")
label_trials.grid(row=5, column=0, padx=5, pady=5)
scrollbar = tk.Scrollbar(root)
scrollbar.grid(row=5, column=2, sticky='ns')
listbox_trials = tk.Listbox(root, height=10, width=80, yscrollcommand=scrollbar.set)
listbox_trials.grid(row=5, column=1, padx=5, pady=5)
scrollbar.config(command=listbox_trials.yview)
# Initialize the application
view_trials()
# Run the application
root.mainloop()
# Close the database connection
conn.close()
Save the Script:
Save the complete code in a file named trial_tracker.py in your project directory.
Install Necessary Libraries (if needed):
The tkinter library usually comes pre-installed with Python. If you encounter any issues, you may need to install it using your package manager.
Run the Script:
Open a terminal or command prompt, navigate to your project directory, and run:
python trial_tracker.py
Use the Application:
You can enhance the application with additional features:
You've now got a functional trial tracker application that you can use to keep track of all your free trials and associated email addresses. Feel free to customize and expand upon this base to suit your specific needs.
Let me know if you have any questions or need further assistance with the code!