簡體   English   中英

將Python / IPython控制台小部件添加到Qt應用程序

[英]Adding a Python/IPython console widget to Qt application

我已經搜索並搜索了有關此方面的良好信息,但是無法找到解決方案。

我正在開發一個具有嵌入式python解釋器的Qt應用程序-一切正常! 用戶可以通過嵌入式解釋器處理的python代碼來驅動應用程序。

我的問題是“控制台”只不過是一個行編輯小部件,它實際上允許用戶向嵌入式解釋器輸入文本。

我真正需要的是使用Python控制台小部件來驅動我的嵌入式python解釋器 ,並帶有完整的選項卡。 制表符的完成實際上是必不可少的。 突出顯示文字將是一個獎勵。 如果我什至可以集成一個普通的python控制台,也可以使用“ from IPython import embed; embed()”技巧來啟動ipython。

可能有一百種方法可以做到這一點,對於某些人來說這也許很明顯,但是老實說,它使我戰勝了! 任何幫助將不勝感激。

謝謝 :)

我有同樣的問題。 存在兼容性問題。 首先,我下載了Pycharm(編輯器),並將此代碼用作小部件。 有用。

import sip

sip.setapi(u'QDate', 2)
sip.setapi(u'QDateTime', 2)
sip.setapi(u'QString', 2)
sip.setapi(u'QTextStream', 2)
sip.setapi(u'QTime', 2)
sip.setapi(u'QUrl', 2)
sip.setapi(u'QVariant', 2)


from IPython.lib import guisupport

from qtconsole.rich_jupyter_widget import RichJupyterWidget
from qtconsole.inprocess import QtInProcessKernelManager

class IPythonWidget(RichJupyterWidget):
    def __init__(self,customBanner=None,*args,**kwargs):
        if not customBanner is None: self.banner=customBanner
        super(IPythonWidget, self).__init__(*args,**kwargs)
        self.kernel_manager = kernel_manager = QtInProcessKernelManager()
        kernel_manager.start_kernel()
        kernel_manager.kernel.gui = 'qt4'
        self.kernel_client = kernel_client = self._kernel_manager.client()
        kernel_client.start_channels()

        def stop():
            kernel_client.stop_channels()
            kernel_manager.shutdown_kernel()
            guisupport.get_app_qt4().exit()
        self.exit_requested.connect(stop)

    def pushVariables(self,variableDict):
        """ Given a dictionary containing name / value pairs, push those variables to the IPython console widget """
        self.kernel_manager.kernel.shell.push(variableDict)
    def clearTerminal(self):
        """ Clears the terminal """
        self._control.clear()

    def printText(self,text):
        """ Prints some plain text to the console """
        #self._append_plain_text(text)
        self.append_stream(text)
    def executeCommand(self,command):
        """ Execute a command in the frame of the console widget """
      #  self._execute(command,False)
        self.execute(command,False)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM