我正在开发一个使用wxPython和Cairo编写的新项目,以便在画布上动态绘制对象。一切都运作良好,虽然我注意到wxPython的一个重要提示,我正在努力工作。我对wxPython很新,对开罗来说很新,所以答案可能很明显,但我似乎无法在任何地方找到它。
我遇到的问题是,当调用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)
答案 0 :(得分:0)
我按照Mike Driscoll的指示去做(in the comment above)并在 wxPython小组和开罗邮件列表上发布了我的问题,以及开罗名单成员的答复是:
看看wxWindowDC,wxClientDC。
在查阅这些课程后,我读到了:
如果应用程序希望从OnPaint事件外部绘制窗口的客户区域,则必须构造wxClientDC。这通常应该构造为临时堆栈对象;不要存储wxClientDC对象。
这正是我想要的。我希望这可以帮助任何人在将来遇到同样的问题。
谢谢! Jeremy Overman