Python ToDo LIst
import sqlite3
conn = sqlite3.connect('test.db')
conn.execute('''CREATE TABLE ITEMS
(PID CHAR(10),
ITEMID CHAR(10) PRIMARY KEY NOT NULL,
ITEM TEXT NOT NULL,
STATUS INT NOT NULL,
OPEN INT NOT NULL);''')
print("Table created successfully")
conn.close()
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
import sqlite3
root = Tk()
conn = sqlite3.connect('test.db')
menubar = Menu(root)
###functions
def onopened(event):
selected_item = tree.selection()[0] ## get selected item
sql = "UPDATE ITEMS set OPEN = 0 where ITEMID = '%s'" % selected_item
conn.execute(sql)
conn.commit()
def onclosed(event):
selected_item = tree.selection()[0] ## get selected item
sql = "UPDATE ITEMS set OPEN = 1 where ITEMID = '%s'" % selected_item
conn.execute(sql)
conn.commit()
def edit():
selected_item = tree.selection()[0] ## get selected item
newitem = simpledialog.askstring("title", "sdfasfdsadf", initialvalue=tree.item(selected_item, "text"))
tree.item(selected_item, text=newitem)
sql = "UPDATE ITEMS set ITEM = '%s' where ITEMID = '%s'" % (newitem, selected_item)
conn.execute(sql)
conn.commit()
def deletechild(item):
sql = "DELETE FROM ITEMS where ITEMID = '%s'" % item
conn.execute(sql)
conn.commit()
x = tree.get_children(item)
for i in x:
deletechild(i)
def delete():
selected_item = tree.selection()[0] ## get selected item
deletechild(selected_item)
tree.delete(selected_item)
def new():
newitem = simpledialog.askstring("title", "sdfasfdsadf", initialvalue="new todo")
if newitem:
newid = tree.insert("", 'end', text=newitem, tags=('inactive'))
sql = "INSERT INTO ITEMS (ITEMID,ITEM,STATUS,OPEN) VALUES ('%s', '%s',0,1)" % (newid, newitem)
messagebox.showinfo(message=sql)
conn.execute(sql)
conn.commit()
def active():
selected_item = tree.selection()[0] ## get selected item
tree.item(selected_item, tags=('active'))
sql = "UPDATE ITEMS set STATUS = 1 where ITEMID = '%s'" % selected_item
conn.execute(sql)
conn.commit()
def inactive():
selected_item = tree.selection()[0] ## get selected item
tree.item(selected_item, tags=('inactive'))
sql = "UPDATE ITEMS set STATUS = 0 where ITEMID = '%s'" % selected_item
conn.execute(sql)
conn.commit()
def done():
selected_item = tree.selection()[0] ## get selected item
tree.item(selected_item, tags=('done'))
sql = "UPDATE ITEMS set STATUS = 2 where ITEMID = '%s'" % selected_item
conn.execute(sql)
conn.commit()
def show():
messagebox.showinfo(message=tree.get_children())
def newchild():
selected_item = tree.selection()[0] ## get selected item
newitem = simpledialog.askstring("title", "sdfasfdsadf", initialvalue="new todo")
if newitem:
newid = tree.insert(selected_item, 'end', text=newitem, tags=('inactive'),open="True")
sql = "INSERT INTO ITEMS (PID,ITEMID,ITEM,STATUS,OPEN) VALUES ('%s', '%s', '%s',0, 0)" % (selected_item, newid, newitem)
messagebox.showinfo(message=sql)
conn.execute(sql)
conn.commit()
menu_specs = [
("del", delete),
("edit", edit),
("doing", active),
("inactive", inactive),
("done", done),
("newchild", newchild),
]
def onselected(event):
for label, command in menu_specs:
menubar.entryconfig(label, state="normal")
###Menu
menubar.add_command(label="new", command=new)
for label, command in menu_specs:
menubar.add_command(label=label, command=command, state="disabled")
menubar.add_command(label="show", command=show)
root.config(menu=menubar)
###Tree
tree = ttk.Treeview(root, columns=("one","two"), displaycolumns=())
tree.column("#0", width=500)
tree.column("one", width=100 )
tree.column("two", width=100)
tree.heading("#0", text=u"主題")
tree.heading("one", text="coulmn A")
tree.heading("two", text="column B")
def status(arg):
switcher = {
0: "inactive",
1: "active",
2: "done"
}
return switcher.get(arg, "inactive")
def open(arg):
switcher = {
0: "True",
1: "False"
}
return switcher.get(arg, "True")
###data init
cursor = conn.execute("SELECT ITEM,ITEMID,PID,STATUS,OPEN FROM ITEMS")
for row in cursor:
if not row[2]:
tree.insert("", 'end', row[1], text=row[0], tags=(status(row[3])), open=open(row[4]))
else:
tree.insert(row[2], 'end', row[1], text=row[0], tags=(status(row[3])), open=open(row[4]))
###tags
activei = 'blue.ico'
active_pic1 = Image.open(activei)
active_pic2 = ImageTk.PhotoImage(active_pic1)
inactivei = 'black.ico'
inactive_pic1 = Image.open(inactivei)
inactive_pic2 = ImageTk.PhotoImage(inactive_pic1)
ticki = 'tick.ico'
tick_pic1 = Image.open(ticki)
tick_pic2 = ImageTk.PhotoImage(tick_pic1)
tree.tag_configure('active', image=active_pic2, foreground="black")
tree.tag_configure('inactive', image=inactive_pic2, foreground="gray")
tree.tag_configure('done', image=tick_pic2, foreground="green")
tree.pack()
tree.bind("<<TreeviewSelect>>", onselected)
tree.bind("<<TreeviewOpen>>", onopened)
tree.bind("<<TreeviewClose>>", onclosed)
root.mainloop()