The_Grey_Hood
The_Grey_Hood

Reputation: 9

how to place frames in tkinter to display the data in a table?

I need to display the data in a table using frames or grid in tkinter. I have displayed the data in the tkinter window but i want to place in a table, so can anyone help me with the code (and also the scroll bar)..

here is the code :

       def allClub():
            data=cursor.execute("SELECT * from CLUBS order by club_name")


            master = Tk()
            master.geometry('500x500')
            master.title('CLUBS')
            Label1 = Label(master, text="CLUB ID", width=10)
            Label1.grid(row=0, column=0)
            Label2 = Label(master, text="CLUB NAME", width=10)
            Label2.grid(row=0, column=1)
            Label3 = Label(master, text="RATING", width=10)
            Label3.grid(row=0, column=2)
            Label1 = Label(master, text="MANAGER", width=10)
            Label1.grid(row=0, column=3)
            Label1 = Label(master, text="CHAIRMAN", width=10)
            Label1.grid(row=0, column=4)
            Label1 = Label(master, text="LEAGUE", width=15)
            Label1.grid(row=0, column=5)
            Label1 = Label(master, text="TITLES", width=10)
            Label1.grid(row=0, column=6)
            Label1 = Label(master, text="YEAR FOUNDED", width=10)
            Label1.grid(row=0, column=7)


            for index, dat in enumerate(data):
                Label(master, text=dat[0]).grid(row=index+1, column=0)
                Label(master, text=dat[1]).grid(row=index+1, column=1)
                Label(master, text=dat[2]).grid(row=index+1, column=2)
                Label(master, text=dat[3]).grid(row=index+1, column=3)
                Label(master, text=dat[4]).grid(row=index+1, column=4)
                Label(master, text=dat[5]).grid(row=index+1, column=5)
                Label(master, text=dat[6]).grid(row=index+1, column=6)
                Label(master, text=dat[7]).grid(row=index+1, column=7)

link for screenshot of output window here: https://i.sstatic.net/zFymD.jpg

Upvotes: 0

Views: 12292

Answers (2)

Hoang Nguyen
Hoang Nguyen

Reputation: 1

# One way to make a table is to use a loop for the Entry class. 

import tkinter as tk

win=tk.Tk()
win.title('Tk GUI')

cols=['Col1','Col2','Col3']
data = [ ["val1", "val2", "val3"],
         ["asd1", "asd2", "asd3"],
         ["bbb1", "bbb3", "bbb4"],
         ["ccc1", "ccc3", "ccc4"],
         ["ddd1", "ddd3", "ddd4"],
         ["eee1", "eee3", "eee4"] ]
for y in range(len(data)+1):
    for x in range(len(cols)):
        if y==0:
            e=tk.Entry(font=('Consolas 8 bold'),bg='light blue',justify='center')
            e.grid(column=x, row=y)
            e.insert(0,cols[x])
        else:
            e=tk.Entry()
            e.grid(column=x, row=y)
            e.insert(0,data[y-1][x])
win.mainloop()

Upvotes: 0

Stevo Mitric
Stevo Mitric

Reputation: 1619

Tkinter doesn't have any "table" widget and if you are planning to have a lot of rows and columns, the best thing you could use is a Treeview or Listbox.

On the other hand you cant create a scrollbar for Frame because the documentation for that widget doesn't say it supports scrolling. There is a solution for this problem that involves creating a canvas and you can check it out here.

Here is an example of Treeview widget:

from tkinter import *
from tkinter import ttk

root = Tk()
root.geometry("500x200")

data = [ ["val1", "val2", "val3"],
         ["asd1", "asd2", "asd3"],
         ["bbb1", "bbb3", "bbb4"],
         ["ccc1", "ccc3", "ccc4"],
         ["ddd1", "ddd3", "ddd4"],
         ["eee1", "eee3", "eee4"] ]


frame = Frame(root)
frame.pack()

tree = ttk.Treeview(frame, columns = (1,2,3), height = 5, show = "headings")
tree.pack(side = 'left')

tree.heading(1, text="Column 1")
tree.heading(2, text="Column 2")
tree.heading(3, text="Column 3")

tree.column(1, width = 100)
tree.column(2, width = 100)
tree.column(3, width = 100)

scroll = ttk.Scrollbar(frame, orient="vertical", command=tree.yview)
scroll.pack(side = 'right', fill = 'y')

tree.configure(yscrollcommand=scroll.set)

for val in data:
    tree.insert('', 'end', values = (val[0], val[1], val[2]) )

root.mainloop()

Upvotes: 3

Related Questions