Boujour,
je débute sur tkinter et sur python en général, et je rencontre un problème à propos de l'affichage des images sur les boutons tkinter.
J'ai écrit un code ressemblant à peu près à ça et ayant pour but de faire choisir des noms, chacun leur tour, à deux joueurs.
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import tkinter as tk
from random import *
from tkinter import *
 
compte = 0
 
# pour que l'ordre des joueurs soit aléatoire
rdm = randint(0, 1)
 
# le nom de mes images :
nom = ['ex1', 'ex2', 'ex3', 'ex4', 'ex5', 'ex6', 'ex7', 'ex8', 'ex9']  # en réalité il y en a un peu plus de 80
 
 
# pour prendre un nom au hasard :
def piocher(liste):
    nb_mots = len(liste)
    nb_choisi = randint(0, nb_mots - 1)
    mot_choisi = liste[nb_choisi]
    del liste[nb_choisi]
    print(mot_choisi)
    return mot_choisi
 
 
# pour "nettoyer" la fenêtre
def new():
    for widget in window.winfo_children():
        widget.pack_forget()
 
 
# le gros du code qui fait faire le choix entre deux noms
def programme():
    # pour enregistrer les noms choisis et supprimer le reste pour recommencer
    def choix():
        Iperso1.pack()
        Iperso2.pack()
        frame4.destroy()
        frame5.destroy()
        frame3.destroy()
        demarrer()
 
    global bgc, fgc
    perso1 = piocher(nom)
    perso2 = piocher(nom)
    iperso1 = PhotoImage(file=perso1 + '.gif')
    iperso2 = PhotoImage(file=perso2 + '.gif')
    if compte % 2 == rdm:
        bgc = '#A2B9FF'
        fgc = '#FF2D2D'
        joueur = tk.Label(window, text='J1', font=('Arial', 45), bg=bgc, fg=fgc)
    else:
        bgc = '#FFA2A2'
        fgc = '#2D61FF'
        joueur = tk.Label(window, text='J2', font=('Arial', 45), bg=bgc, fg=fgc)
    window.config(background=bgc)
    frame2 = tk.Frame(window, bg=bgc)
    frame3 = tk.Frame(window, bg=bgc)
    frame4 = tk.Frame(window, bg=bgc)
    frame5 = tk.Frame(window, bg=bgc)
    Iperso1 = tk.Label(frame3, image=iperso1, bg=bgc)
    Iperso2 = tk.Label(frame2, image=iperso2, bg=bgc)
    perso1B = tk.Button(frame4, image=iperso1, bg=bgc, fg=fgc, activebackground=bgc, activeforeground=fgc,
                        command=choix)
    perso2B = tk.Button(frame5, image=iperso2, bg=bgc, fg=fgc, activebackground=bgc, activeforeground=fgc,
                        command=choix)
    joueur.pack()
    perso1B.pack()
    perso2B.pack()
    frame4.pack(expand=YES, side=LEFT)
    frame5.pack(expand=YES, side=RIGHT)
    frame2.pack()
    frame3.pack()
    if compte == 3:
        frame3.destroy()
        frame4.destroy()
        frame5.destroy()
 
 
# pour répéter l'action précédente 3 fois puis permettre de tout relancer
def demarrer():
    new()
    global compte
    compte = compte + 1
    if compte < 3:
        programme()
    else:
        compte = 0
        frame6 = tk.Frame(bg=bgc)
        frame6.pack(expand=YES)
        relancer = tk.Button(frame6, text='Relancer', bg=bgc, activebackground=bgc, fg=fgc, activeforeground=fgc,
                             command=demarrer)
        relancer.pack()
        global nom
        nom = ['ex1', 'ex2', 'ex3', 'ex4', 'ex5', 'ex6', 'ex7', 'ex8', 'ex9']
 
 
window = tk.Tk()
window.config(background='#A2B9FF')
 
demarrerB = tk.Button(window, text='Démarrer', font=('Arial', 45), command=demarrer, bg='#A2B9FF', fg='#FF2E2E',
                      activebackground='#A2B9FF', activeforeground='#FF2E2E')
demarrerB.pack(expand=YES)
 
window.mainloop()

Le code est un peu plus long mais c'est sur cette partie que le problème intervient :
après que les boutons contenant les images soit placer avec .pack() , seul les contours sont affichés et les boutons ne semblent pas réagir lorsque je clique dessus.

De plus, je ne suis pas sûr que le global nom soit autorisé mais je n'ai pas pu tester puisque je n'arrive même pas à la deuxième boucle...


Merci d'avance pour vos réponses.