-1

In a tkinter GUI, I have a few classes, and at the end, a while loop. Without the while loop, everything works fine, save for the functions in the loop. But whenever I put the while loop on the end, the tkinter window didn't open. Here is some of my code:

while 1:
    Game['paper'] += Game['totalpps']/10
    time.sleep(0.1)

There is a lot I cut out, but I have a Game dictionary that has all the games data. But when the while loop is going, the tkinter window doesn't appear. Even after I put a time.sleep(5) before the while loop, the window didn't appear.

Any ideas why?

Any help is appreciated.

Note: I am using a raspberry pi, with the Raspbian OS.

cs1349459
  • 911
  • 9
  • 27
  • 1
    When you call sleep, the application sleeps. That means it can't refresh the window. – Bryan Oakley Jun 16 '20 at 22:49
  • Does this answer your question? [use after method to prevent main loop from freezing](https://stackoverflow.com/a/25753719/7414759) – stovfl Jun 17 '20 at 08:19

2 Answers2

1

Threads can easily solve your problem:

from threading import Thread

# All this code must occur before calling window.mainloop()

def loop():
    while 1:
        Game['paper'] += Game['totalpps']/10
        time.sleep(0.1)

Thread(target=loop).start()
Amin Guermazi
  • 1,632
  • 9
  • 19
0

If you just put root.update() before the while loop and in the while loop, everything should work fine.

cs1349459
  • 911
  • 9
  • 27