0

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.stack.imgur.com/zFymD.jpg

j_4321
  • 15,431
  • 3
  • 34
  • 61
The_Grey_Hood
  • 9
  • 1
  • 2
  • 2
  • It appears your data is already in a table. What is wrong with what you have now? – Bryan Oakley Nov 22 '17 at 14:17
  • I imported data from sqlite3, well i wanted place data in a box like thing(just like how to place button,which have fixed dimension and label displays on it) – The_Grey_Hood Nov 23 '17 at 03:16
  • I still don't understand what you want. You ask for a "table". A common definition of "table" is "rows and columns", which you already have. How is a table different than the rows and columns you have now? – Bryan Oakley Nov 23 '17 at 04:08
  • What i meant to say was, I want the data to be displayed in an excel spreadsheet type border. How can I get such a structure? – The_Grey_Hood Nov 23 '17 at 05:11

2 Answers2

3

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()
Stevo Mitric
  • 1,570
  • 18
  • 19
0
# 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()