简体   繁体   中英

Python Tinker Sqlite3 Retrieve Data and Show on Tkinter GUI

I have a database with various tool components. I also have a page with fields that I would like to populate with data from the database . I have looked at a lot of docs for both Tkinter and Sqlite3 , and can't figure out how to do this. 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. Can someone please show me an example of how I could get specific table data from the database to populate these fields? 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:

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM