[英]Python Tinker Sqlite3 Retrieve Data and Show on Tkinter GUI
I have a database
with various tool components.我有一个包含各种工具组件的
database
。 I also have a page with fields that I would like to populate with data from the database
.我还有一个页面,其中包含我想用
database
中的数据填充的字段。 I have looked at a lot of docs for both Tkinter
and Sqlite3
, and can't figure out how to do this.我查看了
Tkinter
和Sqlite3
的很多文档,但不知道如何做到这一点。 It seems like it would be a simple thing to do, but nothing I try has worked.看起来这将是一件简单的事情,但我尝试过的任何事情都没有奏效。 The code for the window I would like to populate is below.
我想填充的 window 的代码如下。 Can someone please show me an example of how I could get specific
table data
from the database
to populate these fields?有人可以向我展示一个如何从
database
中获取特定table data
来填充这些字段的示例吗? Thanks in advance.提前致谢。
Here is my code so far:到目前为止,这是我的代码:
from tkinter import *
import sqlite3
data = Tk()
data.title("WellSav | Data")
data.geometry("1400x800")
conn = sqlite3.connect('well_sav.db')
c = conn.cursor()
# Tools Frame Insert Data Into Fields
tools_frame = LabelFrame(data, text="Tools Data", padx=5, pady=5)
tools_frame.grid(row=0, column=0, padx=10, pady=10)
tools_type_label = Label(tools_frame, text="Tool Type")
tools_type_label.grid(row=0, column=0,)
tools_type_entry = Entry(tools_frame)
tools_type_entry.grid(row=0, column=1)
tools_length_label = Label(tools_frame, text="Tool Length")
tools_length_label.grid(row=1, column=0)
tools_length_entry = Entry(tools_frame)
tools_length_entry.grid(row=1, column=1)
tools_inside_diameter_label = Label(tools_frame, text="Tool ID")
tools_inside_diameter_label.grid(row=2, column=0)
tools_inside_diameter_entry = Entry(tools_frame)
tools_inside_diameter_entry.grid(row=2, column=1)
tools_outside_diameter_label = Label(tools_frame, text="Tool OD")
tools_outside_diameter_label.grid(row=3, column=0)
tools_inside_diameter_entry = Entry(tools_frame)
tools_inside_diameter_entry.grid(row=3, column=1)
data.mainloop()
A ttk.Treeview
without the tree part can be used to display a table:没有树部分的
ttk.Treeview
可用于显示表格:
tree = ttk.Treeview(master, columns=('Position', 'Name', 'Score'), show='headings')
Then set the column labels with然后设置列标签
tree.heading(<column>, text="Label")
and add rows with并添加行
tree.insert("", "end", values=(<position>, <name>, <score>))
The first argument is the item's parent, since you want a table, all items have the same parent, the root ""
.第一个参数是项目的父项,因为您想要一个表,所有项目都具有相同的父项,即根
""
。 The second argument is the position of the new item in the tree.第二个参数是树中新项目的 position。
Full example:完整示例:
import tkinter as tk
from tkinter import ttk
import sqlite3
def show():
cr=sqlite3.connect('database_name')
c.cursor()
c.execute("select * from table_name")
tempList = c.fetchall()
tempList.sort(key=lambda e: e[1], reverse=True)
for i, (name, score) in enumerate(tempList, start=1):
listBox.insert("", "end", values=(i, name, score))
scores = tk.Tk()
label = tk.Label(scores, text="High Scores", font=("Arial",30)).grid(row=0, columnspan=3)
# create Treeview with 3 columns
cols = ('Position', 'Name', 'Score')
listBox = ttk.Treeview(scores, columns=cols, show='headings')
# set column headings
for col in cols:
listBox.heading(col, text=col)
listBox.grid(row=1, column=0, columnspan=2)
showScores = tk.Button(scores, text="Show scores", width=15, command=show).grid(row=4, column=0)
closeButton = tk.Button(scores, text="Close", width=15, command=exit).grid(row=4, column=1)
scores.mainloop()
You can find more details about the Treeview
widget here .您可以在此处找到有关
Treeview
小部件的更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.