**問題遇到的現象和發生背景 **
Qt 5.15.2 + Python 3.8
使用Python/C API實現C++和Python間的交互
問題相關代碼
Main.cpp:
#include
#include "mainwindow.h"#include
#include
int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; //show the UI w.show(); return a.exec();}
MainWindow.cpp:
#include <Python.h>#include "mainwindow.h"#include "ui_mainwindow.h"#include
#
include
#
include
#
include
#
include
#
include
#
include
MainWindow::
MainWindow(QWidget *parent) :
QMainWindow(parent) , ui(
new Ui::MainWindow){ ui->setup
Ui(this);
//num init
char *num1, *num2;
//Python config
//Python init
Py_Initialize();
//init the python interpreter
if(!
Py_IsInitialized())
//succeed or not { q
Debug() <<
"Python init failed."; }
//add path into sys
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.insert(0,'.')");
//PyObject_init PyObject *
module;
// module and func
module =
PyImport_ImportModule("MathPy");
//import module
//Menubar -> exit connect(ui->actionexit,&QAction::triggered,this,
[=]
(){
Py_Finalize(); this->close
(); });
//num -> change connect(ui->EditNum1,&QLineEdit::textChanged,this,
[&]
(){ num1 = ui->EditNum1->text
().
to
Utf8().data
(); }); connect(ui->EditNum2,&QLineEdit::textChanged,this,
[&]
(){ num1 = ui->EditNum2->text
().
to
Utf8().data
(); });
//six buttons connect(ui->ButtonPlus,&QPushButton::clicked,this,
[&]
(){ PyObject *func =
PyObject_GetAttrString(module,"plus"); PyObject *result =
PyObject_CallFunction(func,num1,num2);
int res =
PyLong_AsLong(result); ui->LabelResult->set
Text(QString(res)); }); connect(ui->ButtonSub,&QPushButton::clicked,this,
[&]
(){ PyObject *func =
PyObject_GetAttrString(module,"sub"); PyObject *result =
PyObject_CallFunction(func,num1,num2);
int res =
PyLong_AsLong(result); ui->LabelResult->set
Text(QString(res)); }); connect(ui->ButtonMulti,&QPushButton::clicked,this,
[&]
(){ PyObject *func =
PyObject_GetAttrString(module,"multi"); PyObject *result =
PyObject_CallFunction(func,num1,num2);
int res =
PyLong_AsLong(result); ui->LabelResult->set
Text(QString(res)); }); connect(ui->ButtonDiv,&QPushButton::clicked,this,
[&]
(){ PyObject *func =
PyObject_GetAttrString(module,"div"); PyObject *result =
PyObject_CallFunction(func,num1,num2);
int res =
PyLong_AsLong(result); ui->LabelResult->set
Text(QString(res)); }); connect(ui->ButtonMax,&QPushButton::clicked,this,
[&]
(){ PyObject *func =
PyObject_GetAttrString(module,"max"); PyObject *result =
PyObject_CallFunction(func,num1,num2);
int res =
PyLong_AsLong(result); ui->LabelResult->set
Text(QString(res)); }); connect(ui->ButtonMin,&QPushButton::clicked,this,
[&]
(){ PyObject *func =
PyObject_GetAttrString(module,"min"); PyObject *result =
PyObject_CallFunction(func,num1,num2);
int res =
PyLong_AsLong(result); ui->LabelResult->set
Text(QString(res)); });}MainWindow::~
MainWindow(){ delete ui;}
MathPy.py
def plus(num1,num2): return num1+num2def sub(num1,num2): return num1-num2def multi(num1,num2): return num1*num2def div(num1,num2): return num1/num2def max(num1,num2): if num1>num2: return num1 else: return num2def min(num1,num2): if num1>num2: return num2 else: return num1
ui有兩個QLineEdit,分別輸入兩個數字,6個QPushButton分別對應MathPy.pysix functions in,一個QLabel顯示結果
運行結果及報錯內容
程序可以運行
But when the button is clicked,It will output the path of an input method on the computer in the application output bar(莫名其妙),然後程序crash
** 我的解答思路和嘗試過的方法 **
Don't know why this error occurs,無從下手
我想要達到的結果
The program realizes the functions it should have,正常運行