Python Forum
Trouble with .after() Tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with .after() Tkinter
#1
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.

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
Reply


Messages In This Thread
Trouble with .after() Tkinter - by purpleicetwice - Mar-02-2019, 03:14 PM
RE: Trouble with .after() Tkinter - by Larz60+ - Mar-02-2019, 04:57 PM
RE: Trouble with .after() Tkinter - by woooee - Mar-02-2019, 06:06 PM
RE: Trouble with .after() Tkinter - by woooee - Mar-02-2019, 09:46 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020