1

I want to make my quiz game as a gui, I made it without one. 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.

I will try to explain more, so basically I couldn't make my quiz game work using tkinter, I tried to make a loop so I don't have to make labels over and over again, It will take so much 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()
2
  • Dear BackpainYT, how can we help if we do not know what your problem is? Please edit your question to include a description of what is not working. Commented Jan 13, 2021 at 19:53
  • The list is empty because you are calling the function initially with ask() Commented Jan 13, 2021 at 22:04

1 Answer 1

1

There are couple of things that need changes. Refer to the code below

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

def ask():
    global score
    print(len(questions))
    if questions:
        #Check both the possible answers
        if useranwser.get() in (questions[0].answer,questions[0].answer2):
            print("Correct")
            score += 1
        else:
            print("Incorrect")
        questions.pop(0)
        if questions:
            s.set(questions[0].prompt) 
        useranwser.set('')
    else:
        print('Done')

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[4], "Uraniam","uraniam"),
    Question(question_prompts[0], "1956","1956"),
    Question(question_prompts[1], "Tunis","tunis"),
    Question(question_prompts[2], "France","france"),
    Question(question_prompts[3], "324m","324")
]

s = tk.StringVar()
useranwser = tk.StringVar() 
q = tk.Label(win,textvariable = s)
q.grid(row = 0, column = 0)
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 ) 

s.set(questions[0].prompt) #Set the initial question 
    
score = 0

win.mainloop()

NOTES

  • useranwser is holding the instance of StringVar(), use the .get() method on it to get the value.
  • There is no point in having for question in questions: loop as it would do the same thing over and over again.
  • for questionpop in questions: this loop will clear your list as soon as you call ask and since you had done that on program initialization, your list had become empty to begin with.
  • In your initial code, the score would always remain 0, firstly because the condition will always be False and even if you made the condition right using .get() it wouldn't work as you are just performing it once on the program initialization. I have added the corrected condition to the ask function so that it it checked every time the submit button is clicked.
  • question.answer only stores a constant value "Uranium" as after the iteration in the code above that, it takes the last element of the list.

EDIT

There is a bug that i couldn't fix in your code, I've read and understood your notes and also the code. I kept trying to fix a problem. The last question gets repeated twice

The question wasn't repeated, it took an extra click for the program to print done, since before that if questions: was True and became False after that additional click. Make the changes below and it should work.

def ask():
    global score
    if questions:
        #Check both the possible answers
        if useranwser.get() in (questions[0].answer,questions[0].answer2):
            print("Correct")
            score += 1
        else:
            print("Incorrect")
        questions.pop(0)
        if questions:
            s.set(questions[0].prompt) 
        else:
            print('Done')
            quit() #optional
        useranwser.set('')
Sign up to request clarification or add additional context in comments.

3 Comments

There is a bug that i couldn't fix in your code, I've read and understood your notes and also the code. I kept trying to fix a problem. The last question gets repeated twice
@BackpainYT I have updated the answer with the code and reason, hope it helps
Thank you for your time, I truly appreciate it man.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.