3

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
  • you need to clear the bitmap to the appropriate colour, and draw the text in the appropriate colour... Commented Apr 6, 2010 at 12:50

2 Answers 2

5

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()
Sign up to request clarification or add additional context in comments.

2 Comments

Anurag, can you tell me what is wrong with the code that I posted plz :)
@aF, i have updated code, so now dc is cleared and I calculate the correct vertical and horizontal centered position for text
1

You use a wx.MemoryDC

1 Comment

There are LOADS of examples in the wxPython demos. just a heads up

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.