How to Get/Set the display area window size with python code?

Slicer 4.11


By default,the window size is about 672x994 resolution which is marked in red.
I can manually expand or shrink the window like this:

I wonder if there is some way to get/set the window size?
Thank you in advance for your advice and help!

I think you have to resize the module panel on the left:

panelDockWidget = slicer.util.mainWindow().findChildren('QDockWidget','PanelDockWidget')[0]
slicer.util.mainWindow().resizeDocks([panelDockWidget],[1000], qt.Qt.Horizontal) # change 1000 to desired width
1 Like

Thanks a lot ! Ebrahim
It works! I noticed that you are actually changing the panel on the left in order to resize the display area so it changes the width.However do you think it possible to change the height? Maybe I need to change the Python Interactor window height in order to change the display area height?

1 Like

However do you think it possible to change the height? Maybe I need to change the Python Interactor window height in order to change the display area height?

You can change the python interactor height in a similar way

pyConsoleDockWidget = slicer.util.mainWindow().findChildren('QDockWidget','PythonConsoleDockWidget')[0]
slicer.util.mainWindow().resizeDocks([pyConsoleDockWidget],[100], qt.Qt.Vertical) # change 100 to desired height

The display area whose size you are trying to manipulate can be accessed as slicer.util.mainWindow().centralWidget(). Depending on what you’re trying to do exactly, you might want to play with its size policy and resize it. Or maybe insert some spacers around it.

Thanks Ebrahim,
Actually,I just want to fix the display area window size.Maybe I accidentally dragged the window and made the size different.

It seems that I can just directly get the QtWidget and play with its size?
However I can not resize the window with the code below:

centralWidget = slicer.util.mainWindow().centralWidget()
slicer.util.mainWindow().resizeDocks([centralWidget],[100], qt.Qt.Horizontal)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
In  [101]:
Line 2:     

ValueError: Called resizeDocks(QList<QDockWidget*> docks, QList<qint32> sizes, Qt::Orientation orientation) -> void with wrong arguments: ([QWidget(0x1e4f319c700, name="CentralWidget") ], [100], 1)
---------------------------------------------------------------------------

Is there something wrong with my code?
Thank you for your continued attention.

It’s not that simple I think. The central widget size is determined according to its size policy, which you can check with slicer.util.mainWindow().centralWidget().sizePolicy. So it might be expanding to fill the space given to it within its layout, and in that case you’d have to control its size indirectly as I’ve described. You can also try change the size policy, or change the min/max sizes to get different effects. I’m not sure what effect you’re trying to get exactly.

resizeDocks was used in the case of resizing the python console and module panel dock widgets-- that’s because those are QDockWidgets, which are special. The central widget is not a QDockWidget so it is treated differently.

1 Like