Below I have an example program with a column of three buttons on the left and a picture widget of aspect ratio 2:1 on the right.
I can freely resize this window. This results in either empty space to the right or at the bottom.
How can I constrain the window resizing such that it snugly fits the content?
#!/usr/bin/python3
import sys
import gi
gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
gi.require_version("GdkPixbuf", "2.0")
from gi.repository import Gtk, Adw, GdkPixbuf, Gdk # noqa: E402
class MainWindow(Gtk.ApplicationWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
for label in ["Open", "Reset", "Run"]:
self.vbox.append(Gtk.Button(label=label))
img = sum([[x, y, x + y] for x in range(64) for y in range(32)], [])
pixbuf = GdkPixbuf.Pixbuf.new_from_data(
img,
colorspace=GdkPixbuf.Colorspace.RGB,
has_alpha=False,
bits_per_sample=8,
width=64,
height=32,
rowstride=64 * 3,
)
texture = Gdk.Texture.new_for_pixbuf(pixbuf)
self.screen = Gtk.Picture.new_for_paintable(texture)
self.screen.set_valign(Gtk.Align.START)
self.hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
self.hbox.append(self.vbox)
self.hbox.append(self.screen)
self.set_child(self.hbox)
class MyApp(Adw.Application):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.connect("activate", self.on_activate)
def on_activate(self, app):
self.win = MainWindow(application=app)
self.win.present()
app = MyApp(application_id="test.local.test1")
app.run(sys.argv)
Too wide:
Just right: