Bonjour,
Je souhaiterai utiliser une boucle pour créer un nombre déterminé de boutons qui appellent respectivement une fonction. Pour celà j'ai écris le code suivant
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import tkinter as tk
import numpy as np
 
fenetre = Tk()
fenetre.title("Calcul de réflectivité")
 
def test():
 
    truc = float(couche.get())+1
    nb_couche = round(truc)
    a = np.zeros(shape=(nb_couche-1,2))
    #print(a)
 
 
    for i in range(1,nb_couche):
        position_ligne=i+5
        global biz
        biz="bou"+str(i) #on appelle les boutons bou1, bou2,...boui
        print(biz)
 
     # Création du bouton matériau
 
        biz = Button(fenetre, text="materiau n°" +str(i),command = lambda i=i : fonction(i))
#        biz = Button(fenetre, text="materiau n°" +str(i),command = fonction)
        biz.grid(row=position_ligne, column=0,sticky=E, padx=5, pady=5)
#        bout1 = Button(fenetre, text='test',command = nb_couche)
#        bout1.grid(row=bat, column=0,sticky=E, padx=5, pady=5)
 
        # Création du texte 'épaisseur'
 
        epaiss=Entry(textvariable=thick,width=5,bg ='bisque', fg='maroon')
        epaiss.grid(row=position_ligne, column=2,sticky=W, padx=5, pady=5)
 
        Label6 = Label(fenetre, text = "thickness n°" +str(i))
        Label6.grid(row=position_ligne, column=1,sticky=E, padx=5, pady=5)
 
        # Création du texte 'indice'
 
        indice=Entry(textvariable="",width=5,bg ='bisque', fg='maroon')
        indice.grid(row=position_ligne, column=4,sticky=W, padx=5, pady=5)
 
        Label4 = Label(fenetre, text = "indice n°" +str(i))
        Label4.grid(row=position_ligne, column=3,sticky=E, padx=5, pady=5)
 
        # Création du texte 'coeff extinction'
 
        imagi=Entry(textvariable="",width=5,bg ='bisque', fg='maroon')
        imagi.grid(row=position_ligne, column=6,sticky=W, padx=5, pady=5)
 
        Label5 = Label(fenetre, text = "extinction n°" +str(i))
        Label5.grid(row=position_ligne, column=5,sticky=E, padx=5, pady=5)
 
def fonction(num):
 
    if bou[num].get()==1:
        print("ok")
 
thick=StringVar()
ncouche=StringVar()
 
bout = Button(fenetre, text='OK',command = test)
bout.grid(row=4, column=2,sticky=EW, padx=5, pady=5)
 
couche=Entry(textvariable=ncouche,width=5,bg ='bisque', fg='maroon')
couche.grid(row=4, column=1,sticky=W, padx=5, pady=5)
 
Label3 = Label(fenetre, text = "couche(s) ")
Label3.grid(row=4, column=0,sticky=E, padx=5, pady=5)
 
fenetre.mainloop()
Le principe est que lorsque je rentre un nombre N de couches, le code me crée N boutons dans mon interface tkinter nommés de 1 à N. Mon objectif est que lorsque je clique sur un bouton, il appelle une fonction qui me remplisse les champs vide se trouvant sur la même ligne que le bouton. Par exemple, si je clique sur le premier bouton "matériau" se trouvant à la ligne 6, il remplisse les champs de la ligne 6 et ainsi de suite pour les N boutons "matériau" créés. Il faudrait que la commande du bouton ainsi que la fonction associée dépendent de "i" mais je ne vois pas comment l'écrire. Merci