Out of interest I had a look at drawing text in a wxGraphicsContext. Previously I have used the following to create a GraphicsContext from a DC:
gcdc = wx.GCDC(dc)
gc = gcdc.GetGraphicsContext()
But the documentation for GraphicsContext says you can also do:
gc = wx.GraphicsContext.Create(dc)
The GraphicsContext.CreateFont() method takes a float for the sizeInPixels arg, whereas the wx.Font() constructor takes an int for the pointSize arg. So the sizes will be different.
However, since wxPython version 4.1/wxWidgets-3.1.2, wx.Font has a SetFractionalPointSize() method which does take a float as the pointSize arg.
The following code experiments with these ideas:
import wx
class TestPanel(wx.Panel):
def __init__(self, *args, **kw):
wx.Panel.__init__(self, *args, **kw)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def Draw(self, dc):
dc.SetBackground(wx.Brush("sky blue"))
dc.Clear()
# gcdc = wx.GCDC(dc)
# gc = gcdc.GetGraphicsContext()
gc = wx.GraphicsContext.Create(dc)
gc_font = gc.CreateFont(11.7, facename='')
gc.SetFont(gc_font)
gc.DrawText("gc.DrawText()", 20, 20)
wx_font = wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.NORMAL)
dc.SetFont(wx_font)
dc.DrawText("dc.DrawText()", 20, 60)
wx_font.SetFractionalPointSize(11.7)
dc.SetFont(wx_font)
dc.DrawText("dc.DrawText()", 20, 80)
wx_font.SetFractionalPointSize(12.0)
dc.SetFont(wx_font)
dc.DrawText("dc.DrawText()", 20, 100)
def OnPaint(self, evt):
dc = wx.PaintDC(self)
self.Draw(dc)
if __name__ == '__main__':
app = wx.App()
frame = wx.Frame(None, title="Graphics Context Test", size=(300, 200))
pnl = TestPanel(frame)
frame.Show()
app.MainLoop()
Tested on Python 3.8.10 + wxPython 4.1.1 gtk3 (phoenix) wxWidgets 3.1.5 + Linux Mint 20.2