· 6 years ago · Aug 23, 2019, 10:38 AM
1from tkinter import *
2from tkinter.messagebox import *
3from db import Task
4
5# https://pastebin.com/4FUEKbWL todo.py
6# https://pastebin.com/H7g4CDnE db.py
7
8
9class TodoApp(Frame):
10 def __init__(self, t):
11 self.t = t
12 super().__init__(self.t)
13 self.t.title("TODO App")
14 self.t.iconbitmap('icon.ico')
15 self.grid(padx=20, pady=20)
16 self.start_interface()
17
18 self.tasks = Task.all()
19 self.rerender()
20 return
21
22 def start_interface(self):
23 font = ("Calibri", 24, "normal")
24
25 self.text_holder = StringVar()
26 self.text_input = Entry(self, textvariable=self.text_holder, font=font)
27 self.text_input.grid(row=1, column=1, columnspan=2)
28
29 self.text_desc = StringVar()
30 self.text_desc_input = Entry(self, textvariable=self.text_desc, font=font)
31 self.text_desc_input.grid(row=2, column=1, columnspan=2)
32
33 self.button = Button(self, text="Dodaj", font=font, command=self.clickButton)
34 self.button.grid(row=3, column=1, pady=10)
35
36 self.delete = Button(self, text="Izbrisi", font=font, command=self.deleteButton)
37 self.delete.grid(row=3, column=2, pady=10)
38
39 self.listaIspis = Listbox(self, font=font)
40 self.listaIspis.grid(row=4, column=1, columnspan=2)
41
42 self.text_input.bind('<Return>', self.clickButton)
43 self.text_desc_input.bind('<Return>', self.clickButton)
44 return
45
46 def clickButton(self, e=None):
47 text = self.text_holder.get()
48 desc = self.text_desc.get()
49
50 if text != '':
51 task = Task(text, desc)
52 task.save()
53 self.tasks = Task.all()
54 self.text_holder.set('')
55 self.text_desc.set('')
56 self.rerender()
57 print(self.tasks)
58
59 return
60
61 def deleteButton(self):
62 selected = list(self.listaIspis.curselection())
63 if len(selected) == 1:
64 output = askyesnocancel('Delete', 'Are you sure you want to delete?')
65 if output:
66 self.tasks[selected[0]].delete()
67 self.tasks = Task.all()
68 self.rerender()
69
70 def rerender(self):
71 self.listaIspis.delete(0, END)
72 for task in self.tasks:
73 self.listaIspis.insert(END, task)
74 return
75
76
77todoApp = TodoApp(Tk())
78mainloop()
79
80# https://pastebin.com/zHHSkNhr
81
82
83from sqlite3 import *
84
85
86class DB:
87 def __init__(self):
88 self.connection = connect('todo.db')
89 self.cursor = self.connection.cursor()
90
91 self.createTable()
92
93 # https://pastebin.com/i7uLp7dB
94
95 def createTable(self):
96 try:
97 self.update('''
98 CREATE TABLE Tasks (
99 id INTEGER UNIQUE NOT NULL PRIMARY KEY AUTOINCREMENT,
100 task TEXT NOT NULL,
101 desc TEXT NOT NULL,
102 date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
103 );
104 ''')
105 except OperationalError:
106 print('Database exists')
107
108 def query(self, query):
109 self.cursor.execute(query)
110 return self.cursor.fetchall()
111
112 def update(self, query):
113 self.cursor.execute(query)
114 self.connection.commit()
115 return self.cursor.fetchall()
116
117
118class Task:
119 def __init__(self, name, desc, id=None):
120 self.name = name
121 self.desc = desc
122 self.id = id
123
124 def __str__(self):
125 if len(self.desc) > 0:
126 return '{} ({})'.format(self.name, self.desc)
127 else:
128 return self.name
129
130 def __repr__(self):
131 return self.__str__()
132
133 def save(self):
134 db.update('''
135 INSERT INTO Tasks
136 ("task", "desc")
137 VALUES("{}", "{}")'''.format(self.name, self.desc)
138 )
139
140 def delete(self):
141 db.update('DELETE FROM Tasks WHERE id = {}'.format(self.id))
142
143 @staticmethod
144 def all():
145 tasks = db.query('SELECT * FROM Tasks')
146 output = []
147 for task in tasks:
148 print(task)
149 output.append(Task(task[1], task[2], task[0]))
150 return output
151
152
153db = DB()