2012-06-29 89 views
2

我通過(簡體)運行嵌入式IPython的控制檯更換InteractiveShellEmbed:使用Qt控制檯

# 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代替嵌入式終端的殼呢?有一些使用ipython qtconsole的例子,但不是如何將它集成到我自己的代碼中。

+0

我願做同樣的 - 好的問題 –

+0

Qt的控制檯運行正常在兩個進程中 - 前端和內核(實際運行代碼)。將內核嵌入到您自己的應用程序中的示例如下:https://github.com/ipython/ipython/blob/master/docs/examples/lib/ipkernel_qtapp.py –

+0

@TomasK:謝謝!你能把它當作一個可以接受的東西嗎?它看起來比MaciekD發佈的要容易得多。是的,我知道它在2個進程中正常運行,但在主進程中我有其他基於PyQt4的GUI,所以我需要在其中運行控制檯。 – eudoxos

回答

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_()