Reputation: 21
I'm 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 and the only option that seems to work is using a timer thread. I'm not familiar with threading or any slightly advanced tkInter at all so I'm all ears.
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 googling, tkinter and threading don't work together very well and I've seen solutions using Queues.
Upvotes: 0
Views: 47
Reputation: 385970
You don't need a timer thread for something this simple. Tkinter widgets have a method named after
which can be used to run commands in the future.
For example, to call nextQuestion
after 7 seconds, you would do this:
screen1.after(7000, nextQuestion, False, question.difficulty)
If you want to cancel the timer, save the return value and use it in a call to after_cancel
:
after_id = screen1.after(7000, nextQuestion, False, question.difficulty)
...
screen1.after_cancel(after_id)
Upvotes: 1