[Tutor] A range of numbers

Leung Cris krazie_mu_boi at hotmail.com
Mon Dec 1 20:44:31 EST 2003


Livewires is ERROR-PRONE. But it's wat i have to use at school, o well. Okay , the code doesn't really work because the function mouse_position() returns not a number, but a None if the cursor isn't on the graphics screen. And becuz livewires made the function begin_graphics() open up a window but doesn't MAXIMIZE it . I'm not able to out race the computer in putting my cursor on to the graphics window. So, I think i need a code that wait for something to happen before going on to the loop. I saw the mouse_wait(how) function on the worksheet, but don't kno how to use it.


>From: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
>To: Cris Leung <krazie_mu_boi at hotmail.com>
>CC: tutor at python.org
>Subject: Re: [Tutor] A range of numbers
>Date: Mon, 1 Dec 2003 17:37:42 -0800 (PST)
>
>
>
>On Mon, 1 Dec 2003, Cris Leung wrote:
>
> > Okay, I was working on it just now, and I found out that this works:
> > __________________________________________________________________
> > def func():
> > 	begin_graphics()
> > 	begin_mouse()
> > 	box(200,200,300,300)
> > 	for i in range(100):
> > 		print 'delay'
> > 	while 1:
> > 		x,y = mouse_position()
> > 		if 200 <= x <= 300 and 200 <= y <= 300 and mouse_buttons()['left'] == 1:
> > 			break
> > 	box(100,100,400,400)
> > 	end_mouse()
> > __________________________________________________________
> >
> > as long as I move my mouse to the graphics window before the "print
> > delay"  is finish. However, if I dun click within seconds after the
> > "print 'delay'"  , the thing freezes.
>
>
>Ah!
>
>Try moving the begin_mouse() and end_mouse() stuff around the
>mouse_position().  What's happening is that begin_mouse() and end_mouse()
>are the function that update the mouse position in the program. Without
>them, mouse_position() will continue to return the same thing over and
>over.
>
>
>###
>def func():
>     begin_graphics()
>     box(200,200,300,300)
>     while 1:
>         begin_mouse()
>         x,y = mouse_position()
>         end_mouse()
>         if (200 <= x <= 300
>             and 200 <= y <= 300
>             and mouse_buttons()['left'] == 1):
>             break
>     box(100,100,400,400)
>###
>
>should fix the bug.
>
>
>This is not an ideal fix --- in programming lingo, this is a "busy
>waiting" loop that continues to ask the system what the mouse position is,
>even if it hasn't changed recently.  So it's a bit inefficient.  Still, it
>should be ok enough to make things work.
>
>
>(But to tell the truth, I don't know why the Livewires API is designed
>that way; it seems very error prone!  I think it might simply be better to
>have mouse_position() automatically call begin_mouse() and end_mouse() for
>us.  I'd have to look more closely at the Livewires API to see why they
>don't do that.)
>
>
>Good luck!
>

_________________________________________________________________
¨Ï¥Î MSN Messenger¡A»PªB¤Í¦b½u¤W²á¤Ñ: http://messenger.microsoft.com/tc 




More information about the Tutor mailing list