Is there a way to wait for a condition to be true?
kotlin wait until condition
wait until boolean is true
java thread waiting on condition
wait for response java
I'm attempting to log coordinates 3 separate times when a user clicks on the turtle screen, then continue running other commands once that is completed. Clicking 3 times does nothing, and the shell keeps printing that it's waiting, while one additional click causes the whole thing to not work and I get a "not Responding" message from the turtle graphics window.
import turtle as t import time canvas=t.getcanvas() xlist= ylist= listcomplete=False def getPos(x,y): xlist.append(canvas.winfo_pointerx()) ##Logs the x and y coords when mouse is clicked ylist.append(canvas.winfo_pointery()) print('appended the lists.') if len(xlist)==3: listcomplete=True t.onscreenclick(getPos) def main(): while listcomplete==False: time.sleep(1) print('waiting...') ##Prints periodically just to let me know it's still running main() print('list complete.') ##Prints to alert the list has been finished print(xlist) (Insert rest of code to follow)
getPos() will not change the global variable, instead it will create a new varable of the same name within the local scope.
To change the global variable, you have to tell python to use it from the global scope:
def getPos(x,y): global listcomplete # tell python to use the variable from the global scope xlist.append(canvas.winfo_pointerx()) ##Logs the x and y coords when mouse is clicked ylist.append(canvas.winfo_pointery()) print('appended the lists.') if len(xlist)==3: listcomplete=True
That's due to the default behavior of the assignment operator (
Other operators, such as the comparision operator (
==) will lookup the variable from the enclosing scope(s) if it's not found within the local scope, thus you may use
while listcomplete==False: within
main() w/o telling pyton to use the variable from the global scope.
But ideally, you do not even have to use that global variable. Instead run the turtle main loop and exit the turtle window when your condition is met:
import turtle as t canvas=t.getcanvas() xlist= ylist= def getPos(x,y): xlist.append(canvas.winfo_pointerx()) ##Logs the x and y coords when mouse is clicked ylist.append(canvas.winfo_pointery()) print('appended the lists.') if len(xlist)==3: t.bye() # exit turtle window t.onscreenclick(getPos) t.Screen().mainloop() # will wait until turtle window is closed print('list complete.') ##Prints to alert the list has been finished print(xlist)
wait x seconds or until a condition becomes true, and notifies or Conditions (which work more or less the same way): it is difficult IMHO, there is no point using a wait/notify pattern when there is a When the condition becomes true, call latch.countdown();; in your waiting� C# – Best way to wait until a condition is true .NET Framework Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions.
Is it possible to continue running the turtle window after the lists have been created?
Things get difficult in turtle when you fight it's event-based model as you're trying to do. Work with the model, and things get easier. The code below presents a blank window, after you click on it in three places, it will connect your points to make a triangle:
from turtle import Screen, Turtle, mainloop def getPosition(x, y): screen.onscreenclick(None) # disable the handler inside the handler positions.append((x, y)) if len(positions) == 3: screen.ontimer(listComplete) # sometime after this function completes else: screen.onscreenclick(getPosition) # restore the handler def listComplete(): for position in positions: turtle.goto(position) turtle.pendown() turtle.goto(positions) # close our triangle # (Insert rest of code to follow) positions =  turtle = Turtle() turtle.hideturtle() turtle.penup() screen = Screen() screen.onscreenclick(getPosition) mainloop() # invoke as function to make Python 2 friendly as well
The key is that "rest of code to follow" will be in functions, not top level code.
Sleep and check until condition is true, The key property that waiting for a condition provides is that it atomically releases you never use Condition instances in this way, except perhaps within their own This is true even if it can be shown that the interrupt occurred after another� I wrote this script as a way to wait for a certain time or condition to become true and then do something. I usually use it in cases where I have to run some one-liner at a certain time and I don't want to go through the hassle of creating a schedule task.
def handle_click(mouse_x,mouse_y): # <== I think thats what ya need right dere, I dont think it knows you're clicking newpin = [[mouse_x,mouse_y], [0,0], 0,0,20, 1000000]
I tried putting a print statement after the click I couldn't get it to even print a test click. That may be me not trying hard enough tho ;) I just remember using something like above to handle a mouse click. (in my situation it created a pin for a pinball game) if you look up the turtle api for circle you can see the [0,0],0,0,20, 100000] means.
But ultimately that last number the 10000 whatever is "mass" so the more of it the less it moves. again my situation. turtle.onscreenclick(handle_click). Thats at least an idea :) also yes u can do a wait after an if. Throw in print statements.
Condition (Java SE 9 & JDK 9 ), That is, it should look just like the previous example. Wait until either a condition becomes true or a timeout occurs. The incorrect code for this typically looks like:. When using condition variables there is always a Boolean predicate involving shared variables associated with each condition wait that is true if the thread should proceed. Spurious wakeups from the pthread_cond_timedwait () or pthread_cond_wait () functions may occur.
WaitNotInLoop Because of spurious wakeups, Object.wait(), Learn how to use wait() and notify() to solve synchronization Such blocks keep a check for a particular condition before resuming the execution. wait by calling wait() on this thread; But when it is true, we toggle the status,� I'm attempting to log coordinates 3 separate times when a user clicks on the turtle screen, then continue running other commands once that is completed. Clicking 3 times does nothing, and the shell
wait and notify() Methods in Java, So if this event object can wait on any condition, how do we tell it when to end gracefully by setting the exit_thread global variable to True . wait causes the current thread to block until the condition variable is notified or a spurious wakeup occurs, optionally looping until some predicate is satisfied. 1) Atomically unlocks lock , blocks the current executing thread, and adds it to the list of threads waiting on * this .
How To Make Python Wait, acquire: mark the lock as owned by the current thread; if some other thread already owns also need a way to wait for another thread to do something (e.g., wait for a Condition variables: used to wait for a particular condition to become true� Therefore, users of this function shall ensure their condition for resumption is met. If pred is specified (2), the function only blocks if pred returns false, and notifications can only unblock the thread when it becomes true (which is especially useful to check against spurious wake-up calls). It behaves as if implemented as:
- You get a "not responding" window if your program doesn't handle incoming Windows events. Using the turtle
mainloop()will fix this; you should be scheduling timer events and then handling them when they come in, not sleeping in the main thread. Some variant of this is true for pretty much every GUI framework; it's not specific to turtle. (Those that don't require the user to run an event loop manually are still doing it under the hood).
- I did add your suggestion, but I think the issue still remains as the turtle window still crashes. I'm not sure if this is related to the issue, but the printing of "appended the lists." never occurs, which makes me think getPos() isn't even being run when I click.
- @Ian I updated my answer with a tested and working code sample that does not even need a global variable.
- @MikeScottyThanks, this is really neat. There's still a lot for me to learn about setting up code properly and using events and handlers. Is it possible to continue running the turtle window after the lists have been created? The x and y lists are part of the end product.
- @Ian Sure, simply move the printing or whatever you want to do inside the
if len(xlist)==3:block instead of exiting the turtle window.