How to draw text in a bitmap using wxpython?

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

How can I do this 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 is missing here ..

+3
source share
2 answers

Select bmp in wx.MemoryDC, draw something on this dc, and then select this bitmap for example.

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()
+5
source

You are using wx.MemoryDC

+1
source

Source: https://habr.com/ru/post/1739960/


All Articles