· 5 years ago · Sep 06, 2020, 12:42 AM
1from tkinter import *
2from tkinter import messagebox
3import sqlite3
4
5conn = sqlite3.connect('example.db')
6c = conn.cursor()
7# Create table
8c.execute('''CREATE TABLE IF NOT EXISTS stocks
9 (date text, trans text, symbol text, qty text, price text)''')#WAS REAL LAST TWO
10# We can also close the connection if we are done with it.
11conn.close()
12
13def delete_by_id():
14 x=ID.get()
15 conn = sqlite3.connect('example.db')
16 c = conn.cursor()
17 x=str(x)
18 c.execute('DELETE FROM stocks WHERE date=?', (x,))
19 conn.commit()
20 conn.close()
21 pass
22
23def delete_data(ag):
24 conn = sqlite3.connect('example.db')
25 c = conn.cursor()
26 c.execute('DELETE FROM stocks WHERE date=?', (ag))
27 conn.commit()
28 conn.close()
29 fr2.destroy()
30 pass
31
32def show_data():
33 tx.delete(1.0, END)
34 conn = sqlite3.connect('example.db')
35 c = conn.cursor()
36 #for row in c.execute('SELECT * FROM stocks'):#enumerate(iterable, start=0)
37 c.execute('SELECT * FROM stocks')
38 rows = c.fetchall()
39 for i, row in enumerate(rows, start=0):
40 #print(row, row[0],row[1],row[2],row[3],row[4])
41 column=0
42 tx.insert("%d.%d" % (i, column), f"{row[0]} \t\t\t\t{row[1]} \t\t\t\t{row[2]} \t\t\t\t{row[3]} \t\t\t\t{row[4]}\n")
43 conn.close()
44
45
46def insert_data():
47 x1=EONE.get()
48 x2=ETWO.get()
49 x3=ETHREE.get()
50 x4=EFOUR.get()
51 x5=EFIVE.get()
52 # Insert a row of data
53 conn = sqlite3.connect('example.db')
54 c = conn.cursor()
55 c.execute("INSERT INTO stocks (date, trans, symbol, qty, price) values (?,?,?,?,?)", (x1,x2,x3,x4,x5))
56 conn.commit()
57 conn.close()
58 #print(x1,x2,x3,x4,x5)
59 tx.insert(END, f"{x1} \t\t\t\t{x2} \t\t\t\t{x3} \t\t\t\t{x4} \t\t\t\t{x5}\n")
60 pass
61
62def ExitApplication():
63 MsgBox = messagebox.askquestion ('Exit Application','Are you sure you want to exit the application',icon = 'warning')
64 if MsgBox == 'yes':
65 root.destroy()
66 else:
67 messagebox.showinfo('Return','You will now return to the application screen')
68
69def delete_all():
70 conn = sqlite3.connect('example.db')
71 c = conn.cursor()
72 c.execute('DELETE FROM stocks;',)
73 conn.commit()
74 conn.close()
75 tx.delete(1.0, END)
76 pass
77
78def delete_all_records():
79 MsgBox = messagebox.askquestion ('Delete All Records','Are you sure you want to Delete All Records ?',icon = 'warning')
80 if MsgBox == 'yes':
81 delete_all()
82 else:
83 messagebox.showinfo('Return','You will now return to the application screen')
84
85root=Tk()
86root.title('Burpee')
87wid = root.winfo_screenwidth()-16
88hit = root.winfo_screenheight()-22
89lc='0+0'
90root.geometry(f'{wid}x{hit}+{lc}')
91
92
93fr=Frame(root, width=wid, height=50, bg='#000000')
94fr.pack(side=TOP, anchor=NW)
95fr.pack_propagate(False)
96lb=Label(fr, bg='#000000', fg='#ffffff', text='Robins Database Table')
97lb.pack(anchor=CENTER)
98
99EONE=StringVar()
100ETWO=StringVar()
101ETHREE=StringVar()
102EFOUR=StringVar()
103EFIVE=StringVar()
104ID=StringVar()
105
106txm=Text(root, width=180, height=1, fg='#0099ff', font=12)
107txm.pack()
108txm.insert(1.0, 'ID \t\t\t\tName \t\t\t\tBrand\
109 \t\t\t\tQty \t\t\t\tPrice')
110
111fr1=Frame(fr, width=wid, height=50, bg='#cecece')
112fr1.pack()
113e1=Entry(fr1, textvariable=EONE)
114e1.pack(side=LEFT)
115e2=Entry(fr1, textvariable=ETWO)
116e2.pack(side=LEFT)
117e3=Entry(fr1, textvariable=ETHREE)
118e3.pack(side=LEFT)
119e4=Entry(fr1, textvariable=EFOUR)
120e4.pack(side=LEFT)
121e5=Entry(fr1, textvariable=EFIVE)
122e5.pack(side=LEFT)
123
124b1=Button(fr1, text='Submit', command=insert_data)
125b1.pack(side=LEFT)
126
127e6=Entry(fr1, textvariable=ID)
128e6.pack(side=LEFT)
129b3=Button(fr1, text='Delete By Id', command=delete_by_id)
130b3.pack(side=LEFT)
131
132b2=Button(fr1, text='Refresh', command=show_data)
133b2.pack(side=LEFT)
134
135button1 = Button (fr1, text='Exit Application',command=ExitApplication,bg='brown',fg='white')
136button1.pack(side=LEFT)
137
138button2 = Button (fr1, text='Delete All Records',command=delete_all_records,bg='brown',fg='white')
139button2.pack(side=LEFT)
140
141tx=Text(root, width=180, font=10)
142tx.pack()
143
144show_data()
145
146root.mainloop()