how to loop a tkinter label using indexes from a list

I want to make my quiz game as a gui, I made it without one and the code wasn't that long. I tried looking all over the internet. I've been trying to find a way to make it work in tkinter but its so confusing. Also I am really motivated about finding a solution but its been 3 days already and I couldn't find one. Is it me or is tkinter a bit hard for beginner developers? Please if you could find a solution and explain all the steps you did I will Truly appreciate it and thanks again.

I will try to explain more, so basically i couldn't make my quiz game work using tkinter, I tried too make a loop so i don't have to make labels over and over again, It will take so mush time and its not that efficient. Would you please help me out.

This is my code without the gui

class Question:
    def __init__(self,prompt,answer, answer2):
        self.answer = answer
        self.prompt = prompt
        self.answer2 = answer2


question_prompts = [
    "When did Tunisia gain independence? \n 1956 , 1984  , 1965 \n", "What is the captial of tunis ? \n Tunis, Gafsa, Nabuel \n",
    "Who won 2018 football world cup ? \n Italy, France, England \n","how long is the eiffel tower ?\n 324m, 354m, 412m \n",
    "What is the heaviest metal \n Iron, Copper, Uraniam \n "
]

questions = [
    Question(question_prompts[0], "1956","1956"),
    Question(question_prompts[1], "Tunis","tunis"),
    Question(question_prompts[2], "France","france"),
    Question(question_prompts[3], "324m","324"),
    Question(question_prompts[4], "Uraniam","uraniam")
]

def playgame():
    score = 0
    for question in questions:
        answer = input(question.prompt)
        if answer == question.answer:
            score += 1
        elif answer == question.answer2:
            score += 1
    print("You have answerd " + str(score) + " / " + str(len(questions)) + " Correct answers ")

playgame()
    

This is my quiz with gui but its buggy

import tkinter as tk
from time import sleep

win = tk.Tk()

win.title("Learning game")

class Question:
    def __init__(self,prompt,answer, answer2):
        self.answer = answer
        self.prompt = prompt
        self.answer2 = answer2


question_prompts = [
    "When did Tunisia gain independence? \n 1956 , 1984  , 1965 \n", "What is the captial of tunis ? \n Tunis, Gafsa, Nabuel \n",
    "Who won 2018 football world cup ? \n Italy, France, England \n","how long is the eiffel tower ?\n 324m, 354m, 412m \n",
    "What is the heaviest metal \n Iron, Copper, Uraniam \n "
]

questions = [
    Question(question_prompts[0], "1956","1956"),
    Question(question_prompts[1], "Tunis","tunis"),
    Question(question_prompts[2], "France","france"),
    Question(question_prompts[3], "324m","324"),
    Question(question_prompts[4], "Uraniam","uraniam")
]

s = tk.StringVar()
useranwser = tk.StringVar() 

def ask():
    for questionpop in questions:
        s.set(question_prompts.pop(0))


for question in questions:
    q = tk.Label(win)
    q.grid(row = 0, column = 0)
    q.config(textvariable = s)
    u_answer = tk.Entry(win, textvariable = useranwser)
    u_answer.grid(row = 1, column = 0)
    b = tk.Button(win, text ="Submit", command = ask)
    b.grid(row = 2, column =0 ) 
    
score = 0
if useranwser == question.answer:
    print("Correct")
    score += 1
else:
    print("Uncorrect")


    


ask()
win.mainloop()


Read more here: https://stackoverflow.com/questions/65708491/how-to-loop-a-tkinter-label-using-indexes-from-a-list

Content Attribution

This content was originally published by BackpainYT at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: