我正在开发一个使用 wxPython 和 Cairo 编写的新项目,以便在画布上动态绘制对象。一切都很好,尽管我注意到 wxPython 有一个很大的警告,我很难解决这个问题。我对 wxPython 很陌生,对 Cairo 也很陌生,所以答案可能很明显,但我似乎无法在任何地方找到它。
我遇到的问题是,当调用 wx.EVT_PAINT 时,您似乎只能在图形上下文(我相信是 wx.GraphicsContext)上绘制,尽管我需要外部类才能绘制到画布容易地。
我最初的计划是创建 Cairo 上下文,然后将其存储在画布所在的类中,然后任何需要访问上下文进行绘制的类都可以使用 canvas.ctx 之类的东西简单地获取它。
我的总体问题是,有没有什么方法可以访问开罗上下文而不需要调用 wx.EVT_PAINT?
这是我现在正在使用的代码:
import wx
import CairoAPI
class Canvas(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "test", pos=(0, 0), size=(640,480))
#self.ShowFullScreen(1)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def DrawRectangle(self):
pass
def OnPaint(self, event):
dc = wx.PaintDC(self)
w,h = dc.GetSizeTuple()
gc = wx.GraphicsContext.Create(dc)
nc = gc.GetNativeContext()
ctx = CairoAPI.Context_FromSWIGObject(nc)
if __name__=="__main__":
app = wx.App()
canvas = Canvas()
canvas.Show()
app.MainLoop()
在这段代码中,我想要完成的是制作 canvas.DrawRectangle 并能够写入画布,而无需从 canvas.OnPaint 中调用该函数。
对不起,如果我让这个问题变得混乱。我昨晚刚开始学习开罗,但我对它的工作原理和某些事情的正确术语仍然有些陌生。
谢谢!
编辑:我忘记了一些代码:“CairoAPI”模块是我从教程中得到的(不记得是哪一个)。这是它的来源。
import ctypes
import cairo
from ctypes.util import find_library
cairo_dll = ctypes.CDLL(find_library("cairo"))
# Pycairo's API representation (from pycairo.h)
class Pycairo_CAPI(ctypes.Structure):
_fields_ = [
('Context_Type', ctypes.py_object),
('Context_FromContext', ctypes.PYFUNCTYPE(ctypes.py_object,
ctypes.c_void_p,
ctypes.py_object,
ctypes.py_object)),
('FontFace_Type', ctypes.py_object),
('FontFace_FromFontFace', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
('FontOptions_Type', ctypes.py_object),
('FontOptions_FromFontOptions', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
('Matrix_Type', ctypes.py_object),
('Matrix_FromMatrix', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
('Path_Type', ctypes.py_object),
('Path_FromPath', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
('Pattern_Type', ctypes.py_object),
('SolidPattern_Type', ctypes.py_object),
('SurfacePattern_Type', ctypes.py_object),
('Gradient_Type', ctypes.py_object),
('LinearGradient_Type', ctypes.py_object),
('RadialGradient_Type', ctypes.py_object),
('Pattern_FromPattern', ctypes.c_void_p),
('ScaledFont_Type', ctypes.py_object),
('ScaledFont_FromScaledFont', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
('Surface_Type', ctypes.py_object),
('ImageSurface_Type', ctypes.py_object),
('PDFSurface_Type', ctypes.py_object),
('PSSurface_Type', ctypes.py_object),
('SVGSurface_Type', ctypes.py_object),
('Win32Surface_Type', ctypes.py_object),
('XlibSurface_Type', ctypes.py_object),
('Surface_FromSurface', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
('Check_Status', ctypes.PYFUNCTYPE(ctypes.c_int, ctypes.c_int))]
# look up the API
ctypes.pythonapi.PyCObject_Import.restype = ctypes.POINTER(Pycairo_CAPI)
pycairo_api = ctypes.pythonapi.PyCObject_Import("cairo", "CAPI").contents
ContextType = pycairo_api.Context_Type
def Context_FromSWIGObject(swigObj):
ptr = ctypes.c_void_p(int(swigObj))
#increment the native context's ref count, since the Pycairo_Context decrements it
#when it is finalised.
cairo_dll.cairo_reference(ptr)
return pycairo_api.Context_FromContext(ptr, ContextType, None)