Bonjour,

Je voudrais récupérer le texte d'un bouton lorsque j'y clique dessus. Voilà mon code :

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
71
72
from tkinter import *
 
master = Tk()
L = master.winfo_screenwidth()
H = master.winfo_screenheight()
 
master.minsize(L,H)
#master.geometry("320x600")
 
class maFrame(Frame):
 
    def __init__(self, parent, *args, **kw):
        Frame.__init__(self, parent, *args, **kw)
 
        # create a canvas object
        canvas = Canvas(self, bd=0, highlightthickness=0, bg="orange")
        canvas.pack(side=LEFT, fill=BOTH, expand=TRUE)
 
        # create a frame inside the canvas which will be scrolled with it
        self.interior = interior = Frame(canvas, bg="green")
        interior_id = canvas.create_window(0, 0, window=interior, anchor=NW)
 
        def _configure_canvas(event):
            if interior.winfo_reqwidth() != canvas.winfo_width():
                # update the inner frame's width to fill the canvas
                canvas.itemconfigure(interior_id, width=canvas.winfo_width())
        canvas.bind('<Configure>', _configure_canvas)
 
def OnButtonClick2():           
           #print(self.cget("text"))
           print("HOOOO!") 
 
 
def callback() :       
    maGrid()
 
 
if __name__ == '__main__':
 
    class maGrid(Tk):
 
        def __init__(self, *args, **kwargs):
 
           self.frame = maFrame(master)
           self.frame.pack(fill=BOTH, expand=TRUE)
 
           i = 0
           fonts = {'normal': 'arial 12','bold': 'arial 9 bold',}
           j = 0
           n = 0
 
           for mot in range(20) :                    
                    color = ['yellow', 'orange'][i % 2] 
                    myLabel = Button(self.frame.interior, text="XXX", borderwidth=10, name = "btn"+str(n), relief="sunken", fg="black", bg=color, command=self.OnButtonClick, font=fonts['normal'])#, width=taille)#anchor='ws', int(L/nbreElement)) #, 
                    myLabel.grid(row=i, column=j, padx=10, pady=10, sticky='ns') #, columnspan=29, padx=1)
                    j= j+1
                    n=n+1
                    self.frame.update()
                    pL = myLabel.winfo_rootx()+ 100# 100 étant la largeur maximale de la colonne
                    tR = master.winfo_width() + master.winfo_rootx()
                    if j>5 :
 
                        j=0
                        i = i+1
        def OnButtonClick(self):
                        print("AAA")
                        #print(self.cget("text"))
 
b = Button(master, text="OK", command=callback)
b.pack()
 
mainloop()
Si je décommente :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
print(self.cget("text"))
ça marche pas.

J'obtiens :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
return getattr(self.tk, attr)
[Previous line repeated 324 more times]
RecursionError: maximum recursion depth exceeded
Pourriez-vous m'aider svp? Merci d'avance.
Arsène