python-2.7 - 如何在 PyQT 应用程序中集成 Ipython 控制台

标签 python-2.7 pyqt ipython

我正在为我的实验室开发 PyQt 软件。在这个软件中,我从 mySQL 数据库(通常在数组中)加载不同类型的 RAW 和分析数据。

我想在 Widget 中集成 Iython 控制台,以便我可以轻松地与这些数据进行交互。

我在使用 Ipython 0.13 时遇到了一些困难。
这是我已经拥有的(整个代码很长,所以我只展示了包含小部件的部分,Ipython 控制台和相应的导入行,如果您需要更多,请告诉我):

##I load everything useful to my application, including the following line
from IPython.frontend.qt.console.qtconsoleapp import IPythonQtConsoleApp

##then is my whole software
##here is a class containing the Graphical User Interface elements. A button call the following function. self.Shell_Widget is the widget containing the Ipython console, self.MainWindow is the application mainwindow
def EmbeddedIpython(self):
    """
    This function should launch an Ipython console
    """


    self.Shell_Widget = QtGui.QDockWidget(self.MainWindow) #Widget creation
    self.MainWindow.addDockWidget(4,self.Shell_Widget)
    self.Shell_Widget.setMinimumSize(400,420)

    console = IPythonQtConsoleApp() #Console Creation
    console.initialize()
    console.start()


    self.Shell_Widget.show()

因此,根据需要,启动了一个 Ipython 控制台,并且似乎可以工作,但我无法访问整个应用程序变量、数组等......我认为 Ipython 控制台是独立于我的软件启动的,但这是我的编程限制...
有人知道如何在我的应用程序中启动 Ipython 吗?也许缺少参数,或者集成 Ipython 的不同方式。

有关信息,这不起作用:
Embedding IPython Qt console in a PyQt application

感谢您的帮助!!

最佳答案

mentioned link似乎在这里工作完美无缺:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import atexit

from IPython.zmq.ipkernel import IPKernelApp
from IPython.lib.kernel import find_connection_file
from IPython.frontend.qt.kernelmanager import QtKernelManager
from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget
from IPython.utils.traitlets import TraitError

from PyQt4 import QtGui, QtCore

def event_loop(kernel):
    kernel.timer = QtCore.QTimer()
    kernel.timer.timeout.connect(kernel.do_one_iteration)
    kernel.timer.start(1000*kernel._poll_interval)

def default_kernel_app():
    app = IPKernelApp.instance()
    app.initialize(['python', '--pylab=qt'])
    app.kernel.eventloop = event_loop
    return app

def default_manager(kernel):
    connection_file = find_connection_file(kernel.connection_file)
    manager = QtKernelManager(connection_file=connection_file)
    manager.load_connection_file()
    manager.start_channels()
    atexit.register(manager.cleanup_connection_file)
    return manager

def console_widget(manager):
    try: # Ipython v0.13
        widget = RichIPythonWidget(gui_completion='droplist')
    except TraitError:  # IPython v0.12
        widget = RichIPythonWidget(gui_completion=True)
    widget.kernel_manager = manager
    return widget

def terminal_widget(**kwargs):
    kernel_app = default_kernel_app()
    manager = default_manager(kernel_app)
    widget = console_widget(manager)

    #update namespace                                                           
    kernel_app.shell.user_ns.update(kwargs)

    kernel_app.start()
    return widget

class mainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(mainWindow, self).__init__(parent)

        self.console = terminal_widget(testing=123)

        print "\nAn Embeded Ipython Console!"

        self.textEdit = QtGui.QTextEdit()

        self.dockShell = QtGui.QDockWidget(self)
        self.dockShell.setWidget(self.textEdit)

        self.addDockWidget(4, self.dockShell)
        self.setCentralWidget(self.console)

if __name__ == "__main__":
    import  sys

    app  = QtGui.QApplication(sys.argv)
    main = mainWindow()
    main.show()
    sys.exit(app.exec_())

关于python-2.7 - 如何在 PyQT 应用程序中集成 Ipython 控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13236422/

相关文章:

qt - 创建资源文件以在Qt Designer中使用它

Python Django : Time Zone Conversion

python - 无法在布局中更改大小

python - 检查分配给 Python 2 中变量的文件大小

python - 如何从 QWidget 返回值

python - Ipython %matplotlib 给出 "ImportError: No module named moves"尽管通过 pip 安装了移动

python - 从 Python 模块导入的装饰器不起作用

python - 错误 : video system not initialized (kivy, ipython)

python - 如何在Python中读取.000文件类型以进行机器学习?

python - 修改 OPTPARSE 、 python 中的默认帮助消息 (-h)