【问题标题】:Displaying SQLite data in a Tkinter GUI在 Tkinter GUI 中显示 SQLite 数据
【发布时间】:2019-03-10 20:50:40
【问题描述】:

所以我有一个非常简单的 Tkinter GUI,它接受一个输入参数并将其输入到 SQLite 数据库中。我希望创建一个辅助 GUI,它将从 SQLite 数据库中提取此参数并将其显示在辅助 GUI 上。你能帮忙看看如何做到这一点吗?最好我想在文本字段或类似的东西上显示来自数据库的这些数据。

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

【问题讨论】:

    标签: database sqlite user-interface tkinter


    【解决方案1】:

    在您的 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-23
      • 1970-01-01
      • 1970-01-01
      • 2018-06-26
      相关资源
      最近更新 更多