首页 > 编程知识 正文

qt嵌入python解释器的简单介绍

时间:2023-12-27 22:27:40 阅读:326473 作者:AQPC

本文目录一览:

QT中调用Python

如果你是嵌入的话,应该没问题。python的库会暴露c api的,你的qt程序照着python文档中的embedded章节就好。 看样子,你的是linux下的,不过一般头文件就是include/python.h,怎么会是include/python2.7呢?你写错了吧。库文件应该是在lib下面。

在qt程序中怎么运行一个python程序

因为process.start()函数实质是采用命令行中"start XXX"的方式启动XXX。这里的XXX仅仅适用于大部分的exe可执行文件以及一些常用文件。对于py或者pyw文件都不行。

有两种解决方案:

另外编写一个bat用来启动py文件,就可以使用start启动这个bat就可以了;

使用另一个函数“QDesktopServices::openUrl”来启动py文件。具体使用方法请自行百度。不做过多说明。

如何嵌入在一个PyQt的窗口小部件一个Python解释器

'''

Created on 18-03-2012

@author: Paweł Jarosz

'''

import os, sys

import atexit

from PySide import QtCore, QtGui

from IPython.zmq.ipkernel import IPKernelApp

from IPython.lib.kernel import find_connection_file, connect_qtconsole

from IPython.frontend.qt.kernelmanager import QtKernelManager

from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget

from IPython.config.application import catch_config_error

class IPythonLocalKernelApp(IPKernelApp):

 """IPython kernel application with nonblocking loop, running in dedicated thread.

 example:

  app = QtGui.QApplication([])

  kernelapp = IPythonLocalKernelApp.instance()

  kernelapp.start()

  namespace = kernelapp.get_user_namespace()

  namespace["QtGui"]=QtGui

  namespace["QtCore"]=QtCore

  app.exec_()"""

 #DEFAULT_INSTANCE_ARGS starting commandline

 DEFAULT_INSTANCE_ARGS = ['qtconsole','--pylab=inline', '--colors=linux']

 @catch_config_error

 def initialize(self, argv=None):

  super(IPythonLocalKernelApp, self).initialize(argv)

  self.kernel.eventloop = self.loop_qt4_nonblocking

 def loop_qt4_nonblocking(self, kernel):

  """Non-blocking version of the ipython qt4 kernel loop"""

  kernel.timer = QtCore.QTimer()

  kernel.timer.timeout.connect(kernel.do_one_iteration)

  kernel.timer.start(1000*kernel._poll_interval)

 def start(self, argv=DEFAULT_INSTANCE_ARGS):

  """Starts IPython kernel app

   argv: arguments passed to kernel

  """

  self.initialize(argv)

  self.heartbeat.start()

  if self.poller is not None:

   self.poller.start()

  self.kernel.start()

 def get_connection_file(self):

  """Returne current kernel connection file."""

  return self.connection_file

 def get_user_namespace(self):

  """Returns current kernel userspace dict"""

  return self.kernel.shell.user_ns

class IPythonConsoleQtWidget(RichIPythonWidget):

 """Ipython console Qt4+ widget

  Usage example:

   app = QtGui.QApplication([])

   kernelapp = IPythonLocalKernelApp.instance()

   kernelapp.start()

   namespace = kernelapp.get_user_namespace()

   widget = IPythonConsoleQtWidget()

   widget.set_default_style(colors='linux')

   widget.connect_kernel(connection_file=kernelapp.get_connection_file())

   # if you won't to connect to remote kernel:

   widget.connect_kernel(connection_file='kernel-16098.json')

   widget.show()

   namespace["widget"] = widget

   namespace["QtGui"]=QtGui

   namespace["QtCore"]=QtCore

   app.exec_()"""

 _connection_file = None

 def __init__(self, *args, **kw):

  RichIPythonWidget.__init__(self, *args, **kw)

  self._existing = True

  self._may_close = False

  self._confirm_exit = False

 def _init_kernel_manager(self):

  km = QtKernelManager(connection_file=self._connection_file, config=self.config)

  km.load_connection_file()

  km.start_channels(hb=self._heartbeat)

  self.kernel_manager = km

  atexit.register(self.kernel_manager.cleanup_connection_file)

 def connect_kernel(self, connection_file, heartbeat=False):

  """Connect's to ipython kernel.

  connection_file - connection file to use

  heartbeat   - should start heartbeat server? Workaround for problems with inproc embedded kernels

        (right click save image as/save as html kills kernel heartbeat/pool(??) serwer """

  self._heartbeat = heartbeat

  if os.path.exists(connection_file):

   self._connection_file = connection_file

  else:

   self._connection_file = find_connection_file(connection_file)

  self._init_kernel_manager()

app = QtGui.QApplication([])

kernelapp = IPythonLocalKernelApp.instance()

kernelapp.start()

namespace = kernelapp.get_user_namespace()

widget = IPythonConsoleQtWidget()

widget.set_default_style(colors='linux')

widget.connect_kernel(connection_file=kernelapp.get_connection_file())

# if you won't to connect to remote kernel:

# widget.connect_kernel(connection_file='kernel-16098.json')

widget.show()

namespace["widget"] = widget

namespace["QtGui"]=QtGui

namespace["QtCore"]=QtCore

app.exec_()

 IPython的一个小部件。

 首先,启动一个内核kernelapp = IPythonLocalKernelApp.instance()

kernelapp.start()

 接下来,创建并连接IPython的控制台小工具widget = IPythonConsoleQtWidget()

widget.set_default_style(colors='linux')

widget.connect_kernel(connection_file=kernelapp.get_connection_file())

# if you won't to connect to remote kernel:

# widget.connect_kernel(connection_file='kernel-16098.json')

widget.show()

 代码是基于IPython的0.12稳定。

有与右击另存为HTML问题

Qt中怎么可以使用python吗

这个看你要求了,如果只是单纯调用,和Qt没什么关系,直接看python手册中C、C++调用python相关的内容就行了。

如果你的python代码中还需要操作Qt相关的东西,那么你需要的 PythonQt 这个第三方的模块(概念上类似于Qt自己的QtScript模块)

Qt下无法调用python,打不开文件为什么

#include QCoreApplication

//包含调用Python相应的头文件

#include Python.h

int main(int argc, char *argv[])

{

 QCoreApplication a(argc, argv);

//初始化Python解释器,这是调用操作的第一步

 Py_Initialize();

 if( !Py_IsInitialized() ){

 return -1;

 }

//执行单句Python语句,用于给出调用模块的路径,否则将无法找到相应的调用模块

 PyRun_SimpleString("import sys");

 PyRun_SimpleString("sys.path.append('./')");

//获取qt_python_fun.py模块的指针

 PyObject* pModule = PyImport_ImportModule("qt_python_fun");

 if (! pModule){

 printf("Can't open python filen");

 return -1;

 }

//获取hello函数的指针

 PyObject* pFunhello = PyObject_GetAttrString(pModule,"hello");

 if (!pFunhello){

 printf("Get function hello failedn");

 return -1;

 }

//调用函数,传入参数为NULL

 PyObject_CallFunction(pFunhello,NULL);

//销毁Python解释器,这是调用的最后一步

 Py_Finalize();

 return a.exec();

}

作者:sleepyjoker

链接:

来源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

我使用QT做好了界面,用python写好了功能,怎么把两者连接起来呢?

答:我知道目前你应该就是用qt designer设计好了界面,然后你的Python代码是PyCharm,那么要将两者连接起来的话,有两种方法。

使用pyuic功能,这个功能需要你在PyCharm上面配置一下pyqt,可以将Qt的UI文件转化为一个Python的类,然后你就可以直接在你的工程里面引用这个类;

无需将UI文件转化为Python中的类,而只需要直接加载该UI文件就可以了,使用如下代码,如图红框所示,其中涉及到PyQt.uic的loadUi类。

直接加载UI文件

希望对你有帮助~

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。