簡體   English   中英

為我的PyQt應用程序采用IPython Qt控制台

[英]Coopt IPython Qt Console for my PyQt Application

我正在使用Python創建一個控制台驅動的Qt應用程序。 我想嵌入IPython Qt控制台,而不是實現我自己的自定義控制台,但也讓它響應我的應用程序。 例如,我希望某些關鍵字輸入到控制台以觸發我的主應用程序中的操作。 所以我在控制台中輸入“dothis”,在我的應用程序的另一個窗口中顯示一個圖。

我在這些方面看到了一些問題: 這個問題討論了如何將IPython Qt小部件嵌入到應用程序中並傳遞函數,盡管看起來這些函數在IPython內核中執行而不是在主應用程序的內核中執行。 還有這個人 ,但是我不能在示例中執行代碼(它已經兩年了),看起來它看起來並不像我想做的那樣。

有沒有辦法可以傳遞將在我的主內核中執行的函數或方法,或者至少通過與IPython內核通信來模擬這種行為? 有沒有人這樣做過?

這就是我想出來的,到目前為止它運作良好。 我繼承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")'

暫無
暫無

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

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