程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Qt中調用Python的異常

編輯:Python

**問題遇到的現象和發生背景 **
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.py中的六個函數,一個QLabel顯示結果
運行結果及報錯內容
程序可以運行

但是點擊按鈕的的時候,會在應用程序輸出欄輸出電腦上某輸入法的路徑(莫名其妙),然後程序crash

** 我的解答思路和嘗試過的方法 **
不知道為什麼會出現這種錯誤,無從下手

我想要達到的結果
程序實現應有功能,正常運行


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved