Skip to content

Instantly share code, notes, and snippets.

@shinysu
Created April 10, 2021 14:31
Show Gist options
  • Save shinysu/a6e88b58b041c7f5ab7c1ae0ac703f56 to your computer and use it in GitHub Desktop.
Save shinysu/a6e88b58b041c7f5ab7c1ae0ac703f56 to your computer and use it in GitHub Desktop.
TODOLIST_DB
import sqlite3
sql_query = """ CREATE TABLE IF NOT EXISTS tasks(
id INTEGER PRIMARY KEY,
taskname TEXT) """
def execute_query(sql_query):
with sqlite3.connect('todolist') as db:
csr = db.cursor()
result = csr.execute(sql_query)
db.commit()
return result
execute_query(sql_query)
import sqlite3
def execute_query(sql_query):
with sqlite3.connect('todolist') as db:
csr = db.cursor()
result = csr.execute(sql_query)
db.commit()
return result
def insert_into_table(task):
sql_query = """INSERT INTO tasks(taskname) VALUES('%s')""" % (task)
execute_query(sql_query)
def select_from_table():
sql_query = """SELECT taskname from tasks"""
result = execute_query(sql_query)
return [result[0] for result in result.fetchall()]
def update_table(oldtask, newtask):
sql_query = """UPDATE tasks SET taskname='%s' WHERE taskname='%s'""" % (newtask, oldtask)
execute_query(sql_query)
def delete_from_table(task):
sql_query = "DELETE FROM tasks WHERE taskname='%s'" % (task)
execute_query(sql_query)
import PySimpleGUI as sg
from dbfunctions import insert_into_table, select_from_table, update_table, delete_from_table
tasks = select_from_table()
#print(tasks)
layout = [
[sg.InputText('', size=(40, 1), font=("Arial", 14), key='todo_item'),
sg.Button(button_text='Add', font=("Arial", 14), key='add_save')],
[sg.Listbox(values=tasks, size=(40, 10), font=("Arial", 14), key='items'),
sg.Button('Delete', font=("Arial", 14), key='delete'),
sg.Button('Edit', key='edit', font=("Arial", 14))]
]
def add_item(task):
action = window.FindElement('add_save').GetText()
if action == 'Add':
insert_into_table(task)
window.FindElement('todo_item').Update('')
new_tasks = select_from_table()
window.FindElement('items').Update(values=new_tasks)
elif action == 'Save':
oldtask = values['items'][0]
update_table(oldtask, task)
new_tasks = select_from_table()
window.FindElement('items').Update(values=new_tasks)
window.FindElement('add_save').Update('Add')
def edit_item(oldtask):
window.FindElement('todo_item').Update(value=oldtask)
window.FindElement('add_save').Update('Save')
def delete_item(task):
delete_from_table(task)
new_tasks = select_from_table()
window.FindElement('items').Update(values=new_tasks)
if __name__ == "__main__":
window = sg.Window("My first app", layout)
while True:
button, values = window.Read()
if button == 'add_save':
add_item(values['todo_item'])
elif button == 'edit':
edit_item(values['items'][0])
elif button == 'delete':
delete_item(values['items'][0])
elif button == sg.WINDOW_CLOSED:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment