示例#1
0
// This function is used for all events destined for Python event handlers.
void wxPyCallback::EventThunker(wxEvent& event) {
    wxPyCallback*   cb = (wxPyCallback*)event.m_callbackUserData;
    PyObject*       func = cb->m_func;
    PyObject*       result;
    PyObject*       arg;
    PyObject*       tuple;
    bool            checkSkip = false;

    wxPyThreadBlocker blocker;
    wxString className = event.GetClassInfo()->GetClassName();
    arg = wxPyConstructObject((void*)&event, className);
    
    if (!arg) {
        PyErr_Print();
    } else {
        // Call the event handler, passing the event object
        tuple = PyTuple_New(1);
        PyTuple_SET_ITEM(tuple, 0, arg);  // steals ref to arg
        result = PyEval_CallObject(func, tuple);
        if ( result ) {
            Py_DECREF(result);   // result is ignored, but we still need to decref it
            PyErr_Clear();       // Just in case...
        } else {
            PyErr_Print();
        }
        Py_DECREF(tuple);
    }
}
示例#2
0
static void callPythonDelegate(PyObject* delegate, wxWindow* window)
{
    // create a Python compatible wxWindow from the one we have
    PyObject* pywin = wxPyConstructObject((void*)window, wxT("wxWindow"), false);

    if (!pywin) {
        fprintf(stderr, "could not create wxWindow PyObject*\n");
        return;
    }

    // pass the wxWindow associated with this event to the Python function
    PyObject* res = PyObject_CallFunctionObjArgs(delegate, win, 0);
    Py_XDECREF(res);
    Py_DECREF(pywin);

    if (PyErr_Occurred()) {
        // print any exceptions that occurred
        PyErr_Print();
        return false;
    } else {
        // if the function succeeded, return true--meaning we handled this event.
        return true;
    }
}