在Tkinter GUI中显示SQLite数据

时间:2019-03-10 20:50:41

标签: database sqlite user-interface tkinter

因此,我有一个非常简单的Tkinter GUI,它使用输入参数并将其输入到SQLite数据库中。我正在寻找创建一个辅助GUI,它将从SQLite数据库中提取此参数并将其显示在辅助GUI上。您能帮忙怎么做吗?最好是我想在文本字段或类似的东西上显示来自DB的数据。

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

1 个答案:

答案 0 :(得分:0)

在read_from_db函数中,您可以打印一个标签,而不是打印数据值:

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