Mar-02-2019, 03:14 PM
Im doing my first big project which is a quiz. I am stuck on trying to limit the time the user has to answer a question. I've searched for so many hours and the only option that seems to work is using a timer thread or .after(). I'm not faliliar with threading or any slightly advanced tkInter at all so I'm all ears.
The version below is using .after()
def revisionMode(question): inputAnswer = StringVar() #-----Creation & placement of buttons and labels qLabel = Label(screen1, text = question.prompt[0] qLabel.grid(row = 6, column = 2) answerBox = Entry(screen1, textvariable = inputAnswer) answerBox.grid(column = 2, row = 10) t = Timer(7.0, nextQuestion, args=(False, question.difficulty), kwargs=None) t.start() #-----The button that will trigger the validation of the answer Button(screen1, text = "Submit", command = lambda: checkAnswer(question)).grid(column = 3, row = 9)The error I get from that is that is: RuntimeError: main thread is not in main loop. From my understanding and desparate googling, tkinter and threading don't work toghether very well and I've seen solutions using Queues.
The version below is using .after()
def revisionMode(question): inputAnswer = StringVar() #-----Creation & placement of buttons and labels qLabel = Label(screen1, text = question.prompt[0] qLabel.grid(row = 6, column = 2) answerBox = Entry(screen1, textvariable = inputAnswer) answerBox.grid(column = 2, row = 10) after_id = screen1.after(7000, nextQuestion, False, question.difficulty) #-----The button that will trigger the validation of the answer Button(screen1, text = "Submit", command = lambda: checkAnswer(question)).grid(column = 3, row = 9) screen1.after_cancel(after_id)With this I don't get any errors but the question displayed does not change after 7 sec. I've been stuck for so long and any help, advice and tips are greatly appreciated! :D