Thread: [ocemp-devel] Overriding the update fuction
Status: Beta
Brought to you by:
marcusva
From: Stephen T. <ste...@be...> - 2008-08-16 21:26:37
|
Sorry for the newbie question. I'm trying to create a widget that will create a line of text that crawls across a certain portion of the screen. I thought that I would create an ImageLabel, select the left hand portion using subsurface, display that subsurface, then override the update function to select a new subsurface one pixel to the right every time it is called. Unfortunately I can't seem to figure out what to do to avoid creating a recursive call to update. I've tried using the lock and unlock functions to no avail. Can someone help me to understand the proper way of overriding the update function, or tell me if there is a better way to implement this? Thanks. class TextCrawl(ImageLabel): def __init__(self,string,x,y): self.xlimit = x self.ylimit = y self.text = String.draw_string(string,'Times',16,1,(0,0,0)) self.rectsize = self.text.get_rect() scrtext = self.text.subsurface((self.xlimit, self.rectsize[1],self.ylimit,self.rectsize[3])) ImageLabel.__init__(self,scrtext) def update(self): self.lock() self.xlimit += 1 self.ylimit += 1 self.set_picture(self.text.subsurface((self.xlimit, self.rectsize[1],self.ylimit,self.rectsize[3]))) self.unlock() ImageLabel.update(self) |
From: Marcus v. A. <ma...@sy...> - 2008-08-18 08:15:28
|
On, Sat Aug 16, 2008, Stephen Terry wrote: > Sorry for the newbie question. I'm trying to create a widget that will > create a line of text that crawls across a certain portion of the > screen. I thought that I would create an ImageLabel, select the left > hand portion using subsurface, display that subsurface, then override > the update function to select a new subsurface one pixel to the right > every time it is called. > > Unfortunately I can't seem to figure out what to do to avoid creating a > recursive call to update. I've tried using the lock and unlock > functions to no avail. Can someone help me to understand the proper way > of overriding the update function, or tell me if there is a better way > to implement this? Thanks. > > class TextCrawl(ImageLabel): > > def __init__(self,string,x,y): > > self.xlimit = x > self.ylimit = y > self.text = String.draw_string(string,'Times',16,1,(0,0,0)) > self.rectsize = self.text.get_rect() > scrtext = self.text.subsurface((self.xlimit, > self.rectsize[1],self.ylimit,self.rectsize[3])) > ImageLabel.__init__(self,scrtext) > > def update(self): > > > self.lock() > self.xlimit += 1 > self.ylimit += 1 > self.set_picture(self.text.subsurface((self.xlimit, > self.rectsize[1],self.ylimit,self.rectsize[3]))) > self.unlock() > ImageLabel.update(self) Overriding the update() method in such a way is likely to break the whole update() system for the widget and can cause exceptions to occur as you miss the **kwargs parameter. Instead of using update(), use an own function to be called, that increments the x and y limits and marks the widget as dirty: class TextCrawl (ImageLabel): ... def crawl (self): self.xlimit += 1 self.ylimit += 1 # ImageLabel.set_picture() causes the widget to be marked as # dirty. self.set_picture(self.text.subsurface((self.xlimit, self.rectsize[1],self.ylimit,self.rectsize[3]))) Regards Marcus |