· 7 years ago · Nov 21, 2018, 08:06 PM
1import sqlite3
2from tkinter import *
3from tkinter import messagebox
4
5class DB:
6 def __init__(self):
7 self.conn = sqlite3.connect("automobiliai.db")
8 self.cur = self.conn.cursor()
9 self.cur.execute(
10 "CREATE TABLE IF NOT EXISTS automobiliai (id INTEGER PRIMARY KEY, marke TEXT, savininkas TEXT, numeris INTEGER)")
11 self.conn.commit()
12
13 def __del__(self):
14 self.conn.close()
15
16 def view(self):
17 self.cur.execute("SELECT * FROM automobiliai")
18 rows = self.cur.fetchall()
19 return rows
20
21 def insert(self, marke, savininkas, numeris):
22 self.cur.execute("INSERT INTO automobiliai VALUES (NULL,?,?,?)", (marke, savininkas, numeris))
23 self.conn.commit()
24 self.view()
25
26 def update(self, id, marke, savininkas, numeris):
27 self.cur.execute("UPDATE automobiliai SET marke=?, savininkas=?, numeris=? WHERE id=?", (marke, savininkas, numeris, id))
28 self.view()
29
30 def delete(self, id):
31 self.cur.execute("DELETE FROM automobiliai WHERE id=?", (id,))
32 self.conn.commit()
33 self.view()
34
35 def search(self, marke="", savininkas="", numeris=""):
36 self.cur.execute("SELECT * FROM automobiliai WHERE marke=? OR savininkas=? OR numeris=?", (marke, savininkas, numeris))
37 rows = self.cur.fetchall()
38 return rows
39
40db = DB()
41
42def get_selected_row(event):
43 global selected_tuple
44 index = list1.curselection()[0]
45 selected_tuple = list1.get(index)
46 e1.delete(0, END)
47 e1.insert(END, selected_tuple[1])
48 e2.delete(0, END)
49 e2.insert(END, selected_tuple[2])
50 e3.delete(0, END)
51 e3.insert(END, selected_tuple[3])
52
53
54def view_command():
55
56 list1.delete(0, END)
57 for row in db.view():
58 list1.insert(END, row)
59
60
61def search_command():
62 list1.delete(0, END)
63 for row in db.search(marke_text.get(), savininkas_text.get(), numeris_text.get()):
64 list1.insert(END, row)
65
66
67def add_command():
68 db.insert(marke_text.get(), savininkas_text.get(), numeris_text.get())
69 list1.delete(0, END)
70 list1.insert(END, (marke_text.get(), savininkas_text.get(), numeris_text.get()))
71
72
73def delete_command():
74 db.delete(selected_tuple[0])
75
76
77def update_command():
78 db.update(selected_tuple[0], marke_text.get(), savininkas_text.get(), numeris_text.get())
79
80
81window = Tk()
82
83window.title("automobiliai")
84
85
86def on_closing():
87 dd = db
88 if messagebox.askokcancel("Išeiti", "Ar tikrai norite išeiti?"):
89 window.destroy()
90 del dd
91
92window.protocol("WM_DELETE_WINDOW", on_closing) # palaiko langa kai uzdaroma
93
94l1 = Label(window, text="MarkÄ—")
95l1.grid(row=0, column=0)
96
97l2 = Label(window, text="Savininkas")
98l2.grid(row=0, column=2)
99
100l3 = Label(window, text="Valstybinis numeris")
101l3.grid(row=1, column=0)
102
103marke_text = StringVar()
104e1 = Entry(window, textvariable=marke_text)
105e1.grid(row=0, column=1)
106
107savininkas_text = StringVar()
108e2 = Entry(window, textvariable=savininkas_text)
109e2.grid(row=0, column=3)
110
111numeris_text = StringVar()
112e3 = Entry(window, textvariable=numeris_text)
113e3.grid(row=1, column=1)
114
115list1 = Listbox(window, height=6, width=35)
116list1.grid(row=2, column=0, rowspan=6, columnspan=2)
117
118sb1 = Scrollbar(window)
119sb1.grid(row=2, column=2, rowspan=6)
120
121list1.configure(yscrollcommand=sb1.set)
122sb1.configure(command=list1.yview)
123
124list1.bind('<<ListboxSelect>>', get_selected_row)
125
126b1 = Button(window, text="Visi automobiliai", width=17, command=view_command)
127b1.grid(row=2, column=3)
128
129b2 = Button(window, text="Ieškoti automobilio", width=17, command=search_command)
130b2.grid(row=3, column=3)
131
132b3 = Button(window, text="Prideti automobilį", width=17, command=add_command)
133b3.grid(row=4, column=3)
134
135b4 = Button(window, text="Atnaujinti pasirinkima", width=17, command=update_command)
136b4.grid(row=5, column=3)
137
138b5 = Button(window, text="Ištrinti pasirinkima", width=17, command=delete_command)
139b5.grid(row=6, column=3)
140
141b6 = Button(window, text="Uždaryti", width=17, command=window.destroy)
142b6.grid(row=7, column=3)
143
144window.mainloop()