Bonjour,
j'ai quelques boutons alignés côte à côte. J'incrémente le padx des boutons jusqu'à ce que leur largeur additionnée soit égale à la largeur de la fenêtre.
Tout marche bien, j'obtiens ce que je veux.
Par contre, dans le code ci-dessous où j'ai 2 boutons l'un en dessous de l'autre, j'arrive pas, en cliquant sur l'un des boutons, à incrémenter le padx du 2ème bouton jusqu'à ce qu'il égale ou dépasse la largeur du premier bouton. Pourrier-vous m'aider svp? Je crois que c'est pas mainloop qu'il faut utiliser dans la boucle while.
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 from tkinter import * root=Tk() Buts={} myLargeurTot=[0] L = root.winfo_screenwidth() H = root.winfo_screenheight() root.minsize(L,H) n=11 def callback() : #print(str(myLargeurTot[1])) fin=0 while fin<1 : myPadx = str(Buts[0,0].cget("padx")) myPadx2=int(myPadx)+1 myWidth0=str(myLargeurTot[1]) myWidth=int(myWidth0) for r in range(1): for c in range(n): if myWidth<L : myWidth=myWidth-Buts[r,c].winfo_reqwidth() Buts[(r,c)].config(padx=myPadx2) myWidth=myWidth+Buts[r,c].winfo_reqwidth() myLargeurTot[1]=myWidth else : fin=1 break #root.mainloop() for r in range(1): for c in range(n): print(Buts[r,c].winfo_reqwidth()) for r in range(1): myWidth=0 for c in range(n): Buts[(r,c)]=Button(root,text='%s/%s'%(r,c),borderwidth=10, command=callback) Buts[r,c].grid(row=r,column=c) myWidth=myWidth+Buts[r,c].winfo_reqwidth() myLargeurTot.append(myWidth) root.mainloop()
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 from tkinter import * master = Tk() L = 1000#master.winfo_screenwidth() H = 260#master.winfo_screenheight() master.minsize(L,H) def callback() : largMax=180 maLargB=b.winfo_reqwidth() maLargC=c.winfo_reqwidth() myPad0 = str(c.cget("padx")) #print("myPad0 = " +str(myPad0)) while maLargC<maLargB : myPadx=int(myPad0)+1 c.config(padx=myPadx) #print("maLargC = " +str(maLargC)) mainloop() b = Button(master, text="Taille minimale à obtenir", width=60, command=callback) b.pack() c = Button(master, text="---", width=56, command=callback) c.pack() mainloop()
Partager