Here is a code snippet that demonstrates the use of GtkGridView from Gtk4:

import gi
gi.require_version('Gtk', '4.0')
from gi.repository import Gtk

class MyWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="GtkGridView Example")

        # Create a list store
        liststore = Gtk.ListStore(str, str)
        liststore.append(["John", "Doe"])
        liststore.append(["Jane", "Doe"])
        liststore.append(["Bob", "Smith"])
        liststore.append(["Alice", "Smith"])

        # Create a grid view
        gridview = Gtk.GridView()
        gridview.set_model(liststore)
        gridview.set_factory(Gtk.SignalListItemFactory())

        # Create a column for the first name
        first_name_column = Gtk.GridViewColumn()
        first_name_column.set_title("First Name")
        first_name_column.set_property("expand", True)
        first_name_column.set_property("resizable", True)
        first_name_column.set_property("min-width", 100)
        first_name_column.set_property("max-width", 200)
        first_name_column.set_property("cell-align", 0.5)
        first_name_column.set_property("cell-spacing", 6)
        first_name_column.set_property("cell-background", "white")
        first_name_column.set_property("cell-text", "{{model[0]}}")
        gridview.append_column(first_name_column)

        # Create a column for the last name
        last_name_column = Gtk.GridViewColumn()
        last_name_column.set_title("Last Name")
        last_name_column.set_property("expand", True)
        last_name_column.set_property("resizable", True)
        last_name_column.set_property("min-width", 100)
        last_name_column.set_property("max-width", 200)
        last_name_column.set_property("cell-align", 0.5)
        last_name_column.set_property("cell-spacing", 6)
        last_name_column.set_property("cell-background", "white")
        last_name_column.set_property("cell-text", "{{model[1]}}")
        gridview.append_column(last_name_column)

        # Add the grid view to the window
        self.add(gridview)

win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

This code creates a window with a grid view that displays a list of names. The grid view is populated with data from a list store. The grid view has two columns, one for the first name and one for the last name. Each column is created using a Gtk.GridViewColumn object. The cell-text property of each column is set to a string that specifies how to display the data in the column. The cell-background property of each column is set to a string that specifies the background color of the cells in the column.

I hope this helps! Let me know if you have any other questions.

Source: Conversation with Bing, 16/03/2023(1) GitHub - Taiko2k/GTK4PythonTutorial: GTK4 + Python tutorial with code .... https://github.com/Taiko2k/GTK4PythonTutorial Accessed 16/03/2023.
(2) Gtk.GridView. https://docs.gtk.org/gtk4/class.GridView.html Accessed 16/03/2023.
(3) GitHub - ToshioCP/Gtk4-tutorial: GTK 4 tutorial for beginners. https://github.com/ToshioCP/Gtk4-tutorial Accessed 16/03/2023.

Edit
Pub: 16 Mar 2023 07:12 UTC
Views: 192