· 4 years ago · Mar 12, 2021, 11:42 AM
1from tkinter import *
2from tkinter import ttk
3import sqlite3
4from datetime import date
5
6
7class Database():
8 def __init__(self, name):
9 self.con = sqlite3.connect(name)
10 self.con.commit()
11 print("Opened database successfully")
12 self.cur = self.con.cursor()
13 self.db_create()
14
15 def db_create(self):
16 create = """
17 CREATE TABLE IF NOT EXISTS log_table(
18 ID INTEGER PRIMARY KEY AUTOINCREMENT,
19 date DATE NOT NULL,
20 log TEXT NOT NULL
21 );"""
22 self.cur.execute(create)
23 self.con.commit()
24
25 print("Table created successfully")
26
27 def add_log(self, input):
28 today = date.today()
29 now = today.strftime("%Y-%m-%d")
30 add = """ INSERT INTO log_table(date, log) VALUES('{date_now}', "{text}");""".format(date_now=now, text=input)
31 self.cur.execute(add)
32 self.con.commit()
33
34 def get_all(self):
35 select = """SELECT * FROM log_table"""
36 log = self.cur.execute(select).fetchall()
37 self.con.commit()
38 return log
39
40 def update(self, id, change_input):
41 update = """UPDATE log_table SET log= '{text}' WHERE ID = {numb}""".format(text=change_input, numb=id)
42 self.cur.execute(update)
43 self.con.commit()
44
45
46class MainApplication():
47 def __init__(self, master):
48 self.db = Database('log_db.db')
49 # self.db.add_log('Test')
50 self.master = master
51 self.user_interface()
52
53 def user_interface(self):
54 self.header = ttk.Label(self.master, text='Log-Buch', ).pack()
55 self.overview = ttk.Frame(self.master).pack()
56 self.tree = ttk.Treeview(self.overview, colum=('log', 'date', 'action'))
57 self.tree.column("#0", width=20)
58 self.tree.column("log", width=100)
59 self.tree.column("date", width=50)
60 self.tree.column("action", width=100)
61 self.tree.heading('#0', text='ID', anchor=CENTER)
62 self.tree.heading('#1', text='Log Text', anchor=CENTER)
63 self.tree.heading('#2', text='Date', anchor=CENTER)
64 self.tree.heading('#3', text='Action', anchor=CENTER)
65
66 rows = []
67 db_output = self.db.get_all()
68 for i in db_output:
69 rows.append(self.tree.insert('', i[0], text=f'{i[0]}', values=(i[1], i[2])))
70
71 self.tree.pack(expand=YES, fill=BOTH)
72 self.input_frame = ttk.Frame(self.master).pack()
73
74 self.input_log = ttk.Entry(self.input_frame)
75
76 def save_data():
77 text = self.input_log.get()
78 self.db.add_log(text)
79 rows = []
80 db_output = self.db.get_all()
81 for i in db_output:
82 rows.append(self.tree.insert('', i[0], text=f'{i[0]}', values=(i[1], i[2])))
83
84 self.btn_save = ttk.Button(self.input_frame, text='Save input', command=save_data).pack()
85 self.input_log.pack()
86
87
88if __name__ in "__main__":
89 root = Tk()
90 root.geometry('600x300')
91 root.title("Log buch")
92 app = MainApplication(root)
93 root.mainloop()
94