Reputation: 452
I am new to gtk programming (but experienced with python). I have a window that has a button widget child. What do I have to do if I want the button to completely fill the window?
By the way, this would be extremely easy to do with xml (fill-parent), is there anything similar to xml for gtk that is native?
Thanks in advance!
Upvotes: 5
Views: 6646
Reputation: 11588
Without knowing what your program currently is, I can only give a general answer. There are two ways to interpret your first question; I'll answer both.
There are two types of containers in GTK+. The first, the bin, only has one child, and that child is automatically given the entire size of the parent to fill. GtkWindow, GtkScrolledWindow, GtkFrame, etc. are bins. If you add a widget directly to a GtkWindow, like in Mohsen's answer, it should fill the container.
The second type is the layout container. These are things like GtkBox and GtkGrid that let you pack multiple widgets into a neat layout. For these, GTK+ provides four widget properties on each individual widget to set: hexpand
, halign
, vexpand
, and valign
. The h
and v
determines which dimension the property applies to (horizontal or vertical). If expand
is TRUE
, the widget is given whatever space is left after non-expanding widgets are positioned using their preferred size. If align
is GTK_ALIGN_FILL
, the widget fills the given space. This page should explain better.
Note that with GTK+, a widget is given its size by its parent, not the other way around. A widget can request a given size to have, but the parent is free to ignore this. (GtkWindow is an example of one that doesn't ignre the size request; generally it will automatically size itself to make enough of its content visible unless explicitly overridden).
There IS an XML-based format for building GTK+ UIs. It's provided by the GtkBuilder type, and Glade produces these files so you don't have to write the XML format yourself. If you do want to write your UI file manually, each widget's documentation will have a section "GtkXxx as GtkBuildable" that shows how to write the XML.
Upvotes: 13
Reputation: 6637
By default any widget that added to many type of containers, fill all the parent:
from gi.repository import Gtk
win = Gtk.Window()
btn = Gtk.Button("Ok")
win.add(btn)
win.show_all()
win.connect('destroy', Gtk.main_quit)
Gtk.main()
Upvotes: 0
Reputation: 8815
By default, a widget will only get the same amount of space it requested, and not any less than its minimum size.
If you want a widget to fill the available space of its parent, you need to mark it as a expandable, using Gtk.Widget.set_hexpand()
and Gtk.Widget.set_vexpand()
.
Upvotes: 7