Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
combobox
#4
This is a new question. You should start a new thread.

What are you trying to do with qtconsole? There is no qtconsole.qt. I would be very surprised if there is a QtCore defined anywhere in qtconsole. QtCore is in PyQt5 along with QtWidget and QtGUI.

I used qtconsole in an application. I created a console window that I could open up to run interactive python.
from qtconsole.inprocess import QtInProcessKernelManager
from qtconsole.rich_jupyter_widget import RichJupyterWidget
from qtpy import QtWidgets
import asyncio


class Console(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        # start an ipython kernel in this process and let it run with the Qt event loop
        self.kernel_manager = QtInProcessKernelManager()
        self.kernel_manager.start_kernel()
        self.kernel = self.kernel_manager.kernel
        self.kernel.shell.register_magic_function(self._back, "line", "BACK")
        self.kernel.shell.register_magic_function(self._where, "line", "WHERE")
        self.kernel.gui = "qt4"  # use qt4, but works with PySide6

        # there are issues if you enter quit() or exit() in the console
        # override these functions
        # self.kernel.shell.push({"quit": self._console_quit, "exit": self._console_quit})
        self.contexts = []

        self.kernel_client = self.kernel_manager.client()
        self.kernel_client.start_channels()

        self.control = RichJupyterWidget()
        self.control.kernel_manager = self.kernel_manager
        self.control.kernel_client = self.kernel_client

        box = QtWidgets.QVBoxLayout(self)
        box.addWidget(self.control)
        self.setWindowTitle("Console")

    def _with(self, object):
        """Set object as namespace"""
        # TBD Make this a magic function.  Need to figure out how
        # to evaluate "line" argument passed as argument when called.
        self.contexts.insert(0, object)
        return self._update_context()

    def _back(self, _):
        """Restore previous namespace"""
        if len(self.contexts) > 1:
            self.contexts.pop(0)
            return self._update_context()
        return None

    def _where(self, _):
        """Display context history from current to starting"""
        return self.contexts

    def _update_context(self):
        """Update shell to use current context"""
        if len(self.contexts) == 0:
            return None
        obj = self.contexts[0]
        if isinstance(obj, dict):
            context = obj.copy()
        else:
            context = obj.__dict__.copy()
        # TBD make WITH a magic function
        context.update({"self": obj, "WITH": self._with})
        self.kernel.shell.reset()
        self.kernel.shell.push(context)
        return obj

    def setcontext(self, context):
        """Set base context"""
        self.contexts = [context]
        self._update_context()

    def showEvent(self, event):
        """Execute when window is shown"""
        self._update_context()
        event.accept()

    def shutdown(self):
        """Shutdown the console and stop kernels."""
        self.kernel_client.stop_channels()
        self.kernel_manager.shutdown_kernel()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    console = Console()
    console.setWindowTitle("In process IPython Kernel with qconsole")
    console.show()
    sys.exit(app.exec_())
Reply


Messages In This Thread
combobox - by paulie70 - May-13-2023, 06:44 PM
RE: combobox - by deanhystad - May-13-2023, 07:39 PM
RE: combobox - by paulie70 - May-19-2023, 05:59 PM
RE: combobox - by deanhystad - May-19-2023, 06:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] How can I sync Combobox index to other combobox index? nickzsche 2 2,417 Jan-03-2022, 12:29 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020