Python/wxPython/Graphics: Difference between revisions

From Fundamental Ramen
Jump to navigation Jump to search
 
(12 intermediate revisions by the same user not shown)
Line 1: Line 1:
= wx.DC hierarchy =
= Known Issues =
<quickgv name="DC" theme="warm>
* wx.DC cannot use matrix on macOS
rankdir=BT;
* pycairo cannot use FreeType font
* wx.lib.wxcairo.FontFaceFromFont(wxfont) is the best way to set font
* If draw something using both wx.DC and cairo.Context, some shape or text would be disappered.
 
= Set font and show text =
<source lang="python" line="true" highlight="21-31">
import subprocess
 
import cairo
import wx
import wx.lib.wxcairo
 
WIDTH = 400
HEIGHT = 300
 
surface = cairo.ImageSurface(
    cairo.FORMAT_RGB24,
    WIDTH,
    HEIGHT
)
ctx = cairo.Context(surface)
ctx.rectangle(0, 0, WIDTH, HEIGHT)
ctx.set_source_rgb(0.8, 0.8, 1)
ctx.fill()


A [label="wx.DC"];
# Drawing code
B [label="wx.WindowDC"];
app = wx.App()
C [label="wx.ClientDC"];
wfont = wx.Font(
D [label="wx.PaintDC"];
    13,
    wx.FONTFAMILY_DEFAULT,
    wx.FONTSTYLE_NORMAL,
    wx.FONTWEIGHT_NORMAL,
    False,
    '蘋果儷細宋'
)
cfont = wx.lib.wxcairo.FontFaceFromFont(wfont)
ctx.set_font_face(cfont)
ctx.set_font_size(13)
ctx.set_source_rgb(0, 0, 0)
ctx.move_to(20, 80)
ctx.show_text('Your grand mom, 林啊罵勒')
# End of drawing code


D -> C;
surface.write_to_png('text.png')
C -> B;
subprocess.run(['open', 'text.png'])
B -> A;
</source>
</quickgv>


= Draw line by PaintDC =
= Draw line by Cairo =
<source lang="python" line="true" highlight="8,13-15">
* [https://cairographics.org/documentation/pycairo/2/reference/context.html#class-context Context class of PyCairo]
* [https://cairocffi.readthedocs.io/en/stable/api.html#context Context class of CairoCFFI]
<source lang="python" line="true" highlight="14-23">
#!/usr/bin/env python3
#!/usr/bin/env python3
import wx
import wx
import wx.lib.wxcairo


class MyApp(wx.App):
class MyApp(wx.App):
Line 28: Line 66:


     def OnPaint(self, evt):
     def OnPaint(self, evt):
         dc = wx.PaintDC(self.mainFrame)
         # dc = wx.BufferedPaintDC(self.mainFrame)
         dc.DrawLine(10, 10, 200, 10)
        # dc = wx.PaintDC(self.mainFrame)
         # dc = wx.ClientDC(self.mainFrame)
        dc = wx.WindowDC(self.mainFrame)
        ctx = wx.lib.wxcairo.ContextFromDC(dc)
        ctx.set_line_width(5)
        ctx.move_to(10, 20)
        ctx.line_to(200, 20)
        ctx.stroke()


if __name__ == '__main__':
if __name__ == '__main__':
Line 36: Line 81:
</source>
</source>


= Draw line by Cairo =
= Draw line by PaintDC =
* [https://cairographics.org/documentation/pycairo/2/reference/context.html#class-context Context class of PyCairo]
<source lang="python" line="true" highlight="8,13-15">
* [https://cairocffi.readthedocs.io/en/stable/api.html#context Context class of CairoCFFI]
<source lang="python" line="true" highlight="14-20">
#!/usr/bin/env python3
#!/usr/bin/env python3
import wx
import wx
import wx.lib.wxcairo


class MyApp(wx.App):
class MyApp(wx.App):
Line 55: Line 97:
     def OnPaint(self, evt):
     def OnPaint(self, evt):
         dc = wx.PaintDC(self.mainFrame)
         dc = wx.PaintDC(self.mainFrame)
         ctx = wx.lib.wxcairo.ContextFromDC(dc)
         dc.DrawLine(10, 10, 200, 10)
        ctx.set_line_width(5)
        ctx.move_to(10, 20)
        ctx.line_to(200, 20)
        ctx.stroke()


if __name__ == '__main__':
if __name__ == '__main__':
Line 65: Line 103:
     app.MainLoop()
     app.MainLoop()
</source>
</source>
= wx.DC hierarchy =
<quickgv name="DC" theme="warm>
rankdir=BT;
A [label="wx.DC"];
B1 [label="wx.WindowDC"];
B2 [label="wx.MemoryDC"];
B3 [label="wx.SVGFileDC"];
B4 [label="wx.MetaFileDC"];
B5 [label="wx.MirrorDC"];
B6 [label="wx.PostScriptDC"];
B7 [label="wx.PrinterDC"];
B8 [label="wx.ScreenDC"];
C [label="wx.ClientDC"];
D [label="wx.PaintDC"];
E [label="wx.BufferedDC"];
F [label="wx.BufferedPaintDC"];
G [label="wx.AutoBufferedPaintDC"];
D -> C;
C -> B1;
{B1 B2 B3 B4 B5 B6 B7 B8} -> A;
G -> F -> E -> B2;
</quickgv>

Latest revision as of 01:07, 11 April 2019

Known Issues

  • wx.DC cannot use matrix on macOS
  • pycairo cannot use FreeType font
  • wx.lib.wxcairo.FontFaceFromFont(wxfont) is the best way to set font
  • If draw something using both wx.DC and cairo.Context, some shape or text would be disappered.

Set font and show text

import subprocess

import cairo
import wx
import wx.lib.wxcairo

WIDTH = 400
HEIGHT = 300

surface = cairo.ImageSurface(
    cairo.FORMAT_RGB24,
    WIDTH,
    HEIGHT
)
ctx = cairo.Context(surface)
ctx.rectangle(0, 0, WIDTH, HEIGHT)
ctx.set_source_rgb(0.8, 0.8, 1)
ctx.fill()

# Drawing code
app = wx.App()
wfont = wx.Font(
    13,
    wx.FONTFAMILY_DEFAULT,
    wx.FONTSTYLE_NORMAL,
    wx.FONTWEIGHT_NORMAL,
    False,
    '蘋果儷細宋'
)
cfont = wx.lib.wxcairo.FontFaceFromFont(wfont)
ctx.set_font_face(cfont)
ctx.set_font_size(13)
ctx.set_source_rgb(0, 0, 0)
ctx.move_to(20, 80)
ctx.show_text('Your grand mom, 林啊罵勒')
# End of drawing code

surface.write_to_png('text.png')
subprocess.run(['open', 'text.png'])

Draw line by Cairo

#!/usr/bin/env python3
import wx
import wx.lib.wxcairo

class MyApp(wx.App):

    def OnInit(self):
        frame = wx.Frame(None, -1, 'Hello World!')
        frame.Bind(wx.EVT_PAINT, self.OnPaint)
        frame.Show()
        self.mainFrame = frame
        return True

    def OnPaint(self, evt):
        # dc = wx.BufferedPaintDC(self.mainFrame)
        # dc = wx.PaintDC(self.mainFrame)
        # dc = wx.ClientDC(self.mainFrame)
        dc = wx.WindowDC(self.mainFrame)
        ctx = wx.lib.wxcairo.ContextFromDC(dc)
        ctx.set_line_width(5)
        ctx.move_to(10, 20)
        ctx.line_to(200, 20)
        ctx.stroke()

if __name__ == '__main__':
    app = MyApp()
    app.MainLoop()

Draw line by PaintDC

#!/usr/bin/env python3
import wx

class MyApp(wx.App):

    def OnInit(self):
        frame = wx.Frame(None, -1, 'Hello World!')
        frame.Bind(wx.EVT_PAINT, self.OnPaint)
        frame.Show()
        self.mainFrame = frame
        return True

    def OnPaint(self, evt):
        dc = wx.PaintDC(self.mainFrame)
        dc.DrawLine(10, 10, 200, 10)

if __name__ == '__main__':
    app = MyApp()
    app.MainLoop()

wx.DC hierarchy