Bonjour ! Comment allez-vous?
Dans le cadre de mon projet ISN, je suis en train de coder un QCM (Module Tkinter), donc en Python 3.2.
J'ai réalisé ce programme :
from tkinter import *
import random
from tkinter.messagebox import *
############################ Les variables ###########
questions=["CH4?", "NH3?", "H2O?"] #Liste de questions
choix=[["CH4","NH3","H2O"],["CH4","NH3","H2O"],["CH4","NH3","H2O"]]#Liste de reponses (Une fois le programme fonctionnel, remplacer par des images).
reponses=[1,2,3] #Liste de bonnes reponses tapees dans l'Entry
############################ Les fonctions ###########
def easy():
if questions!=[]:
fr1.destroy()#Detruit la fenetre des difficultes
n=random.randint(0,len(questions)-1)#n choisit au hasard qui figure dans les 3 listes
affich_question(n)#Fonction permettant d'afficher la question et les choix de reponses
else:
showinfo("Fin du jeu","Votre score est")
fen.destroy()
def affich_question(pi) :
Q=questions[pi]#Affiche la question n
fr2=Frame(fen,bd=2, relief=SOLID, bg = "sky blue")
l1=Label(fr2, bd=2, text = "Question :", relief=SOLID, bg = "red")
l2=Label(fr2, bd=2, text = str(Q), relief=SOLID, bg = "red")
l3=Label(fr2, bd=2,text = "Indiquez le numero de votre reponse", relief=GROOVE, bg = "bisque")
l3.grid(row=3, column=0)
R= StringVar()
en=Entry(fr2, textvariable = R, bg ='bisque', fg='maroon')
en.focus_set()
en.grid(row=3, column=1)
b4=Button(fr2, bd=2, text="Valider", width=15, command=lambda:reponse(pi,R))
b4.grid(row=4, column=1)
l2.grid(row=1, column=1)
l1.grid(row=1, column=0)
fr2.grid(row=3, column=1)
affich_reponse(pi)
def affich_reponse(oo):
adresse="H:\Spe\Projet ISN pour le bac\Programmation python\i"+str(choix[oo][0])+".ppm"
img = PhotoImage(file=adresse)
can.create_image(100, 150, image=img)
adresse1="H:\Spe\Projet ISN pour le bac\Programmation python\i"+str(choix[oo][1])+".ppm"
img1 = PhotoImage(file=adresse1) #Affiche les 3 images possibles dans le canevas
can.create_image(250, 150, image=img1)
adresse2="H:\Spe\Projet ISN pour le bac\Programmation python\i"+str(choix[oo][2])+".ppm"
img2 = PhotoImage(file=adresse2)
can.create_image(400, 150, image=img2)
can.create_text(100, 250, font='ComicSansMS 16', text='-1-')
can.create_text(250, 250, font='ComicSansMS 16', text='-2-')
can.create_text(400, 250, font='ComicSansMS 16', text='-3-')
def reponse(num,a):
if a.get == reponses[num]:
showinfo("Resultat","Reponse juste")
S=S+1
else :
showwarning("Resultat","Reponse fausse")
questions.pop(num)
choix.pop(num)
reponses.pop(num)
easy()
########################## Fenetre Tkinter ######
fen=Tk()
fen.title('Molecule Project')
fen.configure(background="blue")
fr1 = Frame(fen,bd =2, relief=GROOVE, bg = "sky blue")
b1 = Button(fr1,text="Mode Facile",font="Algerian 16",width =15, command=easy)
b1.grid(row=0, column=1)
b2 = Button(fr1,text="Mode Normal", font="Algerian 16",width =15, command="")
b2.grid(row=0, column=2)
b3 = Button(fr1,text="Mode Difficile", font="Algerian 16",width =15, command="")
b3.grid(row=0, column=3)
fr1.grid(row=0, column=1)
can = Canvas(fen, bg="cyan", width=500, height=400, bd=2, relief=SOLID)
can.grid(row=3, column=4)
fr3 = Frame(fen, bd=2, relief=SOLID, bg = "white")
score = Label(fr3, text="Score = "+str(S), width=10, font="TimesNewRoman 14")
score.grid(row=3, column=4)
fr3.grid(row=4, column=4)
b5 = Button(fen,bd =2, text="Quitter", font="Algerian 16",width =15, command = fen.destroy)
b5.grid(row=4, column=5)
fen.mainloop()
Et le résultat : les images ne s'affichent pas sur le canevas, mais les numéros -1- -2- et -3- s'affichent.
Donc j'ai décidé de tester un programme plus simple afin de chercher l'erreur :
from tkinter import *
fen=Tk()
fen.title('Molecule Project')
fen.configure(background="blue")
can = Canvas(fen, bg="cyan", width=500, height=400, bd=2, relief=SOLID)
can.grid(row=3, column=4)
adresse="H:\Spe\Projet ISN pour le bac\Programmation python\iH2O.ppm"
img = PhotoImage(file=adresse)
can.create_image(100, 150, image=img)
adresse1="H:\Spe\Projet ISN pour le bac\Programmation python\iCH4.ppm"
img1 = PhotoImage(file=adresse1) #Affiche les 3 choix possibles dans le canevas
can.create_image(250, 150, image=img1)
adresse2="H:\Spe\Projet ISN pour le bac\Programmation python\iNH3.ppm"
img2 = PhotoImage(file=adresse2)
can.create_image(400, 150, image=img2)
can.create_text(100, 250, font='ComicSansMS 16', text='-1-')
can.create_text(250, 250, font='ComicSansMS 16', text='-2-')
can.create_text(400, 250, font='ComicSansMS 16', text='-3-')
fen.mainloop()
Et là, les images étaient bien sur le canevas... bizarrement...
J'ai un autre programme qui, une fois que j'enlevais la fonction lambda du bouton Valider, m'affichait ces images, la fonction lambda serait-elle le problème?
Excusez-moi de vous déranger pendant la période des vacances... :oops:
Merci pour votre coopération ;)