0

Is there a way to display data taken from my database in terms of table and that i can actually copy the individual contents of? using listbox and scrollbar currently i defined a function logs and gave looped thru it like this :

def logs():
    log = Toplevel(root)
    log.title('View all customers')
    con = mysql.connect(host='localhost', user='root',
                            password='*****', database='BOOK')
    c = con.cursor()

    c.execute("SELECT * FROM library")
    result = c.fetchall()

    for index, x in enumerate(result):
        num = 0
        for y in x:
            lookup_label = Label(log, text=y)
            lookup_label.grid(row=index+1, column=num)
            num +=1
    l1 = Label(log,text='ID',font=font_text)
    l2 = Label(log,text='S_ID',font=font_text)
    l3 = Label(log,text='S_NAME',font=font_text)
    l4 = Label(log,text='B_NAME',font=font_text)
    l5 = Label(log,text='Date Taken',font=font_text)
    l6 = Label(log,text='Date_Returned',font=font_text)
    l7 = Label(log,text='Status',font=font_text)
    btn_ext = Button(log,text='Exit',font=font_text,command=log.destroy,borderwidth=2,fg='#eb4d4b')
    l1.grid(row=0,column=0,padx=20)
    l2.grid(row=0,column=1,padx=20)
    l3.grid(row=0,column=2,padx=20)
    l4.grid(row=0,column=3,padx=20)
    l5.grid(row=0,column=4,padx=20)
    l6.grid(row=0,column=5,padx=20)
    l7.grid(row=0,column=6,padx=20)
    btn_ext.grid(row=index+2,columnspan=7,ipadx=540)

is there any alternatives to this if not listbox? to make tables? Thanks in advance :)

3
  • 1
    You can use ttk.Treeview. Commented May 12, 2020 at 4:31
  • how do i pack my data from db into it? Commented May 12, 2020 at 4:52
  • 1
    Use insert(). Commented May 12, 2020 at 4:56

1 Answer 1

2

You can use ttk.Treeview to show data from database:

import tkinter as tk
from tkinter import ttk
import mysql.connector as mysql

def logs():
    log = tk.Toplevel(root)
    log.transient(root)
    log.title('View all customers')

    # setup treeview
    columns = ('ID', 'S_ID', 'S_NAME', 'B_NAME', 'Date_Taken', 'Date_Returned', 'Status')
    tree = ttk.Treeview(log, height=20, columns=columns, show='headings')
    tree.grid(row=0, column=0, sticky='news')

    # setup columns attributes
    for col in columns:
        tree.heading(col, text=col)
        tree.column(col, width=100, anchor=tk.CENTER)

    # fetch data
    con = mysql.connect(host='localhost', user='root', password='******', database='BOOK')
    c = con.cursor()
    c.execute('SELECT * FROM library')

    # populate data to treeview
    for rec in c:
        tree.insert('', 'end', value=rec)

    # scrollbar
    sb = tk.Scrollbar(log, orient=tk.VERTICAL, command=tree.yview)
    sb.grid(row=0, column=1, sticky='ns')
    tree.config(yscrollcommand=sb.set)

    btn = tk.Button(log, text='Exit', command=log.destroy, width=20, bd=2, fg='#eb4d4b')
    btn.grid(row=1, column=0, columnspan=2)
Sign up to request clarification or add additional context in comments.

6 Comments

AttributeError: module 'tkinter.ttk' has no attribute 'BOTH' im getting dis error here, any idea?
Updated answer using import tkinter as tk instead of from tkinter import *.
works like a charm thanks :) is there a way to copy wahts inside this rows? add a scrollbar?
Updated answer with scrollbar. You can get the values of selected item by using tree.item(tree.focus())['values'].
tree.item(..) returns a dictionary of information for the current selected item. values is the key to get the item values from the dictionary. Detail information can refer to ttk.Treeview.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.