wxPython draw text onto EXISTING bitmap or image

让人想犯罪 __ 提交于 2020-01-03 15:34:06

问题


I'm trying to add a background image to some text, I'm hoping to do this by using MemoryDC to write text to an image which will then be placed in my GridSizer.

Everything works as I want it to with the exception that I cannot use an existing image to draw text onto, I have to create a new, empty, bitmap which is pointless for me because it puts me back where I started.

Here is my code, it places a white box into my GridSizer with text drawn onto it:

    w, h = 150,64
    bmp = wx.EmptyBitmap(w,h)
    dc = wx.MemoryDC()
    dc.SelectObject(bmp)
    dc.Clear()
    text = 'whatever'
    tw, th = dc.GetTextExtent(text)
    dc.DrawText(text, (w-tw)/2, (h-th)/2)
    dc.SelectObject(wx.NullBitmap)
    output_bitmap = wx.StaticBitmap(self, -1, bmp)
    return output_bitmap

Now here is the same code changed to try and use an existing image:

    png = wx.Image(location, wx.BITMAP_TYPE_PNG).ConvertToBitmap()
    bmp = wx.StaticBitmap(self, -1, bitmap = png, size=(150, 64))

    dc = wx.MemoryDC()
    dc.SelectObject(bmp)
    dc.Clear()
    text = 'whatever'
    tw, th = dc.GetTextExtent(text)
    dc.DrawText(text, (w-tw)/2, (h-th)/2)
    dc.SelectObject(wx.NullBitmap)
    output_bitmap = wx.StaticBitmap(self, -1, bmp)
    return output_bitmap

That gives the following error message:

File "/home/user/projects/project/base.py", line 267, in online_indicator
dc.SelectObject(bmp)

File "/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/wx/_gdi.py", line 4783, in SelectObject
return _gdi_.MemoryDC_SelectObject(*args, **kwargs)

TypeError: in method 'MemoryDC_SelectObject', expected argument 2 of type 'wxBitmap &'

I've also tried using dc.DrawLabel but that won't accept my bitmap image.

EDIT:

Think I might be nearly there, started using dc.DrawImageLabel, now I can draw either an image or text, from what I can see it should be able to draw both but for some reason it won't, here is my new code:

To draw text:

    png = wx.Image(location, wx.BITMAP_TYPE_PNG).ConvertToBitmap()
    want_bmp = wx.StaticBitmap(self, -1, bitmap = png, )
    w, h = 150,64
    bmp = wx.EmptyBitmap(w,h)
    dc = wx.MemoryDC()
    dc.SelectObject(bmp)
    dc.Clear()
    rect = bmp.GetRect()
    dc.DrawImageLabel(text='this',image=wx.NullBitmap, rect=rect, alignment=10)
    dc.SelectObject(wx.NullBitmap)
    output_bitmap = wx.StaticBitmap(self, -1, bmp)
    return output_bitmap

to draw image:

    png = wx.Image(location, wx.BITMAP_TYPE_PNG).ConvertToBitmap()
    want_bmp = wx.StaticBitmap(self, -1, bitmap = png, )
    w, h = 150,64
    bmp = wx.EmptyBitmap(w,h)
    dc = wx.MemoryDC()
    dc.SelectObject(bmp)
    dc.Clear()
    rect = bmp.GetRect()
    dc.DrawImageLabel(text='this',image=wx.Bitmap(location), rect=rect, alignment=10)
    dc.SelectObject(wx.NullBitmap)
    output_bitmap = wx.StaticBitmap(self, -1, bmp)
    return output_bitmap

回答1:


MemoryDC works on a Bitmap, not a StaticBitmap control.

Also, you can load Bitmaps directly, no need to use an Image object.

bitmap = wx.Bitmap(location)
dc = wx.MemoryDC(bitmap)
text = 'whatever'
w, h = dc.GetSize()
tw, th = dc.GetTextExtent(text)
dc.DrawText(text, (w - tw) / 2, (h - th) / 2) #display text in center
del dc
control = wx.StaticBitmap(self, -1, bitmap)


来源:https://stackoverflow.com/questions/20287152/wxpython-draw-text-onto-existing-bitmap-or-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!