Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a web page when a Gtk.Box is clicked anywhere, in spite of the Gtk.LinkButton links inside it?

Practically, I like the style of Gtk.LinkButton and I want to use it to make an advertisement banner in my program. The label below would be a single-paragraph description of the destination of the link. But anywhere on the banner should a mouse click open the link.

This is what I tried. When I click on the Gtk.LinkButton, both URIs are opened. When I click elsewhere on the window, nothing is done.

example.py:

# coding=utf-8

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

b = Gtk.Builder()
b.add_from_file("test.glade")

w = b.get_object("window1")


def box_clicked(widget, event, user_data=None):
    del widget, event, user_data

    Gtk.show_uri_on_window(w, "http://lumea-lui-silviu.blogspot.ro",
                           Gdk.CURRENT_TIME)


linkButton = b.get_object("linkButton")
box = b.get_object("box1")
box.connect("button-release-event", box_clicked)

w.connect("delete-event", Gtk.main_quit)
w.show_all()
Gtk.main()

test.glade:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkBox" id="box1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkLinkButton" id="linkButton">
            <property name="label" translatable="yes">button</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="relief">none</property>
            <property name="uri">http://www.google.com</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="multilineLabel">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">this
is
a
multiline
label</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

Screenshot:

Screenshot

Currently, I study and try some tehniques described here, but I wish someone helps me earlier and faster.

like image 778
silviubogan Avatar asked Oct 20 '25 18:10

silviubogan


1 Answers

A simple solution would be to wrap the box container inside a Gtk.EventBox. This can be done easily in Glade by right clicking the box container and choosing Add Parent and then Event Box as illustrated in the image below (Note: the widget tree already has the event box added and that should be the result of adding the event box):

Glade add parent

Then, eg., in the code, box should be the event box to receive the button-release-event signal and the handler can receive the LinkButton from user_data and emit the signal clicked to simulate the button click.

Adapting the code:

# coding=utf-8

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

b = Gtk.Builder()
b.add_from_file("test.glade")

w = b.get_object("window1")

def box_clicked(widget, event, user_data):
    user_data.clicked()
    return True

linkButton = b.get_object("linkButton")
box = b.get_object("eventbox1")
box.connect("button-release-event", box_clicked, linkButton)

w.connect("delete-event", Gtk.main_quit)
w.show_all()
Gtk.main()
like image 88
José Fonte Avatar answered Oct 23 '25 09:10

José Fonte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!