user5517534
user5517534

Reputation: 3

How to 're-run' the mainloop in Tkinter

I've been asked to make a Chat program using Tkinter. For the final part, I'm supposed to have the Chat window open, which has one Entry field, one Button (SEND) and a Text Widget to display the chat log. This is my first week learning Tkinter, and I've been told in class that the mainloop() is an infinite loop as long as the user doesn't close the window or write root.quit(). So in the chat window, I'm supposed to check for new messages every 10 seconds. Is it possible to do that in the mainloop()? If yes, please let me know how, because I have no idea how that can happen since the stuff before the mainloop() is read only once. For example, something like a print statement is printed only once, even though the mainloop() is an infinite loop.

Upvotes: 0

Views: 571

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385910

You can call the after method of the root window to call something in the future. If that function itself calls after, you can set it up so that your function runs every 10 seconds forever.

def check_for_messages():

    <your code here>

    root.after(10000, check_for_messages)

Call that function once before calling mainloop() and it will run every 10 seconds for as long as mainloop() is running.

Upvotes: 1

Related Questions