当前位置: 首页 > wzjs >正文

网站毕业设计模板google搜索首页

网站毕业设计模板,google搜索首页,图文网站建设,外贸网站建设公司排名1、使用QT Creator 新建项目 2、添加Python解释器 在.pro 文件中添加python头文件与链接库 INCLUDEPATH -I /usr/include/python3.8/ LIBS -L /usr/lib/python3.8/config-3.8-x86_64-linux-gnu -lpython3.8本文实验为ubuntu自带python3.8,虚拟环境中的python解释…

1、使用QT Creator 新建项目

2、添加Python解释器

在.pro 文件中添加python头文件与链接库

INCLUDEPATH += -I /usr/include/python3.8/
LIBS += -L /usr/lib/python3.8/config-3.8-x86_64-linux-gnu -lpython3.8

本文实验为ubuntu自带python3.8,虚拟环境中的python解释器运行python脚本未成功

3、Python脚本编写

import os
import sys
import matplotlib.pyplot as plt
import numpy as npprint(os.path.dirname(os.getcwd()))
print(sys.path.append(os.path.dirname(os.getcwd())))
def test_add():x = [0,1,2,3,4,5,6,7,8,9,10]y = [2.83, 9.53, 14.52, 21.57, 38.26, 53.92, 73.15, 101.56, 129.54, 169.75, 207.59]z = np.polyfit(x, y, 3)p = np.poly1d(z)y_pre = p(x)# 绘图plt.plot(x,y, '+')plt.plot(x,y_pre)# 显示plt.show()def add(a, b):c = a + bprint(f"{a}+{b}={c}")return (c,7.7)# test_add()
# add(2, 3)

4、 mainwindow.cpp内容编写

4.1 无参函数的调用

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#undef slots
#include "Python.h"
#define slotsMainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{Py_Initialize();if(!Py_IsInitialized()){qDebug() << "init err";}PyRun_SimpleString("import os");PyRun_SimpleString("import sys");PyRun_SimpleString("print(os.getcwd())");PyRun_SimpleString("sys.path.append(os.path.dirname(os.path.dirname(os.getcwd())))");//PyRun_SimpleString("print(os.path.dirname(os.path.dirname(os.getcwd())))");PyRun_SimpleString("print(sys.path)");PyObject * pmodle = PyImport_ImportModule("pyplot");if(!pmodle){qDebug() << "can not open the file";}PyObject * callback = PyObject_GetAttrString(pmodle, "test_add");PyObject * ret = PyObject_CallObject(callback, NULL);Py_Finalize();ui->setupUi(this);
}MainWindow::~MainWindow()
{delete ui;
}

 4.2 有参函数的调用

#include "mainwindow.h"
#include "ui_mainwindow.h"
#undef slots
#include <Python.h>
#define slots
#include <QDebug>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{Py_Initialize();if(!Py_IsInitialized()){qDebug() << "init err";}PyRun_SimpleString("import os");PyRun_SimpleString("import sys");PyRun_SimpleString("sys.path.append(os.path.dirname(os.path.dirname(os.getcwd())))");PyRun_SimpleString("print(os.getcwd())");PyRun_SimpleString("print(os.path.dirname(os.path.dirname(os.getcwd())))");PyRun_SimpleString("print(sys.path)");PyObject * pmodule = PyImport_ImportModule("pyplot");if(!pmodule){qDebug() << "can not open the file";}PyObject * callback = PyObject_GetAttrString(pmodule, "add");PyObject * args = PyTuple_New(2);// PyObject * arg1 = PyLong_FromLong(3);// PyObject * arg2 = PyLong_FromLong(6);PyObject * arg1 = PyFloat_FromDouble(3.3);PyObject * arg2 = PyFloat_FromDouble(6.6);PyTuple_SetItem(args, 0, arg1);PyTuple_SetItem(args, 1, arg2);PyObject * ret = PyObject_CallObject(callback, args);// long c_ret = PyLong_AsLong(ret);float c_ret = PyFloat_AsDouble(ret);qDebug() << "c_ret = " << c_ret;Py_Finalize();ui->setupUi(this);
}MainWindow::~MainWindow()
{delete ui;
}

 4.3 函数返回多个参数的调用

#include "mainwindow.h"
#include "ui_mainwindow.h"
#undef slots
#include <Python.h>
#define slots
#include <QDebug>
#include <QLineEdit>
#include <QPushButton>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{Py_Initialize();if(!Py_IsInitialized()){qDebug() << "init err";}PyRun_SimpleString("import os");PyRun_SimpleString("import sys");PyRun_SimpleString("sys.path.append(os.path.dirname(os.path.dirname(os.getcwd())))");PyRun_SimpleString("print(os.getcwd())");PyRun_SimpleString("print(os.path.dirname(os.path.dirname(os.getcwd())))");PyRun_SimpleString("print(sys.path)");PyObject * pmodule = PyImport_ImportModule("pyplot");if(!pmodule){qDebug() << "can not open the file";}PyObject * callback = PyObject_GetAttrString(pmodule, "add");PyObject * args = PyTuple_New(2);// PyObject * arg1 = PyLong_FromLong(3);// PyObject * arg2 = PyLong_FromLong(6);// 传入参数PyObject * arg1 = PyFloat_FromDouble(3.3);PyObject * arg2 = PyFloat_FromDouble(6.6);PyTuple_SetItem(args, 0, arg1);PyTuple_SetItem(args, 1, arg2);PyObject * ret = PyObject_CallObject(callback, args);// 传出参数// long c_ret = PyLong_AsLong(ret);// float c_ret = PyLong_AsLong(ret);// qDebug() << "c_ret = " << c_ret;double ret1, ret2;PyArg_ParseTuple(ret, "d|d", &ret1, &ret2);qDebug() << "ret1 = " << ret1 << " ret2 = " << ret2;ui->setupUi(this);
}MainWindow::~MainWindow()
{Py_Finalize();delete ui;
}

4.4  QT按键调用python函数

 4.4.1 mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#undef slots
#include <Python.h>
#define slots
#include <QDebug>
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private:Ui::MainWindow *ui;QLineEdit * A_par;QLineEdit * B_par;QLineEdit * C_par;QPushButton * func_btn;QGridLayout * Grid;PyObject * callback;PyObject * args;PyObject * arg1;PyObject * arg2;PyObject * ret;PyObject * pmodule;private slots:void python_func();};
#endif // MAINWINDOW_H

 4.4.2 mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);Py_Initialize();if(Py_IsInitialized()){qDebug() << "init ok";}PyRun_SimpleString("import os");PyRun_SimpleString("import sys");PyRun_SimpleString("sys.path.append(os.path.dirname(os.path.dirname(os.getcwd())))");PyRun_SimpleString("print(os.getcwd())");PyRun_SimpleString("print(os.path.dirname(os.path.dirname(os.getcwd())))");PyRun_SimpleString("print(sys.path)");pmodule = PyImport_ImportModule("pyplot");if(pmodule){PyErr_Print();qDebug() << "file is ok";}callback = PyObject_GetAttrString(pmodule, "add");A_par = new QLineEdit();B_par = new QLineEdit();C_par = new QLineEdit();func_btn = new QPushButton("=");Grid = new QGridLayout();Grid->addWidget(A_par, 0, 1);Grid->addWidget(B_par, 0, 2);Grid->addWidget(C_par, 1, 1);Grid->addWidget(func_btn, 0, 3);this->centralWidget()->setLayout(Grid);connect(func_btn, SIGNAL(clicked()), this, SLOT(python_func()));}MainWindow::~MainWindow()
{Py_Finalize();delete ui;
}void MainWindow::python_func()
{double A = A_par->text().toDouble();double B = B_par->text().toDouble();args = PyTuple_New(2);// PyObject * arg1 = PyLong_FromLong(3);// PyObject * arg2 = PyLong_FromLong(6);// 传入参数arg1 = PyFloat_FromDouble(A);arg2 = PyFloat_FromDouble(B);PyTuple_SetItem(args, 0, arg1);PyTuple_SetItem(args, 1, arg2);ret = PyObject_CallObject(callback, args);// 传出参数// long c_ret = PyLong_AsLong(ret);// float c_ret = PyLong_AsLong(ret);// qDebug() << "c_ret = " << c_ret;double ret1, ret2;PyArg_ParseTuple(ret, "d|d", &ret1, &ret2);qDebug() << "ret1 = " << ret1 << " ret2 = " << ret2;C_par->setText(QString::number(ret1));
}

 4.4.3 main.cpp 

#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}

 运行结果:


文章转载自:

http://FMoCTs3E.kpcdc.cn
http://boZSZOY3.kpcdc.cn
http://Jkv74v7n.kpcdc.cn
http://Jawp0jK1.kpcdc.cn
http://yU4lnfTq.kpcdc.cn
http://6ygGX1Hk.kpcdc.cn
http://5G5aQ0dn.kpcdc.cn
http://nL77raW6.kpcdc.cn
http://4aAQsrEG.kpcdc.cn
http://a9fCF3Hf.kpcdc.cn
http://nUJ2MMb6.kpcdc.cn
http://C7RVpZiB.kpcdc.cn
http://jCnj3XAO.kpcdc.cn
http://5C3rc6RN.kpcdc.cn
http://bBPYaeRp.kpcdc.cn
http://b0yaOsIn.kpcdc.cn
http://G6wPCoOb.kpcdc.cn
http://YhPz3Bus.kpcdc.cn
http://f6Ahje9x.kpcdc.cn
http://qWWREs6E.kpcdc.cn
http://YG3MJM13.kpcdc.cn
http://ICUUJjjw.kpcdc.cn
http://mal20Qbe.kpcdc.cn
http://SOR83ZFy.kpcdc.cn
http://qz36K2S1.kpcdc.cn
http://O5N3RwdN.kpcdc.cn
http://st6pgvPa.kpcdc.cn
http://zn94I1r3.kpcdc.cn
http://Lq8GBlCc.kpcdc.cn
http://yw3aOuB0.kpcdc.cn
http://www.dtcms.com/wzjs/661597.html

相关文章:

  • 如何做旅游网站的思维导图168推广
  • 离型剂技术支持东莞网站建设做电台用啥什么网站
  • 广东网站建设网站免费智能seo收录工具
  • 口岸地区网站建设内容360收录提交申请
  • 高端企业网站要多少钱wordpress分类目录和页面
  • 为什么我的网站没有百度索引量服装搭配网站建设策划书
  • 停止wordpress东莞网站优化排名
  • 泰安网站建设企业wordpress建站免费教程
  • 如何进行电子商务网站推广?怎么利用wordpress管理站点
  • 建材做哪些网站适合机械网站的wordpress主题模板
  • 细谈电商网站外链建设的策略电脑建设网站服务器
  • 网站图片alt属性国外做名片网站
  • 中国建设很行河北省分行合作网站推销什么企业做网站和app
  • 婚纱网站php牛商网站建设
  • 有口碑的合肥网站建设网上商城用wordpress
  • 班组建设展板哪个网站有知更鸟WordPress用户中心
  • 网站平台建设如何做一元购物网站
  • 网站屏蔽右键网站设置超链接代码
  • 个旧市城乡建设局网站福田蒙派克图片
  • 聊城哪儿做网站便宜app开发价格公司
  • 生活服务网站开发网站建设公司业务提成多少
  • 做网站 科目西部数码网站开发管理助手
  • 西南大学校园网站建设往年考试卷网站开发用几种字体
  • 个人公司网站模板网站建设捌金手指花总十九
  • 专业的网站建设公泰安网站建设总结
  • 网站百度不到验证码怎么办怎样编辑网页
  • 企业网站优化的三层含义电子商务网站数据库建设
  • 关于网站制作的指标中南建设网官方网站
  • wordpress站点如何添加百度分享代码手机装修设计软件
  • php网站建设的公司国家建设协会官方网站