Noah Park
Noah Park

Reputation: 11

How to fix a listing problem with CSV on Tkinter

I need help with making a set of buttons where the first button would say Camera 1 all the way to the seventh button which would say Camera 7. From what I see in my code, all 7 buttons just take the last line of the CSV file which is Camera 7. Is there a way where I can have each button have its distinctive name?

I tried changing around the list in line 24, but I'm not sure how

This is my python code:

import tkinter.messagebox
root = Tk()
root.title("Video Equipment Reservation System")

infile = open('CamCSV.txt','r')
for line in infile:
    data = line.split(',')
    button = list()
    for i in range(7):
        button.append(Button(text=data[1], background=data[2]))
        button[i].grid(row=0,column=i)

and this is my CSV file

cam,Camera 2,green,0
cam,Camera 3,green,0
cam,Camera 4,green,0
cam,Camera 5,green,0
cam,Camera 6,green,0
cam,Camera 7,red,0

Each button should be different.

Upvotes: 0

Views: 45

Answers (1)

minterm
minterm

Reputation: 258

Delete the second for loop. You read the first line, then you make 7 buttons with the information from that first line. Each time you read a new line, you rewrite those 7 buttons. Instead, make one button each time you read a line.

button = list()
i = 0
for line in infile:
    data = line.split(',')
    button.append(Button(text=data[1], background=data[2]))
    button[i].grid(row=0,column=i)
    i += 1

Upvotes: 1

Related Questions