Home > OS >  Csv to TKinter in Python 3.6
Csv to TKinter in Python 3.6

Time:03-04

I have a code that works perfectly fine:

import time
import csv
from os.path import exists

winner = input("What is player 1's first name?(placeholder)")
date = input("Enter date in dd/mm/yy format")
winscore = ("95") #place holder
fieldnames = ['Username', 'Score','Date']

if exists("Scores.csv"):
     with open('Scores.csv', 'a') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writerow({'Username': winner, 'Score': winscore,'Date': date})
else:
    # Note over here we are using 'w' because no file exists yet and
    # we need to write a new header
    with open('Scores.csv', 'w') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerow({'Username': winner, 'Score': winscore,'Date': date})

print("Saving..............")
time.sleep(5)
print("Saving complete. Thank you for playing ,Goodbye :)")

But the problem lies in this code:

from tkinter import *
import csv

global passScreen

passScreen = Tk()
passScreen.geometry("300x300")
passScreen.resizable(width=False, height=True)
passScreen.title("Scores")

Label(passScreen, text="Username").grid(row=3, column=1, padx=20)
Label(passScreen, text="Score").grid(row=3, column=2, padx=20)
Label(passScreen, text="Date").grid(row=3, column=2, padx=20)


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

entrieslist = []

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

     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][2]).grid(rowspan=i   4, column=3)

passScreen.mainloop()

The code above outputs something like this:(in tkinter)

Username     Date
Score
             Date
                     Date

And gives the error:

    entrieslist.append(data[entries][1])
 IndexError: list index out of range

It should output the contents of the csv the first shown code creates and writes in. This however is not the case, I cannot seem to find the issue.

CodePudding user response:

You're not setting the row for each label in the loop. Since you aren't setting the row, tkinter will automatically place the item on a new row.

The reason for the "list index out of range error" is probably due to you having blank lines in your csv file. data[entries] will be [], so data[entries[][1] throws an error. You need to make sure you have a non-empty list before trying to get a value out of it.

CodePudding user response:

It seems that this code have no value for the "in range" works

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

Consider always using "if" before it, and "raise Exception" after. This way your code wont "break".

note I didnt ready your code completly, bcuz its too big, consider just sending some lines next time.

  • Related