Search code examples
pythontkinterradio-button

Refreshing Radiobutton selection


I've created a quiz using tkinter and ive bumped into a small problem. After having chosen the answer for the first question and clicking submit, the same radiobutton is already selected from the previous question for the next question. Is there anyway of maybe refreshing the page so that there is no selected answer for the next question?

The image shows an answer being selected for the first question.

The image shows an answer being selected for the first question.

after having clicked submit, the same radio button choice is already selected.

after having clicked submit, the same radio button choice is already selected.

var =  IntVar()

ans1 = Radiobutton(root, text=answer1[count], variable=var, value=1,
                  command=sel)
ans2 = Radiobutton(root, text=answer2[count], variable=var, value=2,
                  command=sel)
ans3 = Radiobutton(root, text=answer3[count], variable=var, value=3,
                  command=sel)
ans4 = Radiobutton(root, text=answer4[count], variable=var, value=4,
                  command=sel)

ans1.pack()
ans2.pack()
ans3.pack()
ans4.pack()

def out():
    global QuestionNo,correct,incorrect,s,count
    global ans1, ans3, ans3 ,ans4, correctans
    # count = count + 1

    answer = (ans1 or ans2 or ans3 or ans4(var.get()))

    print (question[QuestionNo])


    # print (answer[count])
    if count != 3:
          if answer  == correctans :
              count = count + 1
              QuestionNo = QuestionNo + 1
              # entry.delete(0, END)
              correct = correct + 1
              label.config(text = question[QuestionNo])
          else:
              QuestionNo = QuestionNo + 1
              count = count + 1
              # entry.delete(0, END)
              incorrect = incorrect + 1
              label.config(text = question[QuestionNo])

          # answer.delete(0, END)
    elif count == 3:
        # entry.delete(0, END)
        label.config(text = "Correct: "+str(correct) + " Incorrect:   "+str(incorrect))

    ans1.configure(text=str(answer1[count]))
    ans2.configure(text=str(answer2[count]))
    ans3.configure(text=str(answer3[count]))
    ans4.configure(text=str(answer4[count]))  

button = tk.Button(root,text = "Submit",command = out)
button.pack()

Solution

  • Use var.set(0) to reset selection.