0

I made a quiz in Tkinter for school.

To anwser a question you select a radibutton, but when you click 3 times on the good radiobutton it will add 3 points instead of 1...

Can anyone help me?

Thanks all!

I tried something else with variable and value, but it get in trouble with global score...

Q1 = Label(fenetre2, text="1) Comment se nomme le président français?", font='Helvetica 10 bold')
Q1.pack()
Q1A = Radiobutton(fenetre2, text="Nicolas Sarkozy", value=2, variable=rep1)
Q1A.pack()
Q1B = Radiobutton(fenetre2, text="Emmanuel Macron", command=ajouter1, value=1, variable=rep1)
Q1B.pack()
Q1C = Radiobutton(fenetre2, text="François Hollande", value= 3, variable=rep1)
Q1C.pack()

def ajouter1():
    global score
    score+=1
    Score = "Bravo, Votre score est de: " + str(score) + "/10"
    var_label.set(Score)

Score = Label(fenetre3, textvariable=var_label, font='Helvetica 10 bold')
var_label.set("Bravo, Votre score est de: 0/10")
Score.pack()    
Saad
  • 3,340
  • 2
  • 10
  • 32

1 Answers1

0

There can be multiple approaches to do what you want I would suggest you to add a Button which when pressed will check the answer and goes to the next page so there'll be no extra score issues though I assume you probably have one page and which have multiple questions so these are some ideas which can be used to solve your issue.

I assigned a list which will store the correct answer in the function ajouter1() and check if that answer is already in the list ( if ans not in score_list: ). Also passing an argument to the function by lambda: ajouter1("A1"). Here's how you pass parameters to the function in tkinter.

Example:

...

score_list = []

def ajouter1( ans ):
    global score
    if ans not in score_list:
        score+=1
        Score = "Bravo, Votre score est de: " + str(score) + "/10"
        var_label.set(Score)
        score_list.append(ans)

Q1 = Label(root, text="1) Comment se nomme le président français?", font='Helvetica 10 bold')
Q1.pack()
Q1A = Radiobutton(root, text="Nicolas Sarkozy", value=2, variable=rep1)
Q1A.pack()
Q1B = Radiobutton(root, text="Emmanuel Macron", value=1, variable=rep1, command=lambda: ajouter1('A1'))
Q1B.pack()
Q1C = Radiobutton(root, text="François Hollande", value= 3, variable=rep1)
Q1C.pack()

...

Another approach that I can think of is when a user clicks on any of the Radiobutton of a question then those Radiobutton's state goes disabled so user won't cheat.

Something like this:

enter image description here

def ajouter1( ans ):
    global score
    if ans:
        score+=1
    Score = "Bravo, Votre score est de: " + str(score) + "/10"
    var_label.set(Score)

    Q1A['state'] = 'disabled'
    Q1B['state'] = 'disabled'
    Q1C['state'] = 'disabled'

Q1 = Label(root, text="1) Comment se nomme le président français?", font='Helvetica 10 bold')
Q1.pack()
Q1A = Radiobutton(root, text="Nicolas Sarkozy", value=2, variable=rep1, command=lambda: ajouter1(False))
Q1A.pack()
Q1B = Radiobutton(root, text="Emmanuel Macron", value=1, variable=rep1, command=lambda: ajouter1(True))
Q1B.pack()
Q1C = Radiobutton(root, text="François Hollande", value= 3, variable=rep1, command=lambda: ajouter1(False))
Q1C.pack()
Saad
  • 3,340
  • 2
  • 10
  • 32