Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading