First of all, that’s gorgeous … and it’s lovely to see stuff like that getting built with napari/magicgui.
at the end of the day, just go with whatever works for you! but here’s a couple thoughts/ tips:
setting sizes with
setMaximumWidth/Height can be great for a given window size, but sometimes doesn’t “adapt” well if the user resizes the window. size policies on the other hand, are more prescriptive, and suggest how a given widget should (or shouldn’t) take advantage of available space as it grows/shrinks (see the available SizePolicy options here). Usually you’ll need a combination of fixed widget sizes and size policies to get the desired outcome depending on the content/intent of the widgets and the viewer window size.
When I use these instructions, I get the layout that I want:
What you’re more or less saying with this code is “I want the commands and colorbars docks to be a fixed width… based on the initial size of the commands” and by omission that leaves the
tunning_dock to fill in the remainder of the space.
Rather than getting and pinning the
commands_dock width, just give the original widget a horizontal SizePolicy of “fixed” (meaning it should use the
sizeHint() as the required size, and should never grow or shrink), and the containing dock will “obey”:
from qtpy.QtWidgets import QSizePolicy
# horizontal size is fixed
# vertical sizeHint should be considered "minimum", but allow widget to grow...
# note, we're adjusting the original widget, not the dock widget
looks like the colorbar widget is a good candidate for a fixed with. doesn’t really matter how wide the napari viewer is, a few hundred pixels might be sufficient (leaving the remaining space to the
that leaves the
tunning_dock with the default sizePolicy of “preferred” meaning "The
sizeHint() is best, but the widget can be shrunk and still be useful. The widget can be expanded, but there is no advantage to it being larger than
from qtpy.QtWidgets import QLabel, QSizePolicy
from magicgui import magicgui
commands = commands.Gui()
colorbars = QLabel("Colorbars")
tunning = QLabel("Tunning")
viewer = napari.Viewer()
commands_dock = viewer.window.add_dock_widget(commands, area='bottom')
colorbars_dock = viewer.window.add_dock_widget(colorbars, area='bottom')
tunning_dock = viewer.window.add_dock_widget(tunning, area='bottom')
or with a larger window size (only tunning expands):
you can also then use
viewer.window._qt_window.resize(width, height) (in addition to
showMaximized as you already did) to set the full viewer size.
(also, note that I was mostly adjusting the originally widgets before creating dock widgets there).
edit: if you wanted to do some more complicated/adaptive space sharing between colorbars and tunning widgets, then you could use the
resizeDocks command as above as an initial command, and (I believe) it should maintain the ratio of widths as you resize the window.