首页 > 解决方案 > 在 Tkinter GUI 中显示 SQLite 特定数据

问题描述

我是 Python 新手。我遇到了 Tkinter 和 Sqlite 的问题。我需要从“查找单词”框中的输入中获取数据库中的特定单词。

#create function to search a record
def wordSearch():
    conn = sqlite3.connect('mydata.db')
    c = conn.cursor()
    c.execute("SELECT * FROM information WHERE my_word = ?")

记录 = c.fetchall() 打印(记录)

#Loop thru results
print_records = ''
for record in records:
    print_records += str(record[0]) + " "+ str(record[1]) + " " + str(record[2])+'\n'

    conn.commit()
    conn.close()


#create text boxes
wordSearch_entry = Entry(root, width = 30)
wordSearch_entry.grid(row = 5, column = 1, pady = 5 )

#create text box labels

wordSearch_entry_label = Label(root, text = "Find word")
wordSearch_entry_label.grid(row = 5, column = 0, pady = 5)


btn_wordSearch = Button(root, text="Search word", command=wordSearch)
btn_wordSearch.grid(row = 6, column = 0, columnspan = 2, pady = 10, ipadx = 130)

标签: pythonsqlitetkinter

解决方案


尝试这个:

def wordSearch():
    conn = sqlite3.connect('mydata.db')
    c = conn.cursor()
    c = c.execute("SELECT * FROM information WHERE my_word = ?",[wordSearch_entry.get()])
    records = c.fetchall()
    print(records)

    #Loop thru results
    print_records = ''
    for record in records:
        print_records += str(record[0]) + " "+ str(record[1]) + " " + str(record[2])+'\n'
        # wordSearch_entry_label['text']=print_records <--- if you want to show your results on wordSearch_entry_label.

#create text boxes
wordSearch_entry = Entry(root, width = 30)
wordSearch_entry.grid(row = 5, column = 1, pady = 5 )

#create text box labels

wordSearch_entry_label = Label(root, text = "Find word")
wordSearch_entry_label.grid(row = 5, column = 0, pady = 5)


btn_wordSearch = Button(root, text="Search word", command=wordSearch)
btn_wordSearch.grid(row = 6, column = 0, columnspan = 2, pady = 10, ipadx = 130)

希望能帮助到你!


推荐阅读