Reputation: 1149
So there I was just messing around in an interpreter when I came across a strange problem. I was just trying to make a bouncing smiley face to brighten my day, but for some reason I'm unable to do this. And I have no desire to do this in Pygame, if anyone thinks to suggest that. This isn't a project, it was just something silly I was doing and don't understand why it's not working.
from Tkinter import *
import time,random
root = Tk()
root.geometry("500x500")
root.mainloop()
bouncer = Label(root, text="=D")
def bounce ():
X = random.randint(1,500)
Y = random.randint(1,500)
bouncer.place(x=X, y=Y)
while True:
time.sleep(0.5)
bounce()
This code does not work, and I for the life of me can't figure out why. What ends up happening is the loop runs forever, and when I ^C bounce() gets called once and the script ends. I tried it in a for loop to see if I could just bounce 100 times, but bounce() would just call once after the loop finished. I'm fairly stumped on this.
Any ideas, anyone?
Upvotes: 3
Views: 1673
Reputation: 4298
sleep
puts the process to the sleep state. Better use after
.
from Tkinter import *
import time,random
root = Tk()
root.geometry("500x500")
bouncer = Label(root, text="=D")
def bounce ():
X = random.randint(1,500)
Y = random.randint(1,500)
bouncer.place(x=X, y=Y)
root.after(1000, bounce)
bounce()
root.mainloop()
Upvotes: 4