Python/wxPython/Graphics: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
| Line 1: | Line 1: | ||
= | = Draw line by Cairo = | ||
* [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"> | |||
<source lang="python" line="true" highlight=" | |||
#!/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 39: | Line 17: | ||
def OnPaint(self, evt): | def OnPaint(self, evt): | ||
dc = wx.PaintDC(self.mainFrame) | # dc = wx.BufferedPaintDC(self.mainFrame) | ||
dc. | # 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 47: | Line 32: | ||
</source> | </source> | ||
= Draw line by | = Draw line by PaintDC = | ||
<source lang="python" line="true" highlight="8,13-15"> | |||
<source lang="python" line="true" highlight=" | |||
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||
import wx | import wx | ||
class MyApp(wx.App): | class MyApp(wx.App): | ||
| Line 65: | Line 47: | ||
def OnPaint(self, evt): | def OnPaint(self, evt): | ||
dc = wx.PaintDC(self.mainFrame) | |||
dc.DrawLine(10, 10, 200, 10) | |||
if __name__ == '__main__': | if __name__ == '__main__': | ||
| Line 79: | Line 54: | ||
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> | |||
Revision as of 03:21, 22 March 2019
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()