1

我正在使用 Python 创建一个控制台驱动的 Qt 应用程序。我不想实现我自己的自定义控制台,而是想嵌入 IPython Qt 控制台,同时让它响应我的应用程序。例如,我希望某些关键字输入到控制台以触发我的主应用程序中的操作。所以我在控制台中输入“dothis”,然后在我的应用程序的另一个窗口中显示一个绘图。

我已经看到了一些这样的问题:这个问题讨论了如何将 IPython Qt 小部件嵌入到您的应用程序中并传递函数,尽管看起来这些函数在 IPython 内核中执行,而不是在我的主应用程序的内核中执行。还有这个家伙,但我无法执行示例中的代码(它已经两年了),而且它看起来也不像我想要的那样。

有没有一种方法可以传入将在我的主内核中执行的函数或方法,或者至少通过与 IPython 内核通信以某种方式模拟这种行为?有没有人这样做过?

4

1 回答 1

3

这就是我想出的,到目前为止它工作得很好。我继承了 RichIPythonWidget 类并重载了该_execute方法。每当用户在控制台中输入内容时,我都会根据已注册的命令列表对其进行检查;如果它匹配一个命令,那么我执行命令代码,否则我只是将输入传递给默认_execute方法。

控制台代码:

from IPython.qt.console.rich_ipython_widget import RichIPythonWidget

class CommandConsole( RichIPythonWidget ):
    """
    This is a thin wrapper around IPython's RichIPythonWidget. It's
    main purpose is to register console commands and intercept
    them when typed into the console.

    """
    def __init__(self, *args, **kw ):
         kw['kind'] = 'cc'
         super(CommandConsole, self).__init__(*args, **kw)
         self.commands = {}


    def _execute(self, source, hidden):
        """
        Overloaded version of the _execute first checks the console
        input against registered commands. If it finds a command it
        executes it, otherwise it passes the input to the back kernel
        for processing.

        """
        try:
            possible_cmd = source.split()[0].strip()
        except:
            return super(CommandConsole, self)._execute("pass", hidden)

        if possible_cmd in self.commands.keys():
            # Commands return code that is passed to the console for execution.
            s = self.commands[possible_cmd].execute()
            return super(CommandConsole, self)._execute( s, hidden )
        else:
            # Default back to the original _execute
            return super(CommandConsole, self)._execute(source, hidden)


    def register_command( self, name, command ):
        """
        This method links the name of a command (name) to the function
        that should be called when it is typed into the console (command).

        """
        self.commands[name] = command

示例命令:

from PyQt5.QtCore import pyqtSignal, QObject, QFile

class SelectCommand( QObject ):
    """
    The Select command class.

    """
    # This signal is emitted whenever the command is executed.
    executed = pyqtSignal( str, dict, name = "selectExecuted" )

    # This is the command as typed into the console.
    name = "select"

    def execute(self):
        """
        This method is executed whenever the ``name`` command is issued
        in the console.

        """
        name = "data description"
        data = { "data dict" : 0 }
        # The signal is sent to my Qt Models
        self.executed.emit( name, data )
        # This code is executed in the console.
        return 'print("the select command has been executed")'
于 2015-05-13T16:24:16.593 回答