0

I have searched and searched for good information on this but I have been unable to find a solution.

I am working on a Qt application which has an embedded python interpreter - all working nicely! The user may drive the application via python code processed by the embedded interpreter.

My problem is that the "console" is little more than a line edit widget which essentially allows the user input text to the embedded interpreter.

What I really need is python console widget driving my embedded python interpreter, with tab complete. Tab complete is virtually essential. Text highlighting would be a bonus. If I could even integrate a normal python console I could start ipython using the "from IPython import embed; embed()" trick.

There may be a hundred ways to do this, and it may be obvious to some, but it honestly has me beat! Any assistance would be greatly appreciated.

Thanks :)

3
  • 1
    You can do this with Jupyter's (formerly IPython's) qtconsole widget. Look at Spyder for an example of an application that does it. Commented Oct 14, 2016 at 11:55
  • Hi Thomas. Thanks for the suggestion. I had found this and worked part way through an example. However, I believe this requires PySide/PyQt. PyQt isn't an option for us (unless needs absolutely must) and I'm not sure of the state PySide is in theses days. Commented Oct 14, 2016 at 12:31
  • Short of reimplementing the qtconsole in C++, that's the only way I know of to do it. It should work with PySide, if you want to try that. Commented Oct 16, 2016 at 8:55

1 Answer 1

1

I had the same problem. There was a compability problem. Firstly I downloaded Pycharm (a editor) and I used this code like a widget. It works.

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Alvaro, sorry I haven't checked here in a while. I'm sorry, I'm not sure how you have used this. Do you set a pyQt app running and launch this widget? I really appreciate the assistance, but a little more info would be great.
Simply create the widget in python and add it to a layout. I have used in inside a C++ Qt application as well, where I have simply executed the python source to initialize and add the widget to a layout of the C++ application...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.