Basic Database Insert with Python Tkinter
- 9 years ago
A very basic programming tutorial in Python Tkinter database. I know I refer to another video, but you are really not missing much. It's a very similar video where all of the entry fields and buttons are on one screen, and this one has them separated out a little more.
Code:
#imports
from Tkinter import *
import sqlite3
#root setup
root = Tk()
root.title("Tkinter Sqlite 2nd")
root.minsize(width = 300, height = 300)
root.maxsize(width = 300, height = 300)
#create the database
db = sqlite3.connect('mydb100.db')
cursor = db.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS people(id INTEGER PRIMARY KEY, name TEXT, phone TEXT)")
db.commit()
db.close()
#the basic elements for the root window
list = Listbox(root) #the listbox that will get updated
name = StringVar()
phone = StringVar()
#the insert function
def insert():
name_i = name.get()
phone_i = phone.get()
conn = sqlite3.connect('mydb100.db')
with conn:
cursor = conn.cursor()
cursor.execute('INSERT INTO people(name, phone) VALUES(?,?)',(name_i, phone_i,))
lis = cursor.execute('SELECT name, phone FROM people')
for item in lis:
list.insert(END, item)
db.close()
#the insert window
def insert_window():
inwin = Toplevel()
inwin.title("Insert")
ne = Entry(inwin, textvariable = name)
pe = Entry(inwin, textvariable = phone)
button = Button(inwin, text = "Insert", command = insert)
ne.pack()
pe.pack()
button.pack()
#load the stuff
connt = sqlite3.connect('mydb100.db')
with connt:
cur = connt.cursor()
lit = cur.execute('SELECT name, phone FROM people')
for item in lit:
list.insert(0, item)
db.close()
#the insert button
ibutton = Button(root, text = "Launch Insert Window", command = insert_window)
#the main window
ibutton.pack()
list.pack()
root.mainloop()
Code:
#imports
from Tkinter import *
import sqlite3
#root setup
root = Tk()
root.title("Tkinter Sqlite 2nd")
root.minsize(width = 300, height = 300)
root.maxsize(width = 300, height = 300)
#create the database
db = sqlite3.connect('mydb100.db')
cursor = db.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS people(id INTEGER PRIMARY KEY, name TEXT, phone TEXT)")
db.commit()
db.close()
#the basic elements for the root window
list = Listbox(root) #the listbox that will get updated
name = StringVar()
phone = StringVar()
#the insert function
def insert():
name_i = name.get()
phone_i = phone.get()
conn = sqlite3.connect('mydb100.db')
with conn:
cursor = conn.cursor()
cursor.execute('INSERT INTO people(name, phone) VALUES(?,?)',(name_i, phone_i,))
lis = cursor.execute('SELECT name, phone FROM people')
for item in lis:
list.insert(END, item)
db.close()
#the insert window
def insert_window():
inwin = Toplevel()
inwin.title("Insert")
ne = Entry(inwin, textvariable = name)
pe = Entry(inwin, textvariable = phone)
button = Button(inwin, text = "Insert", command = insert)
ne.pack()
pe.pack()
button.pack()
#load the stuff
connt = sqlite3.connect('mydb100.db')
with connt:
cur = connt.cursor()
lit = cur.execute('SELECT name, phone FROM people')
for item in lit:
list.insert(0, item)
db.close()
#the insert button
ibutton = Button(root, text = "Launch Insert Window", command = insert_window)
#the main window
ibutton.pack()
list.pack()
root.mainloop()