天天看点

PyGobject(十七)布局容器之Button篇——Gtk.ModelButtonGtk.ModelButton附录

  • GtkModelButton
    • 继承关系
    • Methods
    • Virtual Methods
    • Properties
    • Signals
    • 例子
  • 附录
    • GtkButtonRole

Gtk.ModelButton

继承关系

Gtk.ModelButton可以用一个Gio.Action作为其模型。当点击按钮时,根据action-name来执行对应的方法。Gtk.ModelButton是Gtk.Button的直接子类

PyGobject(十七)布局容器之Button篇——Gtk.ModelButtonGtk.ModelButton附录

Methods

方法修饰词 方法名及参数
static new ()

Virtual Methods

Properties

Name Type Flags Short Description
active bool r/w/en 是否选中
centered bool r/w/en 内容是否居中
icon Gio.Icon r/w/en 图片
iconic bool r/w/en 是否是文字图标
inverted bool r/w/en 菜单是否是一个父容器
menu-name str r/w/en 要打开菜单的名字
role Gtk.ButtonRole r/w/en button样式
text str r/w/en 文本

Signals

Name Short Description

例子

PyGobject(十七)布局容器之Button篇——Gtk.ModelButtonGtk.ModelButton附录
PyGobject(十七)布局容器之Button篇——Gtk.ModelButtonGtk.ModelButton附录

这个例子的布局文件是通过glade生成的。

modelbutton.glade

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.6 -->
  <object class="GtkWindow" id="window1">
    <child type="titlebar">
      <object class="GtkHeaderBar">
        <property name="visible">1</property>
        <property name="show-close-button">1</property>
        <property name="title" translatable="yes">Model Button</property>
      </object>
    </child>
    <child>
      <object class="GtkBox">
        <property name="visible">1</property>
        <property name="orientation">vertical</property>
        <property name="margin">80</property>
        <child>
          <object class="GtkMenuButton">
            <property name="visible">1</property>
            <property name="popover">thing_a</property>
            <child>
              <object class="GtkLabel">
                <property name="visible">1</property>
                <property name="label">Color</property>
                <property name="hexpand">1</property>
              </object>
            </child>
          </object>
        </child>
        <child>
          <object class="GtkMenuButton">
            <property name="visible">1</property>
            <property name="popover">thing_b</property>
            <child>
              <object class="GtkLabel">
                <property name="visible">1</property>
                <property name="label">Flavors</property>
                <property name="hexpand">1</property>
              </object>
            </child>
          </object>
        </child>
        <child>
          <object class="GtkMenuButton">
            <property name="visible">1</property>
            <property name="popover">thing_c</property>
            <child>
              <object class="GtkLabel">
                <property name="visible">1</property>
                <property name="label">Tools</property>
                <property name="hexpand">1</property>
              </object>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkPopover" id="thing_a">
    <child>
      <object class="GtkBox">
        <property name="visible">1</property>
        <property name="margin">10</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="action-name">win.color</property>
            <property name="action-target">'red'</property>
            <property name="text">Red</property>
            <property name="inverted">1</property>
          </object>
        </child>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="action-name">win.color</property>
            <property name="action-target">'green'</property>
            <property name="text">Green</property>
            <property name="inverted">1</property>
          </object>
        </child>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="action-name">win.color</property>
            <property name="action-target">'blue'</property>
            <property name="text">Blue</property>
            <property name="inverted">1</property>
          </object>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkPopover" id="thing_b">
    <child>
      <object class="GtkBox">
        <property name="visible">1</property>
        <property name="margin">10</property>
        <property name="orientation">vertical</property>
        <property name="spacing">10</property>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="action-name">win.chocolate</property>
            <property name="text">Chocolate</property>
          </object>
        </child>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="action-name">win.vanilla</property>
            <property name="text">Vanilla</property>
          </object>
        </child>
        <child>
          <object class="GtkSeparator">
            <property name="visible">1</property>
          </object>
        </child>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="action-name">win.sprinkles</property>
            <property name="text">Add Sprinkles</property>
          </object>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkPopover" id="thing_c">
    <child>
      <object class="GtkBox">
        <property name="visible">1</property>
        <property name="margin">10</property>
        <property name="orientation">vertical</property>
        <property name="spacing">10</property>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="text">Hammer</property>
            <property name="role">check</property>
            <signal name="clicked" handler="tool_clicked"/>
          </object>
        </child>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="text">Screwdriver</property>
            <property name="role">check</property>
            <signal name="clicked" handler="tool_clicked"/>
          </object>
        </child>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="text">Drill</property>
            <property name="role">check</property>
            <signal name="clicked" handler="tool_clicked"/>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>
           

代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/14
# section 018
TITLE = "ModelButton"
DESCRIPTION = """
Gtk.ModelButton is a button class that can use a Gio.Action as its model.
"""
import gi

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

win_entries = ("color", "chocolate", "vanilla", "sprinkles")


class ModelButtonWindow():
    def __init__(self):
        builder = Gtk.Builder.new_from_file(os.path.join(os.path.dirname(__file__), "../../../Data/modelbutton.glade"))
        window = builder.get_object("window1")
        builder.connect_signals(self)
        actions = Gio.SimpleActionGroup()
        for entry in win_entries:
            action = Gio.SimpleAction.new(entry, GLib.VariantType.new("s") if entry == "color" else None)
            action.connect("activate", self.do_something)
            actions.insert(action)
        window.insert_action_group("win", actions)
        window.connect("destroy", Gtk.main_quit)
        window.show_all()
        Gtk.main()

    @staticmethod
    def tool_clicked(button):
        active = button.props.active
        button.props.active = not active

    @staticmethod
    def do_something(action, parameter):
        parameter_type = action.get_parameter_type()
        type_string = ""
        if parameter_type:
            type_string = parameter_type.dup_string()
        print(action.get_name(), type_string, parameter)


def main():
    ModelButtonWindow()


if __name__ == "__main__":
    main()
           

读取ui配置文件

builder = Gtk.Builder.new_from_file(os.path.join(os.path.dirname(__file__), "../../../Data/modelbutton.glade"))
           

从builder中获取id=”window1”的窗口

设置在当前类中处理ui中的信号

builder.connect_signals(self)
           

创建一个Gio.SimpleActionGroup(),用来处理选项的点击事件

创建四个Gio.SimpleAction,将其“activate”信号绑定到do_something()方法,并添加到Gio.SimpleActionGroup组中。

win_entries = ("color", "chocolate", "vanilla", "sprinkles")
for entry in win_entries:
    action = Gio.SimpleAction.new(entry, 
    GLib.VariantType.new("s") if entry == "color" else None)
    action.connect("activate", self.do_something)
    actions.insert(action)


 @staticmethod
def do_something(action, parameter):
    parameter_type = action.get_parameter_type()
    type_string = ""
    if parameter_type:
        type_string = parameter_type.dup_string()
    print(action.get_name(), type_string, parameter)
           

设置window的Gio.ActionGroup

window.insert_action_group("win", actions)
           

点击各item时打印信息,如下

color s 'red'
color s 'green'
color s 'blue'
sprinkles  None
vanilla  None
chocolate  None
           

附录

Gtk.ButtonRole

class Gtk.ButtonRole

Bases: GObject.GEnum

Gtk.ModelButton的样式

NORMAL = 0

普通按钮

CHECK = 1

CheckButton

RADIO = 2

RadioButton

代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728