Home > OS >  Answer variable doesn't update a second time in tkinter
Answer variable doesn't update a second time in tkinter

Time:01-07

I'm trying to make a quiz for myself and it I'm able to answer it once but after that the answer doesn't update. This is my first bigger python/tkinter project.

from tkinter import *
from tkinter import ttk
from ctypes import windll
import random
windll.shcore.SetProcessDpiAwareness(1)

words = {

    "triste": "sad",
    "mahalang": "sick",
    "chocho": "eat" ,
    "kada dia": "everyday" ,
    "eskuela": "school" ,
    "asagua": "wife",
    "nobia": "girlfriend" ,
    "nobiu": "boyfriend" ,
    "bintåna": "window"
}

def quizMain(): 

    root = Tk()
    root.title("Chamorro Quiz")
    root.minsize(300, 150)

    word = random.choice(list(words.keys()))
    answer = words[word]

    print(word)
    print(answer)

    wordQuestion = ttk.Label(root, text=word)
    wordQuestion.grid()

    wordEntry = StringVar()
    wordAnswer = ttk.Entry(root, textvariable=wordEntry)
    wordAnswer.grid()
    
    def getAnswer():
        userAnswer = wordAnswer.get()

        if userAnswer == answer:
            changeWord()
    
    def changeWord():
        word = random.choice(list(words.keys()))
        answer = words[word]
        print(word, answer)
        wordQuestion.configure(text=word)
        
    submit = ttk.Button(root, text="Submit", command=getAnswer)
    submit.grid()

    root.mainloop()

quizMain()

I've tried moving around variables in and out of functions and I can't figure out why it won't update.

CodePudding user response:

We can use the nonlocal keyword to allow the child function to reassign the parent function variables. Hope this helps.

from tkinter import *
from tkinter import ttk
from ctypes import windll
import random

windll.shcore.SetProcessDpiAwareness(1)

words = {
    "triste": "sad",
    "mahalang": "sick",
    "chocho": "eat",
    "kada dia": "everyday",
    "eskuela": "school",
    "asagua": "wife",
    "nobia": "girlfriend",
    "nobiu": "boyfriend",
    "bintåna": "window"
}


def main():
    root = Tk()
    root.title("Chamorro Quiz")
    root.minsize(300, 150)

    word = random.choice(list(words.keys()))
    answer = words[word]

    print(f"WORD: {word} | ANSWER: {answer}")

    word_question = ttk.Label(root, text=word)
    word_question.grid()

    word_entry = StringVar()
    word_answer = ttk.Entry(root, textvariable=word_entry)
    word_answer.grid()

    def get_answer():
        user_answer = word_answer.get()

        if user_answer == answer:
            word_answer.delete(0, END)  # Erases input when correct
            change_word()

    def change_word():
        nonlocal answer, word  # Allows reassigning parent function variables
        word = random.choice(list(words.keys()))
        answer = words[word]
        print(f"WORD: {word} | ANSWER: {answer}")
        word_question.configure(text=word)

    submit = ttk.Button(root, text="Submit", command=get_answer)
    submit.grid()

    root.mainloop()


if __name__ == "__main__":
    main()
  • Related