silviubogan
silviubogan

Reputation: 3461

How to put borders around the cells of a Gtk.Grid, just like in an HTML table?

I try to make an HTML-like table based on Gtk.Grid (with borders between cells and around the table). Since the table contains only labels I began trying to style the labels. I tried this:

label {
    border: 1px solid black;
}

but it didn't work, although in the same CSS file I was able to set the background color of the window to lightblue.

So I simplified my problem to the following two files.

test.py:

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.add(Gtk.Label("These words should be red."))

provider = Gtk.CssProvider()
provider.load_from_path("style.css")
win.get_style_context().add_provider(provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)

#win.add(Gtk.Label("These words should pe red.")) # the same behavior: the label's text is not painted red

win.show_all()
Gtk.main()

style.css:

window {
    background-color: lightblue; /* this works */ }

label {
    color: red; /* this does not work */ }

In MSYS2 MinGW 32-bit I tried both this:

hope@hope-PC MINGW32 ~/python+gtk/test
$ winpty python2 test.py

and this

hope@hope-PC MINGW32 ~/python+gtk/test
$ python2 test.py

But I do not know how I can style the label because the code above does not work how I want, as I specified in the comments. There is no error or warning printed in the terminal.

Screenshot

I have this version of GTK+ & Python 2.7 installed:

hope@hope-PC MINGW32 ~/python+gtk/test
$ pacman -Q mingw-w64-i686-gtk3
mingw-w64-i686-gtk3 3.22.16-1
hope@hope-PC MINGW32 ~/python+gtk/test
$ python2 --version
Python 2.7.13

I use the latest Windows 10 Creators upgrade as of 5th of August, 2017, with all the updates installed. I use MSYS2 with all the upgrades and updates done with the pacman -Syyu instruction in the terminal.

Here are some relevant links (3 links to official documentation and 1 link to another SO question):

  1. Gtk.Label from lazka.github.io
  2. Gtk.Label description from gtk.org
  3. Theming in GTK+ from developer.gnome.org (archive from Wayback Machine (archive.org))
  4. This is the question that thaught me the instructions with which I can load a CSS file in a GTK+ application: CSS styling in GTKSharp

In these conditions, how can I style a label? Or, at least, please guide me into making borders around the cells of a Gtk.Grid through other means.

Upvotes: 3

Views: 3676

Answers (1)

silviubogan
silviubogan

Reputation: 3461

I read this SO question and its only answer: Which GTK+ elements support which CSS properties? as recommended by @MichaelKanis in a comment to my question. For now, I use Frames to contain my Labels. The Frames in the Adwaita theme on Windows 10 have by default a small border, so the Grid looks almost like an HTML table.

The relevant part of my code is this:

def set_value_at(self, row, col, val):
    l = Gtk.Label(None)
    l.set_markup(val)

    f = Gtk.Frame()
    f.add(l)

    self.attach(f, col, row, 1, 1)
    return f, l

Upvotes: 1

Related Questions