· 8 years ago · Feb 11, 2018, 09:54 PM
1from tkinter import *
2import sqlite3
3
4class mainGUI(object):
5
6def __init__(self,window):
7
8 self.window=window
9 self.window.wm_title("Running Diary") #Main window label/title
10
11 F1 = LabelFrame(window, text="Dragan Mudric", borderwidth=2, relief="groove") #Create LabelFrame widget in main window
12 F1.grid(row=0, column=0, padx=3, sticky="EW") #Put LabelFrame widget in row 0 column 0
13
14 self.E1 = Entry(F1) #Create Entry widget (for date)
15 self.E1.grid(row=1, column=0, padx=15)
16 self.EG1 = self.E1.get()
17
18 self.E2 = Entry(F1) #Create Entry widget (for location)
19 self.E2.grid(row=1, column=1, padx=15)
20 self.EG2 = self.E2.get()
21
22 self.E3 = Entry(F1) #Create Entry widget (for distance)
23 self.E3.grid(row=1, column=2, padx=15)
24 self.EG3 = self.E3.get()
25
26 global E4
27 E4 = Listbox(F1,height=20, width=10) #Create Listbox widget (for run details output)
28 E4.grid(row=2, rowspan=4, columnspan=3, padx=15, pady=4, sticky="EW")
29 E4.bind("<<ListboxSelect>>",database.get_selected_row) #Call function get_selected_row when click on activity in listbox. Function returns the index (id) of the row.
30
31 B1 = Button(F1, text="Add entry", width=12, command=lambda: database.insert(self.EG1,self.EG2,self.EG3)) #Add button with label "Insert" which inserts the run details
32 B1.grid(row=1, column=4, padx=15, pady=3, sticky="W")
33
34 B2 = Button(F1, text="View all",width=12, command=database.view_all) #Add button with label "View all" which inserts the run details
35 B2.grid(row=2, column=4, padx=15, pady=3, sticky="WN")
36
37 window.mainloop()
38
39class Database(mainGUI):
40
41def __init__(self, db): #_init_ function creates database if it doesn't exist
42 self.conn = sqlite3.connect(db)
43 self.curs = self.conn.cursor()
44 self.curs.execute("CREATE TABLE IF NOT EXISTS diary (id INTEGER PRIMARY KEY, date TEXT, location TEXT, distance INTEGER)")
45 self.conn.commit()
46
47def get_variables(self): #This function gets strings and integers from Entry fields and then returns variables
48 date=E1.get()
49 location=E2.get()
50 distance=E3.get()
51 return date,location,distance
52
53def get_selected_row(self, event): #This function gets the index of the selected activity in listbox and then inserts Date,Location and Distance in appropriate entry widgets
54 try:
55 index=E4.curselection()[0]
56 self.selectedTuple=E4.get(index)
57 E1.delete(0,END)
58 E1.insert(END,self.selectedTuple[1])
59 E2.delete(0,END)
60 E2.insert(END,self.selectedTuple[2])
61 E3.delete(0,END)
62 E3.insert(END,self.selectedTuple[3])
63 except IndexError:
64 pass
65
66def insert(self,date,location,distance): #Adding activity. This function gets date,location and distance from entry widgets and then pass them as the arguments for the function add_entry. At the end it clears listbox and inserts the activity.
67 database.add_entry(date, location, distance)
68 E4.delete(0, END)
69 E4.insert(END,(date, location, distance))
70
71def add_entry(self,date,location,distance): #This function connects to the database and inserts new activity. It is called by insert() function.
72 self.curs.execute("INSERT INTO diary VALUES (NULL,?,?,?)", (date,location,distance))
73 self.conn.commit()
74
75def view_all(self): #This function connects to the database fetch all rows and shows them on the listbox.
76 E4.delete(0, END)
77 self.curs.execute("SELECT * FROM diary")
78 data = self.curs.fetchall()
79 for row in data:
80 E4.insert(END,row)
81 print(gui.var)
82
83def test(self,stri):
84 print(stri)
85
86def _del_(self):
87 self.curs.close()
88 self.conn.close()
89
90if __name__ == "__main__":
91 database = Database("database.db")
92 window=Tk()
93 gui = mainGUI(window)
94 database.test("pikele")