1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| from tkinter import *
from PIL import ImageTk, Image
import sqlite3
import os.path
root = Tk()
root.title("Zbraaaa")
root.geometry("400x400") #size of the box we create
# Databases
# Create a database or to connect to one
Base_dir = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(Base_dir,"address_book.db")
conn = sqlite3.connect(db_path)
# Create cursor
c = conn.cursor()
# Create table
'''
c.execute("""CREATE TABLE adresses (
first_name text,
last_name text,
address text,
city text,
state text,
zipcode integer
)""")
'''
# Create submit function for database
def submit():
# Create a database or to connect to one
conn = sqlite3.connect(db_path)
# Create cursor
c = conn.cursor()
#Insert Into Table
c.execute("INSERT INTO addresses VALUES (:f_name, :l_name, :address, :city, :state, :zipcode)",
{
'f_name' :f_name.get(),
'l_name' :l_name.get(),
'address' :address.get(),
'city' :city.get(),
'state' :state.get(),
'zipcode' :zipcode.get()})
# Commit Changes
conn.commit()
# Close Connect
conn.close()
#Clear the Text Boxes
f_name.delete(0,END)
l_name.delete(0,END)
address.delete(0,END)
city.delete(0,END)
state.delete(0,END)
zipcode.delete(0,END)
# Create Query function
def query():
# Create a database or to connect to one
conn = sqlite3.connect("address_book.db")
# Create cursor
c = conn.cursor()
# Query the database
c.execute("SELECT * , OID FROM addresses")
records = c.fetchall() #fetchmany, fetchone, fetchall
print(records)
# Commit Changes
conn.commit()
# Close Connect
conn.close()
# Create Text Boxes
f_name =Entry(root,width=30)
f_name.grid(row=0,column=1,padx=20)
l_name =Entry(root,width=30)
l_name.grid(row=1,column=1,padx=20)
address =Entry(root,width=30)
address.grid(row=2,column=1,padx=20)
city =Entry(root,width=30)
city.grid(row=3,column=1,padx=20)
state =Entry(root,width=30)
state.grid(row=4,column=1,padx=20)
zipcode =Entry(root,width=30)
zipcode.grid(row=5,column=1,padx=20)
# Create Text Box Labels
f_name_label = Label(root,text="First Name")
f_name_label.grid(row=0,column=0)
l_name_label = Label(root,text="Last Name")
l_name_label.grid(row=1,column=0)
address_label = Label(root,text="Address")
address_label.grid(row=2,column=0)
city_label = Label(root,text="City")
city_label.grid(row=3,column=0)
state_label = Label(root,text="State")
state_label.grid(row=4,column=0)
zipcode_label = Label(root,text="Zipcode")
zipcode_label.grid(row=5,column=0)
# Create Submit Button
submit_btn = Button(root, text="Add record to Database",command=submit)
submit_btn.grid(row=6,column=0,columnspan=2,padx=10,pady=10,ipadx=100)
# Create a Query Button
query_btn = Button(root, text="Show Records", command=query)
query_btn.grid(row=7,column=0,columnspan=2, padx=10, pady=10,ipadx=137)
# Commit Changes
conn.commit()
# Close Connect
conn.close()
root.mainloop() |
Partager