为什么在Glade和代码中添加时,使用不同的参数调用事件处理程序?

时间:2017-08-22 23:53:41

标签: python-2.7 mouseevent gtk3 glade msys2

以下是4个文件:

案例#1 example1.py 使用 example1.glade

案例#2 example2.py 使用 example2.glade

要查看两种情况下的输出,每种情况的步骤都相同。打开其中一个程序后,只需单击多行标签或链接按钮,输出就会显示在终端中。

截图,两种情况相同

Screenshot for both cases

第一种情况

example1.py

# coding=utf-8

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


def box_clicked(*args):
    print(args)
    return True


b = Gtk.Builder()
b.add_from_file("example1.glade")
w = b.get_object("window1")
lb = b.get_object("linkButton")
eb = b.get_object("eventbox1")
eb.connect("button-release-event", box_clicked, lb)

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

example1.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>

信号和处理程序是从代码手动连接的。

传递给信号处理程序的参数示例,使用print(args)打印:

(<Gtk.EventBox object at 0x02bc4b6c (GtkEventBox at 0x02b9e290)>, <Gdk.EventButt
on object at 0x02bc4c34 (void at 0x02e691c8)>, <Gtk.LinkButton object at 0x02668
914 (GtkLinkButton at 0x02e37120)>)

漂亮的印刷品:

  1. Gtk.EventBox
  2. Gtk.EventButton
  3. Gtk.LinkBut​​ton
  4. 第二种情况

    example2.py

    # coding=utf-8
    
    import gi
    gi.require_version("Gtk", "3.0")
    from gi.repository import Gtk
    
    
    class Builder(Gtk.Builder):
        def __init__(self):
            Gtk.Builder.__init__(self)
            self.add_from_file("example2.glade")
            self.connect_signals(self)
            self.win = self.get_object("window1")
    
        def box_clicked(self, *args):
            print(args)
            return True
    
    
    win = Builder().win
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()
    

    example2.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>
                <signal name="button-release-event" handler="box_clicked" object="linkButton" swapped="no"/>
                <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>
    

    事件处理程序已连接到Glade中的信号。

    传递给信号处理程序的参数示例,使用print(args)打印:

    (<Gtk.LinkButton object at 0x02c68554 (GtkLinkButton at 0x02e96118)>, <Gdk.Event
    Button object at 0x02c6b5f4 (void at 0x02d56a60)>)
    

    漂亮的印刷品

    1. Gtk.LinkBut​​ton
    2. Gdk.EventButton
    3. 漂亮的打印比较

      第一种情况的论点:

      1. Gtk.EventBox
      2. Gtk.EventButton
      3. Gtk.LinkBut​​ton
      4. 第二种情况的论点:

        1. Gtk.LinkBut​​ton
        2. Gdk.EventButton
        3. 我想了解这两种情况下输出差异背后的原因。

          我在Windows 10上的MSYS2上使用Python 2.7.13和GTK + 3.22.MSYS2和Windows都安装了所有可用的更新。

1 个答案:

答案 0 :(得分:2)

第二个glade文件具有object元素的signal属性,相当于在C中使用g_signal_connect_object()而不是g_signal_connect()。它传递给定对象(链接按钮)作为信号处理程序的第一个参数。