Search code examples
popupipythonspyder

Automatically view IPython console in Spyder


I am using Python 3.8 in Spyder. The graphics backend of the IPython console is set to 'automatic'. When I call the IPython Console with a Python algorithm, the window is minimized, so I have to click on it at the taskbar to see it. However, this only happens the first time when I run the algorithm. If I run the algorithm once, click on the window from the taskbar, and then close it, then the next time that I run the algorithm the IPython console is immediately visible. It continues to work fine, but when I restart the kernel I get the same problem again. Here is a MWE:

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

How can I make sure that the IPython console always is maximized automatically, even when I run the algorithm for the first time? I am using Python 3.8 in Spyder.


Solution

  • It can be done using 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 )
    

    Note that this only works if no other windows called 'Figure 1' are opened.