Basic Database Insert with Python Tkinter

Basic Database Insert with Python Tkinter

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. br br Code: br br #imports br br from Tkinter import * br import sqlite3 br br #root setup br br root = Tk() br root.title("Tkinter Sqlite 2nd") br root.minsize(width = 300, height = 300) br root.maxsize(width = 300, height = 300) br br #create the database br br db = sqlite3.connect('mydb100.db') br cursor = db.cursor() br cursor.execute("CREATE TABLE IF NOT EXISTS people(id INTEGER PRIMARY KEY, name TEXT, phone TEXT)") br db.commit() br db.close() br br #the basic elements for the root window br br list = Listbox(root) #the listbox that will get updated br br name = StringVar() br phone = StringVar() br br #the insert function br br def insert(): br br namei = name.get() br phonei = phone.get() br br conn = sqlite3.connect('mydb100.db') br with conn: br cursor = conn.cursor() br cursor.execute('INSERT INTO people(name, phone) VALUES(?,?)',(namei, phonei,)) br lis = cursor.execute('SELECT name, phone FROM people') br for item in lis: br list.insert(END, item) br db.close() br br br #the insert window br br def insertwindow(): br br inwin = Toplevel() br inwin.title("Insert") br br ne = Entry(inwin, textvariable = name) br pe = Entry(inwin, textvariable = phone) br br button = Button(inwin, text = "Insert", command = insert) br br ne.pack() br pe.pack() br button.pack() br br #load the stuff br br connt = sqlite3.connect('mydb100.db') br with connt: br cur = connt.cursor() br lit = cur.execute('SELECT name, phone FROM people') br for item in lit: br list.insert(0, item) br db.close() br br #the insert button br br ibutton = Button(root, text = "Launch Insert Window", command = insertwindow) br br #the main window br br ibutton.pack() br list.pack() br root.


User: M Carroll

Views: 1

Uploaded: 2015-09-14

Duration: 04:10