1

i use python 3, tkinter and mysql connector. I want to make an optionMenu object with my sql data i tried to do like that :

     mongroupe3 = LabelFrame(app, text='nouvelle div 3').pack()
     Label(mongroupe3, text='groupe3').pack()
     select_Classes = ("select id,nom from classe")
     cursor.execute(select_Classes)
     result = cursor.fetchall()
     for row in result:
        id = row[0]
        nom = row[1]
        mongroupe3 = Label(app, text=nom).pack()
        v = StringVar()
        v.set(nom)
        om = OptionMenu(mongroupe3, v, nom)
        om.pack()

the code has to show a dropdown menu with a list of the "nom" on my "classe" table but my option menu is in my for loop and is executed as many time as i have records in my classe table .

for exemple if i have 3 record i got 3 option menu with 3 different value.

o tried to put my optionMenu out of my for loop like that :

     mongroupe3 = LabelFrame(app, text='nouvelle div 3').pack()
     Label(mongroupe3, text='groupe3').pack()
     select_Classes = ("select id,nom from classe")
     cursor.execute(select_Classes)
     result = cursor.fetchall()
     for row in result:
        id = row[0]
        nom = row[1]
        mongroupe3 = Label(app, text=nom).pack()
        v = StringVar()
        v.set(nom)
     om = OptionMenu(mongroupe3, v, nom)
     om.pack()

but i got only the last record i inserted on my table. i cant put my for loop inside my OptionMenu cause of the following shema OptionMenu(parent, default variable, var)

1 Answer 1

1

You got what you did not expect for this reason:

You create only one label and you override it in each iteration (the same about the class variable v). So by the end, it is logic you get only one row (because, again, you have only one label widget).

Apart from this, you have to care about the widgets and where you place them: for example, I believe mongroup3 must be the parent widget of the row you want to create, unlike what you did.

Sign up to request clarification or add additional context in comments.

1 Comment

mongroup3 was just here to test my for loop and it return all my record of my table . If I understood correctly i need to put my for loop inside my option menu ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.