python - 在 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)
解决方案
尝试这个:
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)
希望能帮助到你!
推荐阅读
- python - 如何在 Python 中运行 shell 命令?
- tensorflow - 如何使用 tensorflow 数据集训练 sklearn 模型?
- apache-kafka - 当 min.compaction.lag.ms 大于 log.cleaner.max.compaction.lag.ms 时会发生什么?
- typescript - TypeScript:使用没有类型变量的条件类型
- swift - 如何在 swift 中将类自定义初始化程序与协调器一起使用
- linux - 当我从服务器 scp 到名称时,我的文件夹在哪里复制
- mysql - 如何检索相同数据 ID 的最大值
- r - 使用向量根据名称选择多个列表元素
- file-upload - ReactPHP --> 文件上传 --> $request->getUploadedFiles() 返回空数组
- flutter - 如何将数组 id 转换为 json Flutter 中的列表元素