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

**URL:** <https://discourse.slicer.org/t/how-to-get-set-the-display-area-window-size-with-python-code/22275>\
**Category:** Support\
**Tags:** python\
**Created:** [March 3, 2022, 6:38am UTC](https://discourse.slicer.org/t/how-to-get-set-the-display-area-window-size-with-python-code/22275 "2022-03-03T06:38:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![user4](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/user4/32/13172_2.png) [@user4](https://discourse.slicer.org/u/user4)\
**Post date:** [March 3, 2022, 6:38am UTC](https://discourse.slicer.org/t/how-to-get-set-the-display-area-window-size-with-python-code/22275/1 "2022-03-03T06:38:10Z")

</div>

Slicer 4.11

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/4/d46346b1440629d0759c1c615c88aa5bce2a6d67.png)  
By default,the window size is about 672x994 resolution which is marked in red.  
I can manually expand or shrink the window like this:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/0/a0213b82acf8b074118aeb8040732d044cf475de.png)  
I wonder if there is some way to get/set the window size?  
Thank you in advance for your advice and help!

---

<div class="post-metadata">

**Author:** ![ebrahim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ebrahim/32/13403_2.png) [@ebrahim](https://discourse.slicer.org/u/ebrahim)\
**Post date:** [March 3, 2022, 2:00pm UTC](https://discourse.slicer.org/t/how-to-get-set-the-display-area-window-size-with-python-code/22275/2 "2022-03-03T14:00:50Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![user4](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/user4/32/13172_2.png) [@user4](https://discourse.slicer.org/u/user4)\
**Post date:** [March 4, 2022, 2:27am UTC](https://discourse.slicer.org/t/how-to-get-set-the-display-area-window-size-with-python-code/22275/3 "2022-03-04T02:27:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ebrahim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ebrahim/32/13403_2.png) [@ebrahim](https://discourse.slicer.org/u/ebrahim)\
**Post date:** [March 4, 2022, 1:39pm UTC](https://discourse.slicer.org/t/how-to-get-set-the-display-area-window-size-with-python-code/22275/4 "2022-03-04T13:39:44Z")

</div>

> 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

```py
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.

---

<div class="post-metadata">

**Author:** ![user4](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/user4/32/13172_2.png) [@user4](https://discourse.slicer.org/u/user4)\
**Post date:** [March 5, 2022, 6:16pm UTC](https://discourse.slicer.org/t/how-to-get-set-the-display-area-window-size-with-python-code/22275/5 "2022-03-05T18:16:18Z")

</div>

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

> [@ebrahim](#):
>
> The display area whose size you are trying to manipulate can be accessed as

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:

```auto
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)
---------------------------------------------------------------------------

```

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/d/6d199bdfb8b7f3efc2c2f5c5729b1500fde09977.png)

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

---

<div class="post-metadata">

**Author:** ![ebrahim](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ebrahim/32/13403_2.png) [@ebrahim](https://discourse.slicer.org/u/ebrahim)\
**Post date:** [March 7, 2022, 1:56pm UTC](https://discourse.slicer.org/t/how-to-get-set-the-display-area-window-size-with-python-code/22275/6 "2022-03-07T13:56:29Z")

</div>

> [@user4](#):
>
> It seems that I can just **directly** get the QtWidget and play with its size?

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.

> [@user4](#):
>
> However I can not resize the window with the code below:

`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.
