Voici l'erreur que j'ai en compilant le code si dessous
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 1550, in __call__
return self.func(*args)
File "C:\Users\User\Documents\python\projet jeux\mdp_tkinter.py", line 13, in Verification
shifumi()
File "C:\Users\User\Documents\python\projet jeux\mdp_tkinter.py", line 127, in shifumi
label1 = Label(fenetre, image=rien)
File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2606, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2139, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage1" doesn't exist
from tkinter import *
from tkinter.messagebox import * # boîte de dialogue
from math import*
from random import randint
# ==== Fonction jeux shi fu mi ====
def Verification():
if Motdepasse.get() == 'jeux':
shifumi()
Mafenetre.destroy()
else:
# le mot de passe est incorrect : on affiche une boîte de dialogue
showwarning('Résultat','Mot de passe incorrect.\nVeuillez recommencer !')
Motdepasse.set('')
# ==== Variable global score ====
ton_score = 0
mon_score = 0
# ==== Création de la fenêtre principale (main window) ====
Mafenetre = Tk()
Mafenetre.title('Identification requise')
# ==== Création d'un widget Label (texte 'Mot de passe') ====
Label1 = Label(Mafenetre, text = 'Mot de passe ')
Label1.pack(side = LEFT, padx = 5, pady = 5)
# ==== Création d'un widget Entry (champ de saisie) ====
Motdepasse= StringVar()
Champ = Entry(Mafenetre, textvariable= Motdepasse, show='*', bg ='bisque', fg='maroon')
Champ.focus_set()
Champ.pack(side = LEFT, padx = 5, pady = 5)
# ==== Création d'un widget Button (bouton Valider) ====
Bouton = Button(Mafenetre, text ='Valider', command = Verification)
Bouton.pack(side = LEFT, padx = 5, pady = 5)
# ==== Fonction score====
def score(mon_coup,ton_coup):#1=pierre , 2 = papier 3 = ciseaux
global mon_score, ton_score#globals affin de facilité leur réutilisation
if mon_coup == 1 and ton_coup == 2:
ton_score += 1
elif mon_coup == 2 and ton_coup == 1:
mon_score += 1
elif mon_coup == 1 and ton_coup == 3:
mon_score += 1
elif mon_coup == 3 and ton_coup == 1:
ton_score += 1
elif mon_coup == 3 and ton_coup == 2:
mon_score += 1
elif mon_coup == 2 and ton_coup == 3:
ton_score += 1
# === test pour la victoire en 3 manche ===
if ((mon_score < 3)and (ton_score == 3)):
showwarning('Titre 2', 'vous avez gagnez')
reintialise()
if ((mon_score == 3)and (ton_score < 3)):
showwarning('Titre 2', 'vous avez perdu')
reintialise()
# ==== Fontcion coup machine ====
def jouer(coup):
global mon_score, tons_score, score1, score2
machine=randint(1,3)
score(machine,coup)
score1.configure(text=str(ton_score))
score2.configure(text=str(mon_score))
if (machine == 1):
label3.configure(image=pierre)
elif machine == 2:
label3.configure(image=papier)
else :
label3.configure(image=ciseaux)
# ==== Fonction jouer general ====
def jouer_pierre():
jouer(1)
label1.configure(image=pierre)
def jouer_papier():
jouer(2)
label1.configure(image=papier)
def jouer_ciseaux():
jouer(3)
label1.configure(image=ciseaux)
# ==== Fonction qui réinitialise les scores ====
def reintialise():
global mon_score, ton_score, score1, score2, label1,label3
ton_score = 0
mon_score = 0
score1.configure(text=str(ton_score))
score2.configure(text=str(mon_score))
label1.configure(image=rien)
label3.configure(image=rien)
def shifumi():
# ==== Variable global score ====
ton_score = 0
mon_score = 0
# ==== Fenetre graphique ====
fenetre = Tk()
fenetre.title("SHI FU MI")
# ==== Image ====
rien = PhotoImage(file ='rien.gif')
pierre = PhotoImage(file ='pierre.gif')
papier = PhotoImage(file ='papier.gif')
ciseaux = PhotoImage(file ='ciseaux.gif')
# ==== Création des labels ====
text1 = Label(fenetre, text ="Vous :" , font=("Helvetica",16))
text1.grid(row=0, column=0)
texte2 = Label(fenetre, text="Machine :", font=("Helvetica", 16))
texte2.grid(row=0, column=2)
texte = Label(fenetre, text="pour jouer cliquez sur une image")
texte.grid(row=3, columnspan=3, pady=5)
score1 = Label(fenetre, text="0", font=("Helvetica", 16))
score1.grid(row=1, column=0)
score2 = Label(fenetre, text="0", font=("Helvetica", 16))
score2.grid(row=1, column=2)
label1 = Label(fenetre, image=rien)
label1.grid(row=2, column=0)
label3 = Label(fenetre, image=rien)
label3.grid(row=2, column=2)
# ==== Button ====
bouton1 = Button(fenetre,command=jouer_pierre)
bouton1.configure(image=pierre)
bouton1.grid(row=4, column=0)
bouton2 = Button(fenetre,command=jouer_papier)
bouton2.configure(image=papier)
bouton2.grid(row=4, column=1)
bouton3 = Button(fenetre,command=jouer_ciseaux)
bouton3.configure(image=ciseaux)
bouton3.grid(row=4, column=2)
bouton4 = Button(fenetre,text='Recommencer',command=reintialise)
bouton4.grid(row=5, column=0, pady=10, sticky=E)
bouton5 = Button(fenetre,text='Quitter',command=fenetre.destroy)
bouton5.grid(row=5, column=2, pady=10, sticky=W)
# demarrage :
fenetre.mainloop()
Mafenetre.mainloop()
Je ne comprend pas pourquoi car le code ci dessous fonctionne :
from math import*
from tkinter import *
from random import randint
from tkinter.messagebox import *
# ==== Fonction score====
def score(mon_coup,ton_coup):#1=pierre , 2 = papier 3 = ciseaux
global mon_score, ton_score#globals affin de facilité leur réutilisation
if mon_coup == 1 and ton_coup == 2:
ton_score += 1
elif mon_coup == 2 and ton_coup == 1:
mon_score += 1
elif mon_coup == 1 and ton_coup == 3:
mon_score += 1
elif mon_coup == 3 and ton_coup == 1:
ton_score += 1
elif mon_coup == 3 and ton_coup == 2:
mon_score += 1
elif mon_coup == 2 and ton_coup == 3:
ton_score += 1
# === test pour la victoire en 3 manche ===
if ((mon_score < 3)and (ton_score == 3)):
showwarning('Titre 2', 'vous avez gagnez')
reintialise()
if ((mon_score == 3)and (ton_score < 3)):
showwarning('Titre 2', 'vous avez perdu')
reintialise()
# ==== Fontcion coup machine ====
def jouer(coup):
global mon_score, tons_score, score1, score2
machine=randint(1,3)
score(machine,coup)
score1.configure(text=str(ton_score))
score2.configure(text=str(mon_score))
if (machine == 1):
label3.configure(image=pierre)
elif machine == 2:
label3.configure(image=papier)
else :
label3.configure(image=ciseaux)
# ==== Fonction jouer general ====
def jouer_pierre():
jouer(1)
label1.configure(image=pierre)
def jouer_papier():
jouer(2)
label1.configure(image=papier)
def jouer_ciseaux():
jouer(3)
label1.configure(image=ciseaux)
# ==== Fonction qui réinitialise les scores ====
def reintialise():
global mon_score, ton_score, score1, score2, label1,label3
ton_score = 0
mon_score = 0
score1.configure(text=str(ton_score))
score2.configure(text=str(mon_score))
label1.configure(image=rien)
label3.configure(image=rien)
# ==== Variable global score ====
ton_score = 0
mon_score = 0
# ==== Fenetre graphique ====
fenetre = Tk()
fenetre.title("SHI FU MI")
# ==== Image ====
rien = PhotoImage(file ='rien.gif')
pierre = PhotoImage(file ='pierre.gif')
papier = PhotoImage(file ='papier.gif')
ciseaux = PhotoImage(file ='ciseaux.gif')
# ==== Création des labels ====
text1 = Label(fenetre, text ="Vous :" , font=("Helvetica",16))
text1.grid(row=0, column=0)
texte2 = Label(fenetre, text="Machine :", font=("Helvetica", 16))
texte2.grid(row=0, column=2)
texte = Label(fenetre, text="pour jouer cliquez sur une image")
texte.grid(row=3, columnspan=3, pady=5)
score1 = Label(fenetre, text="0", font=("Helvetica", 16))
score1.grid(row=1, column=0)
score2 = Label(fenetre, text="0", font=("Helvetica", 16))
score2.grid(row=1, column=2)
label1 = Label(fenetre, image=rien)
label1.grid(row=2, column=0)
label3 = Label(fenetre, image=rien)
label3.grid(row=2, column=2)
# ==== Button ====
bouton1 = Button(fenetre,command=jouer_pierre)
bouton1.configure(image=pierre)
bouton1.grid(row=4, column=0)
bouton2 = Button(fenetre,command=jouer_papier)
bouton2.configure(image=papier)
bouton2.grid(row=4, column=1)
bouton3 = Button(fenetre,command=jouer_ciseaux)
bouton3.configure(image=ciseaux)
bouton3.grid(row=4, column=2)
bouton4 = Button(fenetre,text='Recommencer',command=reintialise)
bouton4.grid(row=5, column=0, pady=10, sticky=E)
bouton5 = Button(fenetre,text='Quitter',command=fenetre.destroy)
bouton5.grid(row=5, column=2, pady=10, sticky=W)
# demarrage :
fenetre.mainloop()
Error tkinter
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.