用Qt控制台替换InteractiveShellEmbed

时间:2012-06-29 15:43:05

标签: ipython

我通过(简化)运行嵌入式ipython控制台:

# the code uses PyQt4, this makes sure it is initialized properly
import IPython.lib.inputhook
qapp=IPython.lib.inputhook.enable_gui(gui='qt4')
# create the embedded terminal
from IPython.frontend.terminal.embed import InteractiveShellEmbed
ipshell=InteractiveShellEmbed()
ipshell()

如果我想运行ipython的Qt console而不是嵌入式终端shell,那么这段代码会是什么样子?有一些使用ipython qtconsole的例子,但不是如何将它集成到我自己的代码中。

1 个答案:

答案 0 :(得分:0)

在此问题中有一个示例脚本:Embedding IPython Qt console in a PyQt application

您可以在此找到它以供参考:

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 PySide import QtGui, QtCore
import atexit

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', '--profile=plask'])
    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

# This simply opens qtconsole widged in a new window. But you can find embed it wherever you want
app = QtGui.QApplication([''])
widget = terminal_widget(testing=123)
widget.setWindowTitle("Your console") 
widget.show()
app.exec_()