2

我正在通过(简化)运行嵌入式 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 控制台而不是嵌入式终端 shell,这段代码会是什么样子?到处都有使用的例子ipython qtconsole,但没有如何将它集成到我自己的代码中。

4

1 回答 1

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_()
于 2012-12-21T10:11:15.653 回答