Can't change tkinter label text constantly in loop Can't change tkinter label text constantly in loop tkinter tkinter

Can't change tkinter label text constantly in loop


You need to wait for the user to enter their answer into the Entry widget. The code you posted doesn't do that. You have to organize your logic a little differently in GUI programss compared to command-line programs because you need to wait for events generated by user actions and then respond to them.

The code below doesn't do everything you want, but it does run. :) It displays a question, waits for the user to type their answer into the self.entry1 widget, and when they hit the Enter key in that widget it calls the .get_answer method which processes their answer and then calls the .ask method to ask a new question. After 10 questions the program exits.

import tkinter as tkimport randomclass Quiz(tk.Frame):    def __init__(self, root):        super().__init__(root)        self.root = root        self.pack()        self.label = tk.Label(self, text="This is page 1")        self.label.pack(side="top", fill="x", pady=10)        self.label1 = tk.Label(self, text='')        self.label1.pack()        self.label2 = tk.Label(self, text='')        self.label2.pack()        self.entry1 = tk.Entry(self)        self.entry1.bind("<Return>", self.get_answer)        self.entry1.pack()        self.label3 = tk.Label(self, text='')        self.label3.pack()        self.entry2 = tk.Entry(self)        self.entry2.pack()        self.start_quiz()        root.mainloop()    def start_quiz(self):        self.qdict = {            "Base-2 number system": "binary",            "Base-8 number system": "octal",            "Base-16 number system": "hexadecimal",        }        self.qkeys = list(self.qdict.keys())        self.score = 0        self.count = 1        self.ask()    def ask(self):        self.question = random.choice(self.qkeys)        self.label1.config(text="Question {}".format(self.count))        self.label2.config(text=self.question + "?")    def get_answer(self, event):        widget = event.widget        guess = widget.get()        answer = self.qdict[self.question]        if guess.lower() == answer.lower():            self.score += 1        self.label3.config(text="Score: {}".format(self.score))        self.count += 1        if self.count <= 10:            self.ask()        else:            self.root.destroy()Quiz(tk.Tk())