如果在任何地方点击Gtk.Box,如何打开网页,尽管里面有Gtk.LinkBut​​ton链接?

时间:2017-08-22 13:47:31

标签: python-2.7 hyperlink ads gtk3 msys2

实际上,我喜欢Gtk.LinkBut​​ton的风格,我想用它在我的程序中制作广告横幅。下面的标签是链接目的地的单段描述。但是横幅上的任何地方都应该点击鼠标来打开链接。

这就是我尝试过的。当我点击Gtk.LinkButton时,两个URI都会被打开。当我点击窗口上的其他地方时,什么也没做。

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

目前,我研究并尝试了一些描述here的技术,但我希望有人能更早,更快地帮助我。

2 个答案:

答案 0 :(得分:3)

一个简单的解决方案是将盒子容器包装在Gtk.EventBox中。这可以通过右键单击框容器并选择添加父级然后选择事件框在Glade中轻松完成,如下图所示(注意:小部件树已经添加了事件框,这应该是添加事件框的结果):

Glade add parent

然后,例如,在代码中,框应该是接收按钮释放事件信号的事件框,处理程序可以从user_data接收LinkBut​​ton并发出单击的信号以模拟按钮单击。

调整代码:

# 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()

答案 1 :(得分:0)

使用the code modifications by José Fonte (please vote his answer too)时,单击标签时,网页会按预期打开,但当用户点击Gtk.LinkBut​​ton时,网页会打开两次。我通过在Glade中将Gtk.EventBox的above-child属性设置为On并从信号处理程序返回True来解决这个问题(行为在找到的文档{{3}中描述})。

Windows上的Adwaita窗口装饰也存在一些问题(10)。当可点击框是窗口中的第一个或唯一的子项时,单击死区然后移动窗口会触发网页的打开。我通过在Gtk.AspectFrame中插入Gtk.Box解决了这个问题。

最后的例子

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):
    #del widget, event, user_data

    # New tehnique:
    user_data.clicked()

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

    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()

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="GtkAspectFrame">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="label_xalign">0</property>
        <child>
          <object class="GtkEventBox" id="eventbox1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="above_child">True</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>
        </child>
      </object>
    </child>
  </object>
</interface>