0

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

Alt_2003
  • 17
  • 3
  • 1
    https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas -> use this to iterate over the rows of your df, your inner loop here is the problem. Basically you create n buttons for each name and overwrite the other buttons again. At the end every button has the name of your last row because this is the final overwrite. – raphael_mav Jun 28 '22 at 15:14

1 Answers1

0

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)
Niko B
  • 751
  • 5
  • 9