0

There are hundreds of questions involving a while True: loop in Tkinter (which of course crashes it, because it never executes the mainloop() (see here and here for two of hundreds of examples)). The obvious (and so far only) answers require a function of some sorts.

My question is: is there any way to have something similar to a while True: loop in Tkinter that doesn't crash, without using a function? I didn't find any questions like this.

3
  • Why without using a function? Commented Jan 4, 2016 at 5:52
  • Because using a function makes the code annoying and fiddly to work with, and I'm really not bothered to deal with it (programmers program because they're lazy, right? No offense). If there's no answer, it's back to the fiddling. Commented Jan 4, 2016 at 5:56
  • 1
    You'll find it less fiddly the more you practice it. Anyway, it's certain to be less fiddly than alternative hacks (if they exist). Commented Jan 4, 2016 at 6:00

1 Answer 1

1

I'm not very familiar with python and tkinter, but this should work:

import tkinter as tk
import threading

root = tk.Tk()
thread = threading.Thread(target=root.mainloop)
thread.start()

while True:
    print("Hello, World!");
    # TODO - add more stuff to this while loop
Sign up to request clarification or add additional context in comments.

3 Comments

Correct me if I'm wrong, but thread.start() is a function. It's just defined in a different file.
Sorry, I should have rephrased that question - it just means without using a function inside of the file itself. EDIT: It works! Thanks!
When expanded to a real program, this will likely crash. Tkinter isn't thread safe; you should normally only ever call mainloop from the same thread that created the widgets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.