2

I'm trying to figure out how to display a .csv file in tkinter's grid, but haven't found much online.

Here is how far I got.

import tkinter


root = tkinter.Tk()

for r in range(3):
    for c in range(4):
          tkinter.Label(root, text='R%s/C%s'%(r,c),borderwidth=1 ).grid(row=r,column=c)

root.mainloop()   

How would I read a .csv file using the same approach?

nbro
  • 15,395
  • 32
  • 113
  • 196
Ted Heath
  • 370
  • 2
  • 5
  • 12
  • Have you done any research? A simple search on "python csv" should be enough to give you plenty of information about how to read a csv file. – Bryan Oakley Mar 12 '17 at 15:31
  • Yes I was okay with the python csv it was the tkinter grid that got my a little puzzled. – Ted Heath Mar 12 '17 at 15:53

2 Answers2

10

You can use reader from the python csv module to read the file. Reader takes a .csv file as input, and can then be iterated over like a table. I've included code, a sample .csv file, and my result.

Code:

import tkinter
import csv

root = tkinter.Tk()

# open file
with open("test.csv", newline="") as file:
    reader = csv.reader(file)

    # r and c tell us where to grid the labels
    for r, col in enumerate(reader):
        for c, row in enumerate(col):
            # i've added some styling
            label = tkinter.Label(
                root, width=10, height=2, text=row, relief=tkinter.RIDGE
            )
            label.grid(row=r, column=c)

root.mainloop()

CSV File:

col1,col2,col3
thing1,thing2,thing3
hi,hey,hello

Result:

J369
  • 436
  • 7
  • 12
3

Instead of Label use treeview like this, it will be more efficient.

from tkinter import *
import tkinter.ttk as ttk
import csv

root = Tk()
root.title("Python - Import CSV File To Tkinter Table")
width = 500
height = 400
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width / 2) - (width / 2)
y = (screen_height / 2) - (height / 2)
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
root.resizable(0, 0)

TableMargin = Frame(root, width=500)
TableMargin.pack(side=TOP)

scrollbarx = Scrollbar(TableMargin, orient=HORIZONTAL)
scrollbary = Scrollbar(TableMargin, orient=VERTICAL)
tree = ttk.Treeview(TableMargin, columns=("Employye ID", "Name", "Date",'Registration Time'), height=400, selectmode="extended",
                    yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
scrollbary.config(command=tree.yview)
scrollbary.pack(side=RIGHT, fill=Y)
scrollbarx.config(command=tree.xview)
scrollbarx.pack(side=BOTTOM, fill=X)
tree.heading('Employye ID', text="Employye ID", anchor=W)
tree.heading('Name', text="Name", anchor=W)
tree.heading('Date', text="Date", anchor=W)
tree.heading('Registration Time', text="Time", anchor=W)
tree.column('#0', stretch=NO, minwidth=0, width=0)
tree.column('#1', stretch=NO, minwidth=0, width=120)
tree.column('#2', stretch=NO, minwidth=0, width=120)
tree.column('#3', stretch=NO, minwidth=0, width=120)
tree.column('#3', stretch=NO, minwidth=0, width=120)
tree.pack()
with open('./RegisteredEmployees/RegisteredEmployees.csv') as f:
  reader = csv.DictReader(f, delimiter=',')
  for row in reader:
    emp_id = row['Employye ID']
    name = row['Name']
    dt = row['Date']
    ti = row['Registration Time']
    tree.insert("", 0, values=(emp_id, name, dt,ti))
root.mainloop()

I just initialize Treeview, the with csv module I read csv and insert column and rows data in treeview.

Result See the result here

Kushal Bhavsar
  • 388
  • 3
  • 6