Home > OS >  Why are the text of my buttons getting the same text of the last item of a CSV file?
Why are the text of my buttons getting the same text of the last item of a CSV file?

Time:06-29

So I have this CSV file:

Number of studs,Name
1,A
2,B
3,C
4,D
5,E
6,F
7,G
8,H
9,I
10,J
11,K
12,L
13,M
14,N
15,O
16,P
17,Q

And my code creates one button for each item in the number of studs column and the text gets the item in Name column respectively

here's the code that do this:

 def widget_creator():
        for i in df['Number of studs']:

            for n in df['Name']:
                
                row, col = divmod(i, 3)
                ct.CTkButton(new_frame, text= n, text_font = ('Montserrat', 15, 'bold'), corner_radius=10, fg_color=random.choice(colors), text_color='#FFFFFF').grid(row=row, column=col, pady=100, padx=50, ipadx = 100, ipady=130)

But now the problem is the buttons don't get the item in the Name column of their respective row but all the buttons get the last item in their text. Like the last item is Q in the Name column so every button has Q in their text instead of having the name in their respective row.

How can I fix this? Thanks

CodePudding user response:

I have not tested (no python available) but this should work:

def widget_creator():
    for i, n in enumerate(df['Name'], start=1):
        row, col = divmod(i, 3)
        ct.CTkButton(new_frame, text= n, text_font = ('Montserrat', 15, 'bold'), corner_radius=10, fg_color=random.choice(colors), text_color='#FFFFFF').grid(row=row, column=col, pady=100, padx=50, ipadx = 100, ipady=130)

EDIT : Maybe even better but still not tested

def widget_creator():
    for i, n in zip(df['Number of studs'],df['Name']):
        row, col = divmod(i, 3)
        ct.CTkButton(new_frame, text= n, text_font = ('Montserrat', 15, 'bold'), corner_radius=10, fg_color=random.choice(colors), text_color='#FFFFFF').grid(row=row, column=col, pady=100, padx=50, ipadx = 100, ipady=130)
  • Related