Jack Donaldson
Jack Donaldson

Reputation: 1

Displaying SQLite data in a Tkinter GUI

So I have a very simply Tkinter GUI which takes an input parameter and inputs it into a SQLite database. I'm looking to create a secondary GUI which will extract this parameter from the SQLite database and display it on the secondary GUI. Can you please help on how to do this? Preferably I want to display this data from the DB on a text field or something of the like.

from Tkinter import *
from PIL import Image, ImageTk
import sqlite3

root = Tk()
root.wm_attributes('-fullscreen','true')
root.title("My Test GUI")


Fullname=StringVar()

conn = sqlite3.connect('Form.db')
cursor=conn.cursor()

def database():
    
    name1=Fullname.get()

    cursor.execute('CREATE TABLE IF NOT EXISTS Student (Fullname TEXT)')
    cursor.execute('INSERT INTO Student (FullName) VALUES(?)',(name1,))
    conn.commit()

def error():

    root1 = Toplevel(root)
    root1.geometry("150x90")
    root1.title("Warning")
    Label(root1, text = "All fields required", fg = "red").pack()
    
def read_from_db():
    cursor.execute('SELECT * FROM Student')
    data = cursor.fetchall()
    print(data)

label_0 = Label(root, text="My Test GUI",width=20,font=("bold", 20))
label_0.place(x=650,y=53)


label_1 = Label(root, text="Name",width=20,font=("bold", 10))
label_1.place(x=550,y=130)

entry_1 = Entry(root,textvar=Fullname)
entry_1.place(x=700,y=130)

Button(root, text='Submit',width=20,bg='brown',fg='white', command=database).place(x=650,y=380)

    root.mainloop()
    read_from_db()

Upvotes: 0

Views: 4522

Answers (1)

Jake Martisek
Jake Martisek

Reputation: 11

Within your read_from_db function, instead of printing the value of data you can make a label out of it:

def read_from_db():
    cursor.execute("SELECT *, oid FROM Student")
    data = c.fetchall()
    showData = ''
    for data in Student:
        showData += str(data) + "\n"

    dataLabel = Label(master, text=showData)
    playerLabel.grid(row=0, column=0)
    conn.commit()
    conn.close()

Upvotes: 1

Related Questions