在 Spyder 中自动查看 IPython 控制台

Automatically view IPython console in Spyder

我在 Spyder 中使用 Python 3.8。 IPython 控制台的图形后端设置为 'automatic'。当我用 Python 算法调用 IPython 控制台时,window 被最小化,所以我必须在任务栏上单击它才能看到它。但是,这仅在我 运行 算法时第一次发生。如果我 运行 算法一次,单击任务栏中的 window,然后关闭它,那么下一次我 运行 算法 IPython 控制台会立即可见的。它继续正常工作,但是当我重新启动内核时,我再次遇到同样的问题。这是一个 MWE:

import matplotlib.pyplot as plt
fig = plt.figure()

如何确保 IPython 控制台始终自动最大化,即使是我第一次 运行 该算法时也是如此?我在 Spyder 中使用 Python 3.8。

可以使用 win32gui 来完成:

import win32gui, win32com.client
import matplotlib.pyplot as plt
fig = plt.figure()

def Set_to_foreground( hwnd, ctx ):
    if win32gui.IsWindowVisible( hwnd ):
    #The IPython Console will usually have the title 'Figure 1'
        if win32gui.GetWindowText(hwnd)=='Figure 1':
            IPython_console=hwnd
            shell = win32com.client.Dispatch("WScript.Shell")
            shell.SendKeys('%')
            win32gui.SetForegroundWindow(IPython_console)

win32gui.EnumWindows( Set_to_foreground, None )

请注意,这仅在没有打开其他名为 'Figure 1' 的 windows 时有效。