I'm using the Graphics and Myro packages in the Calico IDE, can anyone figure a way for me to hit the 'q' key and have the program terminate? Currently when I hit the 'q' key I have to click the mouse on my window to terminate.
def main():
win = Window(800,500)
bg = Picture("http://www.libremap.org/data/boundary/united_states/contig_us_utm_zone_14_600px.png")
bg.draw(win)
while True:
char = win.getKeyPressed()
if char == 'q':
win.close()
break
x, y = win.getMouse()
MPO = Rectangle(Point(x,y), Point(x+10,y+10))
MPO.fill = Color("white")
MPO.draw(win)
I've never heard of Calico before, but from 5 seconds looking at the docs, I see this:
So, I'm willing to bet this is why you have to click on your window before hitting the Q key has any effect—because you're program is stuck waiting inside the
getMouse()
call, just as the docs say it should be.Even if the docs didn't explain this, you could probably figure it out pretty quickly by adding some printing/logging and/or running in a debugger, to see where it's bogged down when it's not responding to your keypresses.
For example, the quick&dirty way to do this:
… and so on.
Of course in real life, you don't want to add a print statement for every single line of code. (And, when you do want that, you want a smarter way of instrumenting than manually writing all those lines.) But you can add a few to narrow it down to the general area, then zoom in and add a few more within that area, and so on, until you find the culprit.
Meanwhile, if you change your code to use
getMouseNow()
instead ofgetMouse()
, that will solve the problem, but only by busy-looping and redrawing the window over and over as fast as possible, whether or not you've done anything.What you really need here—as for any GUI app—is an event loop. I can see that there are functions called
onMouseDown
andonKeyPress
, which looks like exactly what you need here.