How to adjust the size of a GtkGrid cell?
Asked Answered
O

1

7

I'm programming a little application in Python + Gtk3. I'm using a GtkGrid with one column and two rows.

In the first row I put: a GtkScrolledWindow, and inside it a TreeView with two columns.

In the second row I put: a ButtonBox, and inside it a GtkButton, a ComboBox and another GtkButton.

Question is: the second row (the bottom one with the buttons) has too much space above and below the buttons, so how can I do to set the height of that cell to match the height of the buttons, without "wasting" space above and below?

The code is here and this is a screen-shot of my program:

enter image description here


Update Note : This code added to question from Wayback Machine

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#-----------------------------------------------------------------------#
# untitled.py                                                           #
#                                                                       #
# Copyright (C) 2013 Germán A. Racca - <gracca[AT]gmail[DOT]com>        #
#                                                                       #
# This program is free software: you can redistribute it and/or modify  #
# it under the terms of the GNU General Public License as published by  #
# the Free Software Foundation, either version 3 of the License, or     #
# (at your option) any later version.                                   #
#                                                                       #
# This program is distributed in the hope that it will be useful,       #
# but WITHOUT ANY WARRANTY; without even the implied warranty of        #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          #
# GNU General Public License for more details.                          #
#                                                                       #
# You should have received a copy of the GNU General Public License     #
# along with this program. If not, see <http://www.gnu.org/licenses/>.  #
#-----------------------------------------------------------------------#


import os
from gi.repository import Gtk, GdkPixbuf

class SetWallGnome3(Gtk.Window):

    def __init__(self):
        """Initialize the window"""
        Gtk.Window.__init__(self, title='Set Wallpaper in Gnome 3')
        self.set_default_size(400, 500)
        self.set_position(Gtk.WindowPosition.CENTER)
        self.set_border_width(10)

        # definimos los directorios
        home_path = os.environ.get('HOME')
        pics_dir = os.path.join('Pictures', 'walls')
        pics_path = os.path.join(home_path, pics_dir)

        # creamos las siguientes listas:
        # pics_list -> camino completo de las imágenes
        # pics_name -> nombre de las imágenes
        pics_list = []
        pics_name = []
        for root, dirs, files in os.walk(pics_path):
            for fn in files:
                pics_name.append(fn)
                f = os.path.join(root, fn)
                pics_list.append(f)

        # creamos una grilla
        self.grid = Gtk.Grid(column_homogeneous=True, row_homogeneous=True,
                             column_spacing=10, row_spacing=10)
        self.add(self.grid)

        # creamos una ventana con barras de scroll
        self.scrolledwindow = Gtk.ScrolledWindow()
        self.scrolledwindow.set_policy(Gtk.PolicyType.NEVER,
                                       Gtk.PolicyType.AUTOMATIC)

        # creamos la ListStore
        self.liststore = Gtk.ListStore(GdkPixbuf.Pixbuf, str)
        for name, pic in zip(pics_name, pics_list):
            pxbf = GdkPixbuf.Pixbuf.new_from_file_at_scale(pic, 100, 100, True)
            self.liststore.append([pxbf, name])

        # creamos el TreeView
        self.treeview = Gtk.TreeView(model=self.liststore)

        # creamos las columnas del TreeView
        renderer_pixbuf = Gtk.CellRendererPixbuf()
        column_pixbuf = Gtk.TreeViewColumn('Picture', renderer_pixbuf, pixbuf=0)
        column_pixbuf.set_alignment(0.5)
        self.treeview.append_column(column_pixbuf)

        renderer_text = Gtk.CellRendererText(weight=600)
        renderer_text.set_fixed_size(200, -1)
        column_text = Gtk.TreeViewColumn('Name', renderer_text, text=1)
        column_text.set_sort_column_id(1)
        column_text.set_alignment(0.5)
        self.treeview.append_column(column_text)

        self.scrolledwindow.add_with_viewport(self.treeview)
        self.grid.attach(self.scrolledwindow, 0, 0, 1, 1)

        # creamos el ComboBox para las opciones del wallpaper
        self.option_store = Gtk.ListStore(str, str)
        opts_list = ['centered', 'scaled', 'spanned', 'stretched', 'wallpaper',
                     'zoom']
        opts_name = ['Centered', 'Scaled', 'Spanned', 'Stretched', 'Wallpaper',
                     'Zoom']
        for opts, names in zip(opts_list, opts_name):
            self.option_store.append([opts, names])
        
        self.option_combo = Gtk.ComboBox.new_with_model(self.option_store)
        self.option_combo.connect('changed', self.on_option_combo_changed)
        renderer_combo_text = Gtk.CellRendererText()
        self.option_combo.pack_start(renderer_combo_text, True)
        self.option_combo.add_attribute(renderer_combo_text, 'text', 1)
        self.option_combo.set_active(0)

        # creamos los botones
        self.buttonbox = Gtk.ButtonBox(Gtk.Orientation.HORIZONTAL)
        self.buttonbox.set_layout(Gtk.ButtonBoxStyle.EDGE)

        self.button_about = Gtk.Button(stock=Gtk.STOCK_ABOUT)
        self.buttonbox.add(self.button_about)

        self.buttonbox.add(self.option_combo)

        self.button_apply = Gtk.Button(stock=Gtk.STOCK_APPLY)
        self.buttonbox.add(self.button_apply)

        self.grid.attach_next_to(self.buttonbox, self.scrolledwindow,
                                 Gtk.PositionType.BOTTOM, 1, 1)

    # callback para el ComboBox
    def on_option_combo_changed(self, combo):
        treeiter = combo.get_active_iter()
        if treeiter != None:
            model = combo.get_model()
            option, name = model[treeiter][:]
            print "Selected: option=%s, name=%s" % (option, name)

def main():
    """Show the window"""
    win = SetWallGnome3()
    win.connect('delete-event', Gtk.main_quit)
    win.show_all()
    Gtk.main()
    return 0

if __name__ == '__main__':
    main()
Octodecillion answered 27/3, 2013 at 15:6 Comment(1)
Next time consider putting your code in the page so we can see it. That link 404'sAcuity
F
17

you're creating the GtkGrid with homogeneous spacing for columns and rows; this means that your columns and rows will receive the same size, which is the maximum natural size between all the columns and rows.

when creating the Grid, remove the row_homogeneous=True attribute. if you want the TreeView to expand, you should set the hexpand and vexpand properties to True on it, i.e.:

    # creamos una grilla
    self.grid = Gtk.Grid(column_homogeneous=True,
                         column_spacing=10,
                         row_spacing=10)

    [...]

    # creamos el TreeView
    self.treeview = Gtk.TreeView(model=self.liststore)
    # set the TreeView to expand both horizontally and vertically
    self.treeview.set_hexpand(True)
    self.treeview.set_vexpand(True)

GtkGrid is a container and layout manager that respects the horizontal and vertical expansion and alignment flags on the GtkWidget, instead of having packing properties, like GtkBox or GtkTable.

Frances answered 27/3, 2013 at 19:52 Comment(1)
Wow... it worked like a charm! Many thanks @Frances for reading my code and for giving such an accurate answer.Octodecillion

© 2022 - 2024 — McMap. All rights reserved.