Re: [Pyobjc-dev] simple animation w/ NSTimer
Brought to you by:
ronaldoussoren
|
From: Black <py...@bl...> - 2008-04-01 12:38:18
|
I'm coming to this thread a little late, but looking at your code, I
don't see where you have installed the timer into the run loop...
Here is an example taken from one of my apps:
self.timer =
NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_
(1/150., self, self.heartbeat, None, True)
NSRunLoop.currentRunLoop().addTimer_forMode_(self.timer,
NSDefaultRunLoopMode) NSRunLoop.currentRunLoop
().addTimer_forMode_(self.timer, NSEventTrackingRunLoopMode)
On Mar 30, 2008, at 7:03 PM, Carl Bauer wrote:
> I'm a physics grad student (not a coder) wanting to put together a
> slick realtime physics simulation for my statistical mechanics
> class. (Simulating the 2D Ising model for ferromagnets in case
> anyone cares).
>
> Anyway, to start, I'm just trying to get a rectangle to change
> colors periodically. Why doesn't this work?
>
> import objc
> from Foundation import *
> from AppKit import *
> from PyObjCTools import NibClassBuilder, AppHelper
> from Quartz import NSColor
>
> NibClassBuilder.extractClasses("MainMenu")
>
> # class defined in MainMenu.nib
> class Lattice(NibClassBuilder.AutoBaseClass):
> # the actual base class is NSView
> color = NSColor.redColor()
> toggle = 0
> rect = NSMakeRect(40,40,40,40)
>
> def drawRect_(self,frameRect):
> NSEraseRect(self.rect)
> self.color.setFill()
> NSRectFill(self.rect)
>
> def start_(self, sender):
>
> self.timer=NSTimer.scheduledTimerWithTimeInterval_target_selector_user
> Info_repeats_(1.0,self,self.tick,None,True)
>
> def tick(self,timer):
> self.toggle = (self.toggle+1)%2
> if self.toggle:
> self.color = NSColor.blueColor()
> else:
> self.color = NSColor.redColor()
> self.display()
>
> if __name__ == "__main__":
> AppHelper.runEventLoop()
>
> The rectangle is drawn just fine at the beginning, but the timer
> doesn't seem to actually invoke drawRect_. I'm probably just not
> understanding how to properly use display()? Let me know if I'm
> being annoying and should just post to a different mailing list.
>
> Thanks,
> Carl
> ----------------------------------------------------------------------
> ---
> Check out the new SourceForge.net Marketplace.
> It's the best place to buy or sell services for
> just about anything Open Source.
> http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/
> marketplace_______________________________________________
> Pyobjc-dev mailing list
> Pyo...@li...
> https://lists.sourceforge.net/lists/listinfo/pyobjc-dev
|