Add hover to gtk.widget

Hi

I am trying to add hover effect using css to a Gtk.Box but without any luck.

I am using fedora 36, with xfwm.

$ xfwm4 --version
This is xfwm4 version 4.14.2 (revision bb38fd909) for Xfce 4.14 
Released uynder the terms of GNU General Public License.
Compiled against GTK+-3.24.28, using GTK+-3.24.28.

Are the version that I use support hover effect? or there something I need to add?

Pointer tracking in GTK3 depends on having an input window associated to a widget. GtkBox, like most containers, does not have a window, so you cannot track if the pointer entered the widget or left it.

At most, you can pack the box inside an EventBox, and then use that to deal with crossing events.

I added GtkBox to EventBox but without any luck using only css.

import gi

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

class HoverDemo(Gtk.Window):
    def __init__(self):
        super().__init__(title="Hover Demo")
        self.set_border_width(10)
        self.set_default_size(300, 250)

        screen = Gdk.Screen.get_default()
        provider = Gtk.CssProvider()
        style_context = Gtk.StyleContext()
        style_context.add_provider_for_screen(
            screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
        )
        css = b"""
        .event_box:hover {
            background-color:red;
        }
        """
        provider.load_from_data(css)

        event_box = Gtk.EventBox()
        event_box.get_style_context().add_class('event_box')

        box = Gtk.Box()

        box.pack_start(Gtk.Label("testing"), False, False, 0)

        event_box.add(box)

        self.add(event_box)
        self.show_all()

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

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.