Basic Database Insert with Python Tkinter

Hosted by Dailymotion. For legal issues report at the Copyright Center, report us on DMC, or use the Instant Removal tool.

Basic Database Insert with Python Tkinter

M
M Carroll

1 Views • Sep 14, 2015

Description

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()