aF.
aF.

Reputation: 66757

How to draw text in a bitmap using wxpython?

I want to draw a number centered inside a wx.EmptyBitmap.

How can I do it using wxpython?

Thanks in advance :)

import wx

app = None

class Size(wx.Frame):
    def __init__(self, parent, id, title):
        frame = wx.Frame.__init__(self, parent, id, title, size=(250, 200))
        bmp = wx.EmptyBitmap(100, 100)
        dc = wx.MemoryDC()
        dc.SelectObject(bmp)
        dc.DrawText("whatever", 50, 50)
        dc.SelectObject(wx.NullBitmap)
        wx.StaticBitmap(self, -1, bmp)
        self.Show(True)


app = wx.App()
Size(None, -1, 'Size')
app.MainLoop()

This code only gives me a black image, what am I doing wrong? What's missing here..

Upvotes: 3

Views: 6052

Answers (2)

Anurag Uniyal
Anurag Uniyal

Reputation: 88855

Select the bmp in a wx.MemoryDC, draw anything on that dc and then select that bitmap out e.g.

import wx

app = None

class Size(wx.Frame):
    def __init__(self, parent, id, title):
        frame = wx.Frame.__init__(self, parent, id, title, size=(250, 200))
        w, h = 100, 100
        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)
        wx.StaticBitmap(self, -1, bmp)
        self.Show(True)


app = wx.App()
app.MainLoop()

Upvotes: 5

Will
Will

Reputation: 75683

You use a wx.MemoryDC

Upvotes: 1

Related Questions