Partage
  • Partager sur Facebook
  • Partager sur Twitter

Afficher le score d'un QCM sur tkinter

Afficher le score d'un QCM sur tkinter

    24 septembre 2022 à 15:23:43

    Bonjour, lorsque j'essaye d'afficher le score de mon qcm ( bonne ou mauvaise réponse ) il s'affiche seulement sur la première "fenêtre" ( celle ou je présente le qcm) et je voudrais qu'il s'affiche jusqu'a la fin en comptabilisant les bonne ou mauvaise réponse, voici mon code :

    from tkinter import *
    from tokenize import String
    
    def create():
    
        for c in racine.winfo_children():
            c.destroy()
    
        label3 = Label(racine, text = "L'amie écureuil de Bob l'éponge s'appelle :").grid(sticky="W")
        choix1 = Radiobutton(racine, text = "Cindy", variable = v, value = "Cindy").grid(sticky = "w")
        choix2 = Radiobutton(racine, text = "Mindy", variable = v, value = "Mindy").grid(sticky = "w")
        choix3 = Radiobutton(racine, text = "Sandy", variable = v, value = "Sandy").grid(sticky = "w")
    
        btn2 = Button(racine, text="Question suivante", command = q2).grid()
    def q2():
        for c in racine.winfo_children():
            c.destroy()
    
        label3 = Label(racine, text = "En dormant, Patrick bave plus que Gary :").grid(sticky = "w")
        choix1 = Radiobutton(racine, text = "Vrai", variable = m, value = "Vrai").grid(sticky = "w")
        choix2 = Radiobutton(racine, text = "Faux", variable = m, value = "faux").grid(sticky = "w")
        choix3 = Radiobutton(racine, text = "Match nul", variable = m, value = "matchnul").grid(sticky = "w")
    
    
        btn3 = Button(racine, text="Question suivante", command = q3).grid()
    
    def q3():
        for c in racine.winfo_children():
            c.destroy()
    
        label3 = Label(racine, text = "Patrick est une étoile :").grid(sticky = "w")
        choix1 = Radiobutton(racine, text = "à 3 branches", variable = p, value = "3").grid(sticky = "w")
        choix2 = Radiobutton(racine, text = "à 5 branches", variable = p, value = "5").grid(sticky = "w")
        choix3 = Radiobutton(racine, text = "à 6 branches", variable = p, value = "6").grid(sticky = "w")
    
        btn4 = Button(racine, text="Quitter", command = racine.destroy).grid()
    
    racine = Tk()
    racine.title("QCM")
    racine.minsize( 200, 100)
    
    
    v = StringVar()
    m = StringVar()
    p = StringVar()
    
    label1 = Label(racine, text="Bienvenue dans ce QCM pour tester vos connaissances sur Bob L'éponge !!").grid(sticky = "w")
    label2 = Label(racine, text="Vous devez cochez la bonne réponse à chaque question").grid(sticky = "W")
    btn = Button(racine, text="C'est parti !", command = create)
    btn.grid()
    
    cannevasImg = Canvas(racine, width=50, height=63)
    photo = PhotoImage(file = "bob.png")
    image = cannevasImg.create_image(25, 31.5, image=photo)
    cannevasImg.grid(column = 1,row = 1)
    
    # Création d'un cadre pour afficher le score.
    cadreScore = Frame(racine, borderwidth=2, relief=GROOVE)
    # Affiche le cadreScore.
    cadreScore.grid(sticky="E", padx=10, pady=10)
    # Affiche le titre du cadreScore.
    Label(cadreScore, text="Score :").grid(padx=10, pady=10)
    # Création d'un cadre dans le "cadreScore" pour Afficher le nombre de bonne réponse.
    cadreVrai = Frame(cadreScore, bg="white", borderwidth=2, relief=GROOVE)
    # Affiche le cadreVrai.
    cadreVrai.grid(sticky="W", padx=10, pady=10)
    # Affiche le titre du cadreVrai.
    Label(cadreVrai, text="Bonne réponse(s) :", bg="white").grid(padx=10, pady=10)
    cpt1 = 0 # Compteur de bonnes réponses
    def inc1():
        val = infos['valeur']
        v += 1
        infos['valeur'] = val
        my_bonnrep.itemconfig(text, text=int(val))
    infos = {'valeur': cpt1, }
    my_bonnrep = Canvas(cadreVrai, width=20, height=20)
    my_bonnrep.grid()
    text = my_bonnrep.create_text((10, 10), text=cpt1)
    # Création d'un cadre dans le "cadreScore" pour afficher le nombre de mauvaise réponse.
    cadreFaux = Frame(cadreScore, bg="white", borderwidth=2, relief=GROOVE)
    # Affiche le cadreFaux.
    cadreFaux.grid(sticky="E", padx=10, pady=10)
    # Affiche le titre du cadreFaux.
    Label(cadreFaux, text="Mauvaise Réponse(s) :", bg="white").grid(padx=10, pady=10)
    cpt2 = 0 # Compteur de mauvaises réponses
    def inc2():
        val = infos['valeur']
        v += 1
        infos['valeur'] = val
        my_mauvrep.itemconfig(text, text=int(val))
    infos = {'valeur': cpt2, }
    my_mauvrep = Canvas(cadreFaux, width=20, height=20)
    my_mauvrep.grid()
    text = my_mauvrep.create_text((10, 10), text=cpt2)
    # Création d'un cadre dans le "cadreScore" pour afficher le pourcentage de bonne réponse.
    cadreSoit = Frame(cadreScore, bg="white", borderwidth=2, relief=GROOVE)
    cpt2 = 0 # Compteur de mauvaises réponses
    def inc2():
        val = infos['valeur']
        v += 1
        infos['valeur'] = val
        my_mauvrep.itemconfig(text, text=int(val))
    infos = {'valeur': cpt2, }
    my_mauvrep = Canvas(cadreSoit, width=20, height=20)
    my_mauvrep.grid()
    text = my_mauvrep.create_text((10, 10), text=cpt2)
    
    
    
    racine.mainloop()
    



    -
    Edité par OzgurAydin1 25 septembre 2022 à 13:53:16

    • Partager sur Facebook
    • Partager sur Twitter
      24 septembre 2022 à 17:28:32

      Bonjour,

      Le message qui suit est une réponse automatique activée par un membre de l'équipe. Les réponses automatiques leur permettent d'éviter d'avoir à répéter de nombreuses fois la même chose, ce qui leur fait gagner du temps et leur permet de s'occuper des sujets qui méritent plus d'attention.
      Nous sommes néanmoins ouverts et si vous avez une question ou une remarque, n'hésitez pas à contacter la personne en question par Message Privé.

      Pour plus d'informations, nous vous invitons à lire les règles générales du forum

      Merci de colorer votre code à l'aide du bouton Code

      Les forums d'Openclassrooms disposent d'une fonctionnalité permettant de colorer et mettre en forme les codes source afin de les rendre plus lisibles et faciles à manipuler par les intervenants. Pour cela, il faut utiliser le bouton Code de l'éditeur, choisir un des langages proposés et coller votre code dans la zone prévue. Si vous utilisez l'éditeur de messages en mode Markdown, il faut utiliser les balises <pre class="brush: python;">Votre code ici</pre>.

      Merci de modifier votre message d'origine en fonction.

      Liens conseillés

      • Partager sur Facebook
      • Partager sur Twitter
      Anonyme
        24 septembre 2022 à 23:19:03

        Salut !

        Je t'invite à utiliser un dictionnaire avec la forme :
        questions = {'Question n°1...':(("Réponse A", "Réponse B", "Réponse C"), "Bonne réponse"), 'Question n°2...': ... }


        Ensuite tu renommes les champs plutôt que de tout détruire :

        label.configure(text=question)
        choix1.configure(text=questions[question][0][0])
        choix2.configure(text=questions[question][0][1])
        choix3.configure(text=questions[question][0][2])

        Pour gérer les emplacements il faut reprendre du début avec un tuto !

        grid() s'utilise avec les arguments row et column

        • Partager sur Facebook
        • Partager sur Twitter
          25 septembre 2022 à 14:25:03

          J'avais pensé ErispoeLeNarvalo à pourquoi pas lors de cette ligne 
          for c in racine.winfo_children():
                  c.destroy()
          mettre un if, et que si ce n'est pas égale aux widgets de mon score qu'il les supprimes : 
          for c in racine.winfo_children():
                  if c != cadreFaux and cadreScore and cadreVrai:
                      c.destroy()

          mais ça ne marche toujours pas 

          (je ne peux pas utiliser ta methode car ca signifierai de tout refaire depuis le début et je n'ai pas le temps ^^')

          • Partager sur Facebook
          • Partager sur Twitter
          Anonyme
            25 septembre 2022 à 16:58:59

            if c != cadreFaux and cadreScore and cadreVrai: #Y aurait pas un soucis dans la condition ?
            • Partager sur Facebook
            • Partager sur Twitter
              25 septembre 2022 à 17:09:27

                  
                  for c in racine.winfo_children():
                      if c != score1 or score2 or score3:
                          c.destroy()

              euh si si j'ai confondus le or et le and, my bad. Mais même en corrigeant cela ça ne marche toujours pas 

              le code modifié :

              from tkinter import *
              from tokenize import String
              
              def create():
              
                  for c in racine.winfo_children():
                      if c != score1 or score2 or score3:
                          c.destroy()
              
                  label3 = Label(racine, text = "L'amie écureuil de Bob l'éponge s'appelle :").grid(sticky="W")
                  choix1 = Radiobutton(racine, text = "Cindy", variable = v, value = "Cindy").grid(sticky = "w")
                  choix2 = Radiobutton(racine, text = "Mindy", variable = v, value = "Mindy").grid(sticky = "w")
                  choix3 = Radiobutton(racine, text = "Sandy", variable = v, value = "Sandy").grid(sticky = "w")
                  
              
                  btn2 = Button(racine, text="Question suivante", command = q2).grid()
                  
                  
              def q2():
                  
                  for c in racine.winfo_children():
                      if c != score1 or score2 or score3:
                          c.destroy()
              
                  label3 = Label(racine, text = "En dormant, Patrick bave plus que Gary :").grid(sticky = "w")
                  choix1 = Radiobutton(racine, text = "Vrai", variable = m, value = "Vrai").grid(sticky = "w")
                  choix2 = Radiobutton(racine, text = "Faux", variable = m, value = "faux").grid(sticky = "w")
                  choix3 = Radiobutton(racine, text = "Match nul", variable = m, value = "matchnul").grid(sticky = "w")
              
              
                  btn3 = Button(racine, text="Question suivante", command = q3).grid()
              
              def q3():
                  
                  for c in racine.winfo_children():
                      if c != score1 or score2 or score3:
                          c.destroy()
              
                  label3 = Label(racine, text = "Patrick est une étoile :").grid(sticky = "w")
                  choix1 = Radiobutton(racine, text = "à 3 branches", variable = p, value = "3").grid(sticky = "w")
                  choix2 = Radiobutton(racine, text = "à 5 branches", variable = p, value = "5").grid(sticky = "w")
                  choix3 = Radiobutton(racine, text = "à 6 branches", variable = p, value = "6").grid(sticky = "w")
              
                  btn4 = Button(racine, text="Quitter", command = racine.destroy).grid()
              
              racine = Tk()
              racine.title("QCM")
              racine.minsize( 200, 100)
              
              
              v = StringVar()
              m = StringVar()
              p = StringVar()
              
              label1 = Label(racine, text="Bienvenue dans ce QCM pour tester vos connaissances sur Bob L'éponge !!").grid(sticky = "w")
              label2 = Label(racine, text="Vous devez cochez la bonne réponse à chaque question").grid(sticky = "W")
               
              btn = Button(racine, text="C'est parti !", command = create)
              btn.grid()
              
              cannevasImg = Canvas(racine, width=50, height=63)
              photo = PhotoImage(file = "bob.png")
              image = cannevasImg.create_image(25, 31.5, image=photo)
              cannevasImg.grid(column = 1,row = 1)
              
              # Création d'un cadre pour afficher le score.
              cadreScore = Frame(racine, borderwidth=2, relief=GROOVE).grid(sticky="E", padx=10, pady=10)  # Affiche le cadreScore.
              # Affiche le titre du cadreScore.
              score1 = Label(cadreScore, text="Score :").grid(padx=10, pady=10)
               
              # Création d'un cadre dans le "cadreScore" pour Afficher le nombre de bonne réponse.
              cadreVrai = Frame(cadreScore, bg="white", borderwidth=2, relief=GROOVE).grid(sticky="W", padx=10, pady=10)# Affiche le cadreVrai.
              
              # Affiche le titre du cadreVrai.
              score2 = Label(cadreVrai, text="Bonne réponse(s) :", bg="white").grid(padx=10, pady=10)
               
              cpt1 = 0 # Compteur de bonnes réponses
               
              def inc1():
                  val = infos['valeur']
                  v += 1
                  infos['valeur'] = val
               
                  my_bonnrep.itemconfig(text, text=int(val))
               
              infos = {'valeur': cpt1, }
               
              my_bonnrep = Canvas(cadreVrai, width=20, height=20)
              my_bonnrep.grid()
              text = my_bonnrep.create_text((10, 10), text=cpt1)
               
              # Création d'un cadre dans le "cadreScore" pour afficher le nombre de mauvaise réponse.
              cadreFaux = Frame(cadreScore, bg="white", borderwidth=2, relief=GROOVE).grid(sticky="E", padx=10, pady=10)# Affiche le cadreFaux.
              # Affiche le titre du cadreFaux.
              score3 = Label(cadreFaux, text="Mauvaise Réponse(s) :", bg="white").grid(padx=10, pady=10)
               
              cpt2 = 0 # Compteur de mauvaises réponses
               
              def inc2():
                  val = infos['valeur']
                  v += 1
                  infos['valeur'] = val
               
                  my_mauvrep.itemconfig(text, text=int(val))
               
              infos = {'valeur': cpt2, }
               
              my_mauvrep = Canvas(cadreFaux, width=20, height=20)
              my_mauvrep.grid()
              text = my_mauvrep.create_text((10, 10), text=cpt2)
               
              # Création d'un cadre dans le "cadreScore" pour afficher le pourcentage de bonne réponse.
              cadreSoit = Frame(cadreScore, bg="white", borderwidth=2, relief=GROOVE)
               
              cpt2 = 0 # Compteur de mauvaises réponses
               
              def inc2():
                  val = infos['valeur']
                  v += 1
                  infos['valeur'] = val
               
                  my_mauvrep.itemconfig(text, text=int(val))
               
              infos = {'valeur': cpt2, }
               
              my_mauvrep = Canvas(cadreSoit, width=20, height=20)
              my_mauvrep.grid()
              text = my_mauvrep.create_text((10, 10), text=cpt2)
              
              
              
              racine.mainloop()
              
              
              
              
              • Partager sur Facebook
              • Partager sur Twitter
              Anonyme
                25 septembre 2022 à 19:25:04

                Bonjour,

                inc2 est défini 2 fois:

                ligne 99 et ligne 117 ...

                text aussi:

                ligne 90 ligne 110 et ligne 128 ...

                -
                Edité par Phil_1857 25 septembre 2022 à 19:26:12

                • Partager sur Facebook
                • Partager sur Twitter

                Afficher le score d'un QCM sur 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.
                • Editeur
                • Markdown