| Reply | « Previous Thread | Next Thread » |
|
Hi,
I am attempting to update the canvas with text and I have a separate class that handles the updates to the text while the main application drives other images that are being added to the canvas. Therefore, I have been passing the Canvas object to the classes that handle modifying the images. For some reason, this does not work and I believe my issue has to do with understanding how callbacks work. Here's my code below: Code:
class DrawText(CurrentCanvas):
def __init__(self,CurrentCanvas):
self.text=0
self.fillarray={}
self.resboxoffset=2
self.canvas = CurrentCanvas(redraw_callback = self.redraw)
self.draw=Draw(self.canvas)
appuifw.app.body=self.canvas
self.redraw()
def redraw(self):
self.draw_text()
def draw_text(self):
self.clear_text()
newtext=u"%dlb"%self.text
self.draw.text((35,105),newtext,(0,192,0),"title")
def clear_text(self):
#Rectangle
self.canvas.rectangle((30,60,90,110), fill=0xFFFFFF)
class DrawImages:
def drawBaseBackground(self,canvas, image):
canvas.blit(image, target = (90, 75), scale = 0)
def drawActionIcon(self,canvas, icon):
canvas.blit(icon, target = (25, 135), scale = 0)
def drawAdvantageIcon(self,canvas, icons):
canvas.blit(icons, target = (60, 205), scale = 0)
canvas=appuifw.Canvas()
appuifw.app.body=canvas
myImages = DrawImages()
e32.ao_sleep(1)
myImages.drawBaseBackground(canvas, base_background)
myImages.drawActionIcon(canvas, icon)
myImages.drawAdvantageIcon(canvas, icons )
carbon = DrawCarbon(canvas)
However, if I use another canvas in the DrawText class (with no interaction with the other images), the text is written correctly. Can someone tell me why this may be occurring? Thanks. |
| PyNewbie08 |
| View Public Profile |
| Find all posts by PyNewbie08 |
|
Join Date: Jul 2007
Posts: 159
Location: Eindhoven, the Netherlands
Offline
Regular Contributor
|
|
|
Quote:
is not defined in your code. Which is why you get this error. |
|
I was not able to get this to work correctly. Essentially, what I would like to do is write text over an existing image. Are there any examples of this?
Code:
import appuifw import messaging import e32 import TopWindow sample_picture=graphics.Image.open(u"e:\\Images\\sample.png") canvas.blit(sample_picture, target = (0, 0), scale = 1) # HOW DO I WRITE TEXT ON TOP OF THE IMAGE? |
| PyNewbie08 |
| View Public Profile |
| Find all posts by PyNewbie08 |
|
hi PyNewbie08,
![]() I think the error you are getting is due to the undefined function which you are using in the constructor. So once you define it there should be no error. ![]() Code:
def __init__(self,CurrentCanvas):
self.text=0
self.fillarray={}
self.resboxoffset=2
self.canvas = CurrentCanvas(redraw_callback = self.redraw)
self.draw=Draw(self.canvas)##error line
appuifw.app.body=self.canvas
self.redraw()
|
|
Ahhh, I see.
Is there a reason why the images on the canvas disappear after a few minutes? |
| PyNewbie08 |
| View Public Profile |
| Find all posts by PyNewbie08 |
|
Join Date: Feb 2008
Posts: 2,542
Location: Bhavnagar, Gujarat, India
gaba88
Offline
Forum Nokia Champion
|
|
|
Quote:
does the image automatically disappears aor it disappears on some click event i mean when you press anything. Hope you got my question Enjoy Pythoning Gaba88 Gargi Das- http://gargidas.blogsot.com Forum Nokia Python Wiki Learn Python at http://mobapps.org/PyS60 |
|
hi PyNewbie08
images on the canvas will dissappear as soon as some click event occurs such as, preseing the menu key and canceling that. To keep the image fixed on the canvas and overcome the problem explained above you need to use image handle_redraw() method. |
|
I haven't yet mastered how the canvas works. If any of you have suggestions as to where I can go to get a quick overview, that would be great. I've been working with the example below to prevent the images from disappearing after a few minutes. I get an invalid syntax error on appuifw.app.body=canvas but I believe the error is caused by the handle_redraw code. Could someone explain the problem here and possibly explain how the redraw is supposed to work? Thanks.
Code:
import appuifw import e32 import TopWindow, graphics from topwindow import * from appuifw import * from graphics import * g_orange = (255,69,0) canvas=appuifw.Canvas(event_callback=handle_event, redraw_callback=handle_redraw) appuifw.app.body=canvas canvas.rectangle([(0,75), (43,220)], g_orange, width=2) # selected draw=Draw(canvas) scoretext=u"Score" draw.text((0,35),scoretext,g_orange,"title") canvas.rectangle([(5,95),(100,90)], fill=(g_orange), width=2) scoretext=u"0" draw.text((5,60),scoretext,g_orange,"title") app_lock = e32.Ao_lock() app_lock.wait() |
| PyNewbie08 |
| View Public Profile |
| Find all posts by PyNewbie08 |
|
Join Date: Oct 2007
Posts: 2,841
Location: Deva, Romania
Offline
Forum Nokia Champion
|
|
handle_redraw and handle_event are callback functions that you have to define yourself. Here's a working example:
Code:
import appuifw import e32 import TopWindow, graphics g_orange = (255,69,0) img=graphics.Image.new((240,320)) img.rectangle([(0,75), (43,220)], g_orange, width=2) # selected scoretext=u"Score" img.text((0,35),scoretext,g_orange,"title") img.rectangle([(5,95),(100,90)], fill=(g_orange), width=2) scoretext=u"0" img.text((5,60),scoretext,g_orange,"title") def handle_redraw(rect): canvas.blit(img) canvas=appuifw.Canvas(event_callback=None, redraw_callback=handle_redraw) appuifw.app.body=canvas app_lock = e32.Ao_lock() app_lock.wait()
Last edited by bogdan.galiceanu : 2008-09-12 at 18:02.
|
|
hi PyNewbie08
![]() To solve the problem related with canvasing you need to understand the concept of image double buffering. I am soon going to write an article on WiKi over it. |
|
hi i think bogdan has shown the exact way.
Just to add it i that the concept of first drawing objects on image and then blinking it over the canvas is reffered to Double Buffering Of image-canvas. |
|
Join Date: Oct 2007
Posts: 2,841
Location: Deva, Romania
Offline
Forum Nokia Champion
|
|
Chintan is right. It is a sort of double buffering. Basically handle_redraw places an image on the canvas. handle_event is a callback for detecting keypresses.
|
|
hi i think bogdan has shown the exact way.
Just to add it i that the concept of first drawing objects on image and then blinking it over the canvas is reffered to Double Buffering Of image-canvas. |
|
Thanks Bogdan and CK!
Bogdan, this works for the simple rectangle and text image but when I try this on my full prototype, it doesn't work. The only difference between the full prototype and this is that I'm using: Code:
canvas.blit(sample_image, target = (0, 0), scale = 1) Code:
import appuifw import e32 import TopWindow, graphics from topwindow import * from appuifw import * from graphics import * g_orange = (255,69,0) canvas=appuifw.Canvas(event_callback=handle_event, redraw_callback=handle_redraw) appuifw.app.body=canvas canvas.rectangle([(0,75), (43,220)], g_orange, width=2) # selected draw=Draw(canvas) scoretext=u"Score" draw.text((0,35),scoretext,g_orange,"title") canvas.rectangle([(5,95),(100,90)], fill=(g_orange), width=2) scoretext=u"0" draw.text((5,60),scoretext,g_orange,"title") app_lock = e32.Ao_lock() app_lock.wait() |
| PyNewbie08 |
| View Public Profile |
| Find all posts by PyNewbie08 |
|
Join Date: Oct 2007
Posts: 2,841
Location: Deva, Romania
Offline
Forum Nokia Champion
|
|
Can you post the code for the Draw() class? Or, if it's possible, for the full prototype. It would be a lot easier to figure out what is wrong.
|
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|
| Rate This Thread | |
| Thread | Thread Starter | Forum | Replies | Last Post |
|---|---|---|---|---|
| How to create a partly transparent Canvas to lay on top of another Canvas? | mkleijer | Mobile Java Media (Graphics & Sounds) | 4 | 2007-05-22 08:00 |
| Order of called methods (Constructor,paint(), run()) when using canvas and runnable? | edmund66 | Mobile Java Media (Graphics & Sounds) | 3 | 2004-09-22 10:40 |
| Major problem with delegation to Canvas | bartekn | Mobile Java Media (Graphics & Sounds) | 8 | 2003-10-08 18:46 |
| 7650 and 3650 Canvas and FullCanvas memory | marcilgen | Mobile Java General | 5 | 2003-07-08 13:24 |
| Larger canvas? | bjajo780 | Mobile Java General | 1 | 2002-06-18 15:17 |