Home > other >  Display CSV with Tkinter
Display CSV with Tkinter

Time:02-10

For my assessment I am attempting to create a password manager and display a list of information into different columns and rows with tkinter. The issue is that the information doesn't stay in line and gets put lower than the pervious. This is making it so the information is not aligned correctly. I have attempted increasing per line and padding but this makes the issue worse.

Code:

from tkinter import *
import csv

global passScreen

# Create and set the GUI for the passScreen of the Password Manager.
passScreen = Tk()
passScreen.geometry("1200x800")
passScreen.resizable(width=False, height=False)
passScreen.title("Password")

Label(passScreen, text="Type").grid(row=3, column=1, padx=40)
Label(passScreen, text="Username").grid(row=3, column=2, padx=40)
Label(passScreen, text="Password").grid(row=3, column=3, padx=40)
Label(passScreen, text="Name").grid(row=3, column=4, padx=40)
Label(passScreen, text="Specific").grid(row=3, column=5, padx=40)
Label(passScreen, text="Date Created").grid(row=3, column=6, padx=40)
Label(passScreen, text="Date Updated").grid(row=3, column=7, padx=40)

passfile = open("PassManager.txt", "r")
read = csv.reader(passfile)
data = list(read)

entrieslist = []

i = 0
for entries in list(range(0, len(data))):
    entrieslist.append(data[entries][0])

    Label(passScreen, text=data[entries][1]).grid(rowspan=i   4, column=1)
    Label(passScreen, text=data[entries][2]).grid(rowspan=i   4, column=2)
    Label(passScreen, text=data[entries][3]).grid(rowspan=i   4, column=3)
    Label(passScreen, text=data[entries][4]).grid(rowspan=i   4, column=4)
    Label(passScreen, text=data[entries][5]).grid(rowspan=i   4, column=5)
    Label(passScreen, text=data[entries][6]).grid(rowspan=i   4, column=6)
    Label(passScreen, text=data[entries][7]).grid(rowspan=i   4, column=7)

passScreen.mainloop()

PasswordManager.txt:

Matt,Application,111111111,1111111,11111,N/A,2022-02-07 14:21:28.481304,2022-02-07 14:21:28.481304
Matt,Application,222,1111111,111122,N/A,2022-02-07 14:24:20.892781,2022-02-07 14:24:20.892781
Matt,Application,222,1111111,111122,N/A,2022-02-07 14:24:21.596781,2022-02-07 14:24:21.596781
Matt,Website,gsdgsd,1212,1243dsg,gsdgsd,2022-02-07 14:24:26.385282,2022-02-07 14:24:26.385282
Matt,Website,gsdgsd,1212,1243dsg,gsdgsd,2022-02-07 14:24:27.053781,2022-02-07 14:24:27.053781
Matt,Application,gsdgsdg,sdgsdg,gsdgsd,N/A,2022-02-07 14:24:31.437782,2022-02-07 14:24:31.437782
Matt,Game,sdgsdgsdgsd,sgsd,sdgsd,gsdgds,2022-02-07 14:24:36.802780,2022-02-07 14:24:36.802780
Matt,Game,sdgsdgsdgsd,sgsd,sdgsd,gsdgds,2022-02-07 14:24:36.802780,2022-02-07 14:24:36.802780

(I am sorry if my formatting for Stack overflow is incorrect!)

CodePudding user response:

You seem to be accessing the data "the hard way" because it can done simply in Python. I've fixed that and also fixed your display problems (I think). Don't know why you were using rowspan, since I don't think it is necessary — but again, I may not have understood what you how the display to look like (could only guess from from your somewhat convoluted code).

import csv
import tkinter as tk  # PEP 8 recommends against `import *`.

# Create and set the GUI for the passScreen of the Password Manager.
passScreen = tk.Tk()
passScreen.geometry("1200x800")
passScreen.resizable(width=False, height=False)
passScreen.title("Password")

col_names = ("Type", "Username", "Password", "Name", "Specific", "Date Created",
             "Date Updated")
for i, col_name in enumerate(col_names, start=1):
    tk.Label(passScreen, text=col_name).grid(row=3, column=i, padx=40)

with open("PassManager.txt", "r", newline="") as passfile:
    reader = csv.reader(passfile)
    data = list(reader)

entrieslist = []
for i, row in enumerate(data, start=4):
    entrieslist.append(row[0])
    for col in range(1, 8):
        tk.Label(passScreen, text=row[col]).grid(row=i, column=col)

passScreen.mainloop()

Result:

screenshot of result

  • Related