views:

185

answers:

2

Hello,

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..

+1  A: 

You use a wx.MemoryDC

Will
can you give me some code example please :)
aF
There are LOADS of examples in the wxPython demos. just a heads up
Steven Sproat
+1  A: 

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()
Anurag Uniyal
Anurag, can you tell me what is wrong with the code that I posted plz :)
aF
@aF, i have updated code, so now dc is cleared and I calculate the correct vertical and horizontal centered position for text
Anurag Uniyal