Trying to code a ToDo app that allows me to create locations and the tasks. Everything worked really well when it was just one value but adding a second has caused issues. Recalling the data from the Database to the listbox. Supposedly it saves to the database with the two values.
Create the Database
# Create database or connect to one
conn = sqlite3.connect('MyList.db')
# Create a cursor
c = conn.cursor()
# Create table
c.execute("""CREATE TABLE if not exists todo_list(
list_item text,
location text)
""")
# Commit changes
conn.commit()
# Close connection
conn.close()
This is supposed to Add the DB to the ListBox
def grab_all():
# Create database or connect to one
conn = sqlite3.connect('MyList.db')
# Create a cursor
c = conn.cursor()
c.execute("SELECT * FROM todo_list")
records = c.fetchall()
# Commit changes
conn.commit()
# Close connection
conn.close()
# Loop through records and add to screen
for record in records:
item = record[0]
location = record[1]
display_text = f"{location}- {item}"
#ToDoListbox.insert('end', display_text)
ToDoListbox.insert('end', item)
set_item_color(ToDoListbox.size() - 1, item) # Set color based on the item
def add_item(event=None):
item = entry1.get().strip() # Get the item from the entry widget
location = SchoolBox.get().strip() # Get the selected location from the ComboBox
if item and location:
display_text = f"{location} - {item}"
ToDoListbox.insert('end', display_text)
set_item_color(ToDoListbox.size() - 1, item) # Set color based on the item
entry1.delete(0, 'end')
SchoolBox.set('') # Reset ComboBox selection
else:
messagebox.showwarning("Warning", "Please enter a task and select a location.")
def save_items(event=None):
# Create database or connect to one
conn = sqlite3.connect('MyList.db')
# Create a cursor
c = conn.cursor()
# Delete everything in the database table
c.execute('DELETE FROM todo_list')
# Blank list to hold todo items
items = []
location = []
# Loop through the listWidget and pull out items
for index in range(ToDoListbox.size()):
items.append(ToDoListbox.get(index))
location = SchoolBox.get().strip()
for item in items:
# Add stuff to the table
c.execute('INSERT INTO todo_list VALUES (:item)', {'item': item})
# Commit changes
conn.commit()
# Close connection
conn.close()
messagebox.showinfo("Info", "Tasks saved successfully.")
'''
def save_items(event=None):
# Create database or connect to one
conn = sqlite3.connect('MyList.db')
# Create a cursor
c = conn.cursor()
# Delete everything in the database table
c.execute('DELETE FROM todo_list')
# Loop through the listWidget and pull out items
for index in range(ToDoListbox.size()):
item = ToDoListbox.get(index)
location = item.split('-')[0].strip() # Split by '-' and take the first part as location
task = item.split('-')[1].strip() # Split by '-' and take the second part as task
# Add stuff to the table
c.execute('INSERT INTO todo_list VALUES (?, ?)', (location,task))
print(task)
print(location)
# Commit changes
conn.commit()
# Close connection
conn.close()
messagebox.showinfo("Info", "Tasks saved successfully.")
Any help is apreciated Thank you
user25296300 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.