查看完整版本: [-- 为什么qt 调用python脚本,里面的print函数,有时候可以在qt控制台上打印出来,有时候不可以呢?? --]

QTCN开发网 -> Python Qt GUI快速编程 -> 为什么qt 调用python脚本,里面的print函数,有时候可以在qt控制台上打印出来,有时候不可以呢?? [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

foxgod 2019-11-26 17:11

为什么qt 调用python脚本,里面的print函数,有时候可以在qt控制台上打印出来,有时候不可以呢??

  代码如下
  1. Py_Initialize();
         if ( !Py_IsInitialized() )
         {
         return -1;
         }
         // 将Python工作路径切换到待调用模块所在目录,一定要保证路径名的正确性
         PyRun_SimpleString("import sys");
         QString setSysPath = QString("sys.path.append('%1')").arg(QCoreApplication::applicationDirPath());
         PyRun_SimpleString(setSysPath.toStdString().c_str());

         PyObject* pModule = PyImport_ImportModule("server");  // 这里的test_py就是创建的python文件
         if (!pModule)
         {
                 qDebug()<< "Cant open python file!\n" << endl;
                 return -1;
         }
         PyObject* pFunhello= PyObject_GetAttrString(pModule,"usps");  // 这里的usps就是python文件定义的函数
         if(!pFunhello)
         {
             qDebug()<<"Get function hello failed"<<endl;
             return -1;
         }
         else
         {

           PyObject_CallFunction(pFunhello,NULL);
           while(true)
           {
               PyObject* pFunhello1= PyObject_GetAttrString(pModule,"udp_msglist");  // 这里的hellow就是python文件定义的函数

               if(!pFunhello1)
               {
                   qDebug()<<"Get function udp_msglist failed"<<endl;
                   return -1;
               }
               PyObject* pReturn= PyObject_CallFunction(pFunhello1,NULL);
               char* cc=NULL;
               int result=-1;
               if(pReturn==NULL)
               {
                   qDebug()<<"cc is null";
               }
               else
               {
                   //PyArg_Parse(pReturn,"s",&cc);
                   PyArg_ParseTuple(pReturn,"s|i",&cc,&result);
                   if(!QString(QLatin1String(cc)).isEmpty())
                   {
                       qDebug()<<"cc is"<<QString(QLatin1String(cc))<<result;
                   }
               }

           }
         }
调用附件里面的py文件,print输出的log,怎么样可以在QT应用程序输出里面看到呢??,我实验了集中程序,控制台程序可以答应出来,窗口程序就打印不出来,什么原因[attachment=21107]


nishiwoenaad 2019-11-29 11:20
  • 环境配置

1,确认在.pro文件中添加python的lib库
  • 运行

  1. [size=3][color=#4d4d4d][font= ]Py_SetPythonHome(“python路径”); //指定python路径[/font][/color][/size]
        Py_Initialize();
        if ( !Py_IsInitialized() )
        {
            return ;
        }
        //导入hello.py模块
        PyObject* pModule = PyImport_ImportModule("hello");
        if (!pModule)
        {
            return ;
        }
        //获取hello模块中的hello函数
        PyObject* pFunhello= PyObject_GetAttrString(pModule,"hello");

        if(!pFunhello)
        {
            return ;
        }
        //调用hello函数
        PyObject_CallFunction(pFunhello,NULL);
        //结束,释放python
        Py_Finalize();




guihua8000 2020-08-28 10:55
              


查看完整版本: [-- 为什么qt 调用python脚本,里面的print函数,有时候可以在qt控制台上打印出来,有时候不可以呢?? --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled