Hello im trying to create a questionnaire with 5 choises for each question. Since I have lots of questions, I wanted to create labels and radiobuttons in a loop. However, I can not call mainloop() function of tkinter in a loop so when I call it on the outside of the loop, my selection on the questionnaire of any question becomes the answer of the last question. How can I fix this?
def selected(value, question):
answer_values[question-1] = value
print(value, question)
root = Tk()
root.title('Kişilik Analiz Testi')
answers = {}
for x in range(0, enneagram.shape[0]):
soru = str(x+1) + ". " + enneagram.SORULAR[x] + " Cümlesi sizi ne kadar iyi ifade ediyor?"
myLabel = Label(root, text = soru).grid(row = x+1, column = 1, sticky = W)
Label(root, text = "Sorular").grid(row = 0, column = 1, sticky = W)
Label(root, text = "Çok Zayıf ").grid(row = 0, column = 2)
Label(root, text = "Zayıf ").grid(row = 0, column = 3)
Label(root, text = "Orta ").grid(row = 0, column = 4)
Label(root, text = " İyi ").grid(row = 0, column = 5)
Label(root, text = "Çok İyi").grid(row = 0, column = 6)
answers["soru{0}".format(x)] = IntVar()
answers["soru{0}".format(x)].set(3)
button = Radiobutton(root, variable = answers["soru{0}".format(x)], value = 1, command = lambda: selected(1, x+1)).grid(row = x+1, column = 2)
button = Radiobutton(root, variable = answers["soru{0}".format(x)], value = 2, command = lambda: selected(2, x+1)).grid(row = x+1, column = 3)
button = Radiobutton(root, variable = answers["soru{0}".format(x)], value = 3, command = lambda: selected(3, x+1)).grid(row = x+1, column = 4)
button = Radiobutton(root, variable = answers["soru{0}".format(x)], value = 4, command = lambda: selected(4, x+1)).grid(row = x+1, column = 5)
button = Radiobutton(root, variable = answers["soru{0}".format(x)], value = 5, command = lambda: selected(5, x+1)).grid(row = x+1, column = 6)
root.mainloop()